| .. | .. |
|---|
| 1 | | -/* |
|---|
| 2 | | - * CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 2 | +/* CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface |
|---|
| 3 | 3 | * |
|---|
| 4 | 4 | * MCP2510 support and bug fixes by Christian Pellegrin |
|---|
| 5 | 5 | * <chripell@evolware.org> |
|---|
| .. | .. |
|---|
| 17 | 17 | * - Sascha Hauer, Marc Kleine-Budde, Pengutronix |
|---|
| 18 | 18 | * - Simon Kallweit, intefo AG |
|---|
| 19 | 19 | * Copyright 2007 |
|---|
| 20 | | - * |
|---|
| 21 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 22 | | - * it under the terms of the version 2 of the GNU General Public License |
|---|
| 23 | | - * as published by the Free Software Foundation |
|---|
| 24 | | - * |
|---|
| 25 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 26 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 27 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 28 | | - * GNU General Public License for more details. |
|---|
| 29 | | - * |
|---|
| 30 | | - * You should have received a copy of the GNU General Public License |
|---|
| 31 | | - * along with this program; if not, see <http://www.gnu.org/licenses/>. |
|---|
| 32 | | - * |
|---|
| 33 | | - * |
|---|
| 34 | | - * |
|---|
| 35 | | - * Your platform definition file should specify something like: |
|---|
| 36 | | - * |
|---|
| 37 | | - * static struct mcp251x_platform_data mcp251x_info = { |
|---|
| 38 | | - * .oscillator_frequency = 8000000, |
|---|
| 39 | | - * }; |
|---|
| 40 | | - * |
|---|
| 41 | | - * static struct spi_board_info spi_board_info[] = { |
|---|
| 42 | | - * { |
|---|
| 43 | | - * .modalias = "mcp2510", |
|---|
| 44 | | - * // "mcp2515" or "mcp25625" depending on your controller |
|---|
| 45 | | - * .platform_data = &mcp251x_info, |
|---|
| 46 | | - * .irq = IRQ_EINT13, |
|---|
| 47 | | - * .max_speed_hz = 2*1000*1000, |
|---|
| 48 | | - * .chip_select = 2, |
|---|
| 49 | | - * }, |
|---|
| 50 | | - * }; |
|---|
| 51 | | - * |
|---|
| 52 | | - * Please see mcp251x.h for a description of the fields in |
|---|
| 53 | | - * struct mcp251x_platform_data. |
|---|
| 54 | | - * |
|---|
| 55 | 20 | */ |
|---|
| 56 | 21 | |
|---|
| 22 | +#include <linux/bitfield.h> |
|---|
| 57 | 23 | #include <linux/can/core.h> |
|---|
| 58 | 24 | #include <linux/can/dev.h> |
|---|
| 59 | 25 | #include <linux/can/led.h> |
|---|
| 60 | | -#include <linux/can/platform/mcp251x.h> |
|---|
| 61 | 26 | #include <linux/clk.h> |
|---|
| 62 | 27 | #include <linux/completion.h> |
|---|
| 63 | 28 | #include <linux/delay.h> |
|---|
| 64 | 29 | #include <linux/device.h> |
|---|
| 65 | | -#include <linux/dma-mapping.h> |
|---|
| 66 | 30 | #include <linux/freezer.h> |
|---|
| 31 | +#include <linux/gpio.h> |
|---|
| 32 | +#include <linux/gpio/driver.h> |
|---|
| 67 | 33 | #include <linux/interrupt.h> |
|---|
| 68 | 34 | #include <linux/io.h> |
|---|
| 35 | +#include <linux/iopoll.h> |
|---|
| 69 | 36 | #include <linux/kernel.h> |
|---|
| 70 | 37 | #include <linux/module.h> |
|---|
| 71 | 38 | #include <linux/netdevice.h> |
|---|
| 72 | | -#include <linux/of.h> |
|---|
| 73 | | -#include <linux/of_device.h> |
|---|
| 74 | 39 | #include <linux/platform_device.h> |
|---|
| 40 | +#include <linux/property.h> |
|---|
| 41 | +#include <linux/regulator/consumer.h> |
|---|
| 75 | 42 | #include <linux/slab.h> |
|---|
| 76 | 43 | #include <linux/spi/spi.h> |
|---|
| 77 | 44 | #include <linux/uaccess.h> |
|---|
| 78 | | -#include <linux/regulator/consumer.h> |
|---|
| 79 | 45 | |
|---|
| 80 | 46 | /* SPI interface instruction set */ |
|---|
| 81 | 47 | #define INSTRUCTION_WRITE 0x02 |
|---|
| .. | .. |
|---|
| 89 | 55 | #define RTS_TXB2 0x04 |
|---|
| 90 | 56 | #define INSTRUCTION_RTS(n) (0x80 | ((n) & 0x07)) |
|---|
| 91 | 57 | |
|---|
| 92 | | - |
|---|
| 93 | 58 | /* MPC251x registers */ |
|---|
| 59 | +#define BFPCTRL 0x0c |
|---|
| 60 | +# define BFPCTRL_B0BFM BIT(0) |
|---|
| 61 | +# define BFPCTRL_B1BFM BIT(1) |
|---|
| 62 | +# define BFPCTRL_BFM(n) (BFPCTRL_B0BFM << (n)) |
|---|
| 63 | +# define BFPCTRL_BFM_MASK GENMASK(1, 0) |
|---|
| 64 | +# define BFPCTRL_B0BFE BIT(2) |
|---|
| 65 | +# define BFPCTRL_B1BFE BIT(3) |
|---|
| 66 | +# define BFPCTRL_BFE(n) (BFPCTRL_B0BFE << (n)) |
|---|
| 67 | +# define BFPCTRL_BFE_MASK GENMASK(3, 2) |
|---|
| 68 | +# define BFPCTRL_B0BFS BIT(4) |
|---|
| 69 | +# define BFPCTRL_B1BFS BIT(5) |
|---|
| 70 | +# define BFPCTRL_BFS(n) (BFPCTRL_B0BFS << (n)) |
|---|
| 71 | +# define BFPCTRL_BFS_MASK GENMASK(5, 4) |
|---|
| 72 | +#define TXRTSCTRL 0x0d |
|---|
| 73 | +# define TXRTSCTRL_B0RTSM BIT(0) |
|---|
| 74 | +# define TXRTSCTRL_B1RTSM BIT(1) |
|---|
| 75 | +# define TXRTSCTRL_B2RTSM BIT(2) |
|---|
| 76 | +# define TXRTSCTRL_RTSM(n) (TXRTSCTRL_B0RTSM << (n)) |
|---|
| 77 | +# define TXRTSCTRL_RTSM_MASK GENMASK(2, 0) |
|---|
| 78 | +# define TXRTSCTRL_B0RTS BIT(3) |
|---|
| 79 | +# define TXRTSCTRL_B1RTS BIT(4) |
|---|
| 80 | +# define TXRTSCTRL_B2RTS BIT(5) |
|---|
| 81 | +# define TXRTSCTRL_RTS(n) (TXRTSCTRL_B0RTS << (n)) |
|---|
| 82 | +# define TXRTSCTRL_RTS_MASK GENMASK(5, 3) |
|---|
| 94 | 83 | #define CANSTAT 0x0e |
|---|
| 95 | 84 | #define CANCTRL 0x0f |
|---|
| 96 | 85 | # define CANCTRL_REQOP_MASK 0xe0 |
|---|
| .. | .. |
|---|
| 205 | 194 | #define SET_BYTE(val, byte) \ |
|---|
| 206 | 195 | (((val) & 0xff) << ((byte) * 8)) |
|---|
| 207 | 196 | |
|---|
| 208 | | -/* |
|---|
| 209 | | - * Buffer size required for the largest SPI transfer (i.e., reading a |
|---|
| 197 | +/* Buffer size required for the largest SPI transfer (i.e., reading a |
|---|
| 210 | 198 | * frame) |
|---|
| 211 | 199 | */ |
|---|
| 212 | 200 | #define CAN_FRAME_MAX_DATA_LEN 8 |
|---|
| .. | .. |
|---|
| 218 | 206 | #define MCP251X_OST_DELAY_MS (5) |
|---|
| 219 | 207 | |
|---|
| 220 | 208 | #define DEVICE_NAME "mcp251x" |
|---|
| 221 | | - |
|---|
| 222 | | -static int mcp251x_enable_dma; /* Enable SPI DMA. Default: 0 (Off) */ |
|---|
| 223 | | -module_param(mcp251x_enable_dma, int, 0444); |
|---|
| 224 | | -MODULE_PARM_DESC(mcp251x_enable_dma, "Enable SPI DMA. Default: 0 (Off)"); |
|---|
| 225 | 209 | |
|---|
| 226 | 210 | static const struct can_bittiming_const mcp251x_bittiming_const = { |
|---|
| 227 | 211 | .name = DEVICE_NAME, |
|---|
| .. | .. |
|---|
| 251 | 235 | |
|---|
| 252 | 236 | u8 *spi_tx_buf; |
|---|
| 253 | 237 | u8 *spi_rx_buf; |
|---|
| 254 | | - dma_addr_t spi_tx_dma; |
|---|
| 255 | | - dma_addr_t spi_rx_dma; |
|---|
| 256 | 238 | |
|---|
| 257 | 239 | struct sk_buff *tx_skb; |
|---|
| 258 | 240 | int tx_len; |
|---|
| .. | .. |
|---|
| 271 | 253 | struct regulator *power; |
|---|
| 272 | 254 | struct regulator *transceiver; |
|---|
| 273 | 255 | struct clk *clk; |
|---|
| 256 | +#ifdef CONFIG_GPIOLIB |
|---|
| 257 | + struct gpio_chip gpio; |
|---|
| 258 | + u8 reg_bfpctrl; |
|---|
| 259 | +#endif |
|---|
| 274 | 260 | }; |
|---|
| 275 | 261 | |
|---|
| 276 | 262 | #define MCP251X_IS(_model) \ |
|---|
| .. | .. |
|---|
| 288 | 274 | |
|---|
| 289 | 275 | if (priv->tx_skb || priv->tx_len) |
|---|
| 290 | 276 | net->stats.tx_errors++; |
|---|
| 291 | | - if (priv->tx_skb) |
|---|
| 292 | | - dev_kfree_skb(priv->tx_skb); |
|---|
| 277 | + dev_kfree_skb(priv->tx_skb); |
|---|
| 293 | 278 | if (priv->tx_len) |
|---|
| 294 | 279 | can_free_echo_skb(priv->net, 0); |
|---|
| 295 | 280 | priv->tx_skb = NULL; |
|---|
| 296 | 281 | priv->tx_len = 0; |
|---|
| 297 | 282 | } |
|---|
| 298 | 283 | |
|---|
| 299 | | -/* |
|---|
| 300 | | - * Note about handling of error return of mcp251x_spi_trans: accessing |
|---|
| 284 | +/* Note about handling of error return of mcp251x_spi_trans: accessing |
|---|
| 301 | 285 | * registers via SPI is not really different conceptually than using |
|---|
| 302 | 286 | * normal I/O assembler instructions, although it's much more |
|---|
| 303 | 287 | * complicated from a practical POV. So it's not advisable to always |
|---|
| .. | .. |
|---|
| 322 | 306 | int ret; |
|---|
| 323 | 307 | |
|---|
| 324 | 308 | spi_message_init(&m); |
|---|
| 325 | | - |
|---|
| 326 | | - if (mcp251x_enable_dma) { |
|---|
| 327 | | - t.tx_dma = priv->spi_tx_dma; |
|---|
| 328 | | - t.rx_dma = priv->spi_rx_dma; |
|---|
| 329 | | - m.is_dma_mapped = 1; |
|---|
| 330 | | - } |
|---|
| 331 | | - |
|---|
| 332 | 309 | spi_message_add_tail(&t, &m); |
|---|
| 333 | 310 | |
|---|
| 334 | 311 | ret = spi_sync(spi, &m); |
|---|
| .. | .. |
|---|
| 337 | 314 | return ret; |
|---|
| 338 | 315 | } |
|---|
| 339 | 316 | |
|---|
| 340 | | -static u8 mcp251x_read_reg(struct spi_device *spi, uint8_t reg) |
|---|
| 317 | +static int mcp251x_spi_write(struct spi_device *spi, int len) |
|---|
| 318 | +{ |
|---|
| 319 | + struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 320 | + int ret; |
|---|
| 321 | + |
|---|
| 322 | + ret = spi_write(spi, priv->spi_tx_buf, len); |
|---|
| 323 | + if (ret) |
|---|
| 324 | + dev_err(&spi->dev, "spi write failed: ret = %d\n", ret); |
|---|
| 325 | + |
|---|
| 326 | + return ret; |
|---|
| 327 | +} |
|---|
| 328 | + |
|---|
| 329 | +static u8 mcp251x_read_reg(struct spi_device *spi, u8 reg) |
|---|
| 341 | 330 | { |
|---|
| 342 | 331 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 343 | 332 | u8 val = 0; |
|---|
| .. | .. |
|---|
| 345 | 334 | priv->spi_tx_buf[0] = INSTRUCTION_READ; |
|---|
| 346 | 335 | priv->spi_tx_buf[1] = reg; |
|---|
| 347 | 336 | |
|---|
| 348 | | - mcp251x_spi_trans(spi, 3); |
|---|
| 349 | | - val = priv->spi_rx_buf[2]; |
|---|
| 337 | + if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { |
|---|
| 338 | + spi_write_then_read(spi, priv->spi_tx_buf, 2, &val, 1); |
|---|
| 339 | + } else { |
|---|
| 340 | + mcp251x_spi_trans(spi, 3); |
|---|
| 341 | + val = priv->spi_rx_buf[2]; |
|---|
| 342 | + } |
|---|
| 350 | 343 | |
|---|
| 351 | 344 | return val; |
|---|
| 352 | 345 | } |
|---|
| 353 | 346 | |
|---|
| 354 | | -static void mcp251x_read_2regs(struct spi_device *spi, uint8_t reg, |
|---|
| 355 | | - uint8_t *v1, uint8_t *v2) |
|---|
| 347 | +static void mcp251x_read_2regs(struct spi_device *spi, u8 reg, u8 *v1, u8 *v2) |
|---|
| 356 | 348 | { |
|---|
| 357 | 349 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 358 | 350 | |
|---|
| 359 | 351 | priv->spi_tx_buf[0] = INSTRUCTION_READ; |
|---|
| 360 | 352 | priv->spi_tx_buf[1] = reg; |
|---|
| 361 | 353 | |
|---|
| 362 | | - mcp251x_spi_trans(spi, 4); |
|---|
| 354 | + if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { |
|---|
| 355 | + u8 val[2] = { 0 }; |
|---|
| 363 | 356 | |
|---|
| 364 | | - *v1 = priv->spi_rx_buf[2]; |
|---|
| 365 | | - *v2 = priv->spi_rx_buf[3]; |
|---|
| 357 | + spi_write_then_read(spi, priv->spi_tx_buf, 2, val, 2); |
|---|
| 358 | + *v1 = val[0]; |
|---|
| 359 | + *v2 = val[1]; |
|---|
| 360 | + } else { |
|---|
| 361 | + mcp251x_spi_trans(spi, 4); |
|---|
| 362 | + |
|---|
| 363 | + *v1 = priv->spi_rx_buf[2]; |
|---|
| 364 | + *v2 = priv->spi_rx_buf[3]; |
|---|
| 365 | + } |
|---|
| 366 | 366 | } |
|---|
| 367 | 367 | |
|---|
| 368 | | -static void mcp251x_write_reg(struct spi_device *spi, u8 reg, uint8_t val) |
|---|
| 368 | +static void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val) |
|---|
| 369 | 369 | { |
|---|
| 370 | 370 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 371 | 371 | |
|---|
| .. | .. |
|---|
| 373 | 373 | priv->spi_tx_buf[1] = reg; |
|---|
| 374 | 374 | priv->spi_tx_buf[2] = val; |
|---|
| 375 | 375 | |
|---|
| 376 | | - mcp251x_spi_trans(spi, 3); |
|---|
| 376 | + mcp251x_spi_write(spi, 3); |
|---|
| 377 | +} |
|---|
| 378 | + |
|---|
| 379 | +static void mcp251x_write_2regs(struct spi_device *spi, u8 reg, u8 v1, u8 v2) |
|---|
| 380 | +{ |
|---|
| 381 | + struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 382 | + |
|---|
| 383 | + priv->spi_tx_buf[0] = INSTRUCTION_WRITE; |
|---|
| 384 | + priv->spi_tx_buf[1] = reg; |
|---|
| 385 | + priv->spi_tx_buf[2] = v1; |
|---|
| 386 | + priv->spi_tx_buf[3] = v2; |
|---|
| 387 | + |
|---|
| 388 | + mcp251x_spi_write(spi, 4); |
|---|
| 377 | 389 | } |
|---|
| 378 | 390 | |
|---|
| 379 | 391 | static void mcp251x_write_bits(struct spi_device *spi, u8 reg, |
|---|
| 380 | | - u8 mask, uint8_t val) |
|---|
| 392 | + u8 mask, u8 val) |
|---|
| 381 | 393 | { |
|---|
| 382 | 394 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 383 | 395 | |
|---|
| .. | .. |
|---|
| 386 | 398 | priv->spi_tx_buf[2] = mask; |
|---|
| 387 | 399 | priv->spi_tx_buf[3] = val; |
|---|
| 388 | 400 | |
|---|
| 389 | | - mcp251x_spi_trans(spi, 4); |
|---|
| 401 | + mcp251x_spi_write(spi, 4); |
|---|
| 390 | 402 | } |
|---|
| 403 | + |
|---|
| 404 | +static u8 mcp251x_read_stat(struct spi_device *spi) |
|---|
| 405 | +{ |
|---|
| 406 | + return mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK; |
|---|
| 407 | +} |
|---|
| 408 | + |
|---|
| 409 | +#define mcp251x_read_stat_poll_timeout(addr, val, cond, delay_us, timeout_us) \ |
|---|
| 410 | + readx_poll_timeout(mcp251x_read_stat, addr, val, cond, \ |
|---|
| 411 | + delay_us, timeout_us) |
|---|
| 412 | + |
|---|
| 413 | +#ifdef CONFIG_GPIOLIB |
|---|
| 414 | +enum { |
|---|
| 415 | + MCP251X_GPIO_TX0RTS = 0, /* inputs */ |
|---|
| 416 | + MCP251X_GPIO_TX1RTS, |
|---|
| 417 | + MCP251X_GPIO_TX2RTS, |
|---|
| 418 | + MCP251X_GPIO_RX0BF, /* outputs */ |
|---|
| 419 | + MCP251X_GPIO_RX1BF, |
|---|
| 420 | +}; |
|---|
| 421 | + |
|---|
| 422 | +#define MCP251X_GPIO_INPUT_MASK \ |
|---|
| 423 | + GENMASK(MCP251X_GPIO_TX2RTS, MCP251X_GPIO_TX0RTS) |
|---|
| 424 | +#define MCP251X_GPIO_OUTPUT_MASK \ |
|---|
| 425 | + GENMASK(MCP251X_GPIO_RX1BF, MCP251X_GPIO_RX0BF) |
|---|
| 426 | + |
|---|
| 427 | +static const char * const mcp251x_gpio_names[] = { |
|---|
| 428 | + [MCP251X_GPIO_TX0RTS] = "TX0RTS", /* inputs */ |
|---|
| 429 | + [MCP251X_GPIO_TX1RTS] = "TX1RTS", |
|---|
| 430 | + [MCP251X_GPIO_TX2RTS] = "TX2RTS", |
|---|
| 431 | + [MCP251X_GPIO_RX0BF] = "RX0BF", /* outputs */ |
|---|
| 432 | + [MCP251X_GPIO_RX1BF] = "RX1BF", |
|---|
| 433 | +}; |
|---|
| 434 | + |
|---|
| 435 | +static inline bool mcp251x_gpio_is_input(unsigned int offset) |
|---|
| 436 | +{ |
|---|
| 437 | + return offset <= MCP251X_GPIO_TX2RTS; |
|---|
| 438 | +} |
|---|
| 439 | + |
|---|
| 440 | +static int mcp251x_gpio_request(struct gpio_chip *chip, |
|---|
| 441 | + unsigned int offset) |
|---|
| 442 | +{ |
|---|
| 443 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 444 | + u8 val; |
|---|
| 445 | + |
|---|
| 446 | + /* nothing to be done for inputs */ |
|---|
| 447 | + if (mcp251x_gpio_is_input(offset)) |
|---|
| 448 | + return 0; |
|---|
| 449 | + |
|---|
| 450 | + val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF); |
|---|
| 451 | + |
|---|
| 452 | + mutex_lock(&priv->mcp_lock); |
|---|
| 453 | + mcp251x_write_bits(priv->spi, BFPCTRL, val, val); |
|---|
| 454 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 455 | + |
|---|
| 456 | + priv->reg_bfpctrl |= val; |
|---|
| 457 | + |
|---|
| 458 | + return 0; |
|---|
| 459 | +} |
|---|
| 460 | + |
|---|
| 461 | +static void mcp251x_gpio_free(struct gpio_chip *chip, |
|---|
| 462 | + unsigned int offset) |
|---|
| 463 | +{ |
|---|
| 464 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 465 | + u8 val; |
|---|
| 466 | + |
|---|
| 467 | + /* nothing to be done for inputs */ |
|---|
| 468 | + if (mcp251x_gpio_is_input(offset)) |
|---|
| 469 | + return; |
|---|
| 470 | + |
|---|
| 471 | + val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF); |
|---|
| 472 | + |
|---|
| 473 | + mutex_lock(&priv->mcp_lock); |
|---|
| 474 | + mcp251x_write_bits(priv->spi, BFPCTRL, val, 0); |
|---|
| 475 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 476 | + |
|---|
| 477 | + priv->reg_bfpctrl &= ~val; |
|---|
| 478 | +} |
|---|
| 479 | + |
|---|
| 480 | +static int mcp251x_gpio_get_direction(struct gpio_chip *chip, |
|---|
| 481 | + unsigned int offset) |
|---|
| 482 | +{ |
|---|
| 483 | + if (mcp251x_gpio_is_input(offset)) |
|---|
| 484 | + return GPIOF_DIR_IN; |
|---|
| 485 | + |
|---|
| 486 | + return GPIOF_DIR_OUT; |
|---|
| 487 | +} |
|---|
| 488 | + |
|---|
| 489 | +static int mcp251x_gpio_get(struct gpio_chip *chip, unsigned int offset) |
|---|
| 490 | +{ |
|---|
| 491 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 492 | + u8 reg, mask, val; |
|---|
| 493 | + |
|---|
| 494 | + if (mcp251x_gpio_is_input(offset)) { |
|---|
| 495 | + reg = TXRTSCTRL; |
|---|
| 496 | + mask = TXRTSCTRL_RTS(offset); |
|---|
| 497 | + } else { |
|---|
| 498 | + reg = BFPCTRL; |
|---|
| 499 | + mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF); |
|---|
| 500 | + } |
|---|
| 501 | + |
|---|
| 502 | + mutex_lock(&priv->mcp_lock); |
|---|
| 503 | + val = mcp251x_read_reg(priv->spi, reg); |
|---|
| 504 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 505 | + |
|---|
| 506 | + return !!(val & mask); |
|---|
| 507 | +} |
|---|
| 508 | + |
|---|
| 509 | +static int mcp251x_gpio_get_multiple(struct gpio_chip *chip, |
|---|
| 510 | + unsigned long *maskp, unsigned long *bitsp) |
|---|
| 511 | +{ |
|---|
| 512 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 513 | + unsigned long bits = 0; |
|---|
| 514 | + u8 val; |
|---|
| 515 | + |
|---|
| 516 | + mutex_lock(&priv->mcp_lock); |
|---|
| 517 | + if (maskp[0] & MCP251X_GPIO_INPUT_MASK) { |
|---|
| 518 | + val = mcp251x_read_reg(priv->spi, TXRTSCTRL); |
|---|
| 519 | + val = FIELD_GET(TXRTSCTRL_RTS_MASK, val); |
|---|
| 520 | + bits |= FIELD_PREP(MCP251X_GPIO_INPUT_MASK, val); |
|---|
| 521 | + } |
|---|
| 522 | + if (maskp[0] & MCP251X_GPIO_OUTPUT_MASK) { |
|---|
| 523 | + val = mcp251x_read_reg(priv->spi, BFPCTRL); |
|---|
| 524 | + val = FIELD_GET(BFPCTRL_BFS_MASK, val); |
|---|
| 525 | + bits |= FIELD_PREP(MCP251X_GPIO_OUTPUT_MASK, val); |
|---|
| 526 | + } |
|---|
| 527 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 528 | + |
|---|
| 529 | + bitsp[0] = bits; |
|---|
| 530 | + return 0; |
|---|
| 531 | +} |
|---|
| 532 | + |
|---|
| 533 | +static void mcp251x_gpio_set(struct gpio_chip *chip, unsigned int offset, |
|---|
| 534 | + int value) |
|---|
| 535 | +{ |
|---|
| 536 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 537 | + u8 mask, val; |
|---|
| 538 | + |
|---|
| 539 | + mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF); |
|---|
| 540 | + val = value ? mask : 0; |
|---|
| 541 | + |
|---|
| 542 | + mutex_lock(&priv->mcp_lock); |
|---|
| 543 | + mcp251x_write_bits(priv->spi, BFPCTRL, mask, val); |
|---|
| 544 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 545 | + |
|---|
| 546 | + priv->reg_bfpctrl &= ~mask; |
|---|
| 547 | + priv->reg_bfpctrl |= val; |
|---|
| 548 | +} |
|---|
| 549 | + |
|---|
| 550 | +static void |
|---|
| 551 | +mcp251x_gpio_set_multiple(struct gpio_chip *chip, |
|---|
| 552 | + unsigned long *maskp, unsigned long *bitsp) |
|---|
| 553 | +{ |
|---|
| 554 | + struct mcp251x_priv *priv = gpiochip_get_data(chip); |
|---|
| 555 | + u8 mask, val; |
|---|
| 556 | + |
|---|
| 557 | + mask = FIELD_GET(MCP251X_GPIO_OUTPUT_MASK, maskp[0]); |
|---|
| 558 | + mask = FIELD_PREP(BFPCTRL_BFS_MASK, mask); |
|---|
| 559 | + |
|---|
| 560 | + val = FIELD_GET(MCP251X_GPIO_OUTPUT_MASK, bitsp[0]); |
|---|
| 561 | + val = FIELD_PREP(BFPCTRL_BFS_MASK, val); |
|---|
| 562 | + |
|---|
| 563 | + if (!mask) |
|---|
| 564 | + return; |
|---|
| 565 | + |
|---|
| 566 | + mutex_lock(&priv->mcp_lock); |
|---|
| 567 | + mcp251x_write_bits(priv->spi, BFPCTRL, mask, val); |
|---|
| 568 | + mutex_unlock(&priv->mcp_lock); |
|---|
| 569 | + |
|---|
| 570 | + priv->reg_bfpctrl &= ~mask; |
|---|
| 571 | + priv->reg_bfpctrl |= val; |
|---|
| 572 | +} |
|---|
| 573 | + |
|---|
| 574 | +static void mcp251x_gpio_restore(struct spi_device *spi) |
|---|
| 575 | +{ |
|---|
| 576 | + struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 577 | + |
|---|
| 578 | + mcp251x_write_reg(spi, BFPCTRL, priv->reg_bfpctrl); |
|---|
| 579 | +} |
|---|
| 580 | + |
|---|
| 581 | +static int mcp251x_gpio_setup(struct mcp251x_priv *priv) |
|---|
| 582 | +{ |
|---|
| 583 | + struct gpio_chip *gpio = &priv->gpio; |
|---|
| 584 | + |
|---|
| 585 | + if (!device_property_present(&priv->spi->dev, "gpio-controller")) |
|---|
| 586 | + return 0; |
|---|
| 587 | + |
|---|
| 588 | + /* gpiochip handles TX[0..2]RTS and RX[0..1]BF */ |
|---|
| 589 | + gpio->label = priv->spi->modalias; |
|---|
| 590 | + gpio->parent = &priv->spi->dev; |
|---|
| 591 | + gpio->owner = THIS_MODULE; |
|---|
| 592 | + gpio->request = mcp251x_gpio_request; |
|---|
| 593 | + gpio->free = mcp251x_gpio_free; |
|---|
| 594 | + gpio->get_direction = mcp251x_gpio_get_direction; |
|---|
| 595 | + gpio->get = mcp251x_gpio_get; |
|---|
| 596 | + gpio->get_multiple = mcp251x_gpio_get_multiple; |
|---|
| 597 | + gpio->set = mcp251x_gpio_set; |
|---|
| 598 | + gpio->set_multiple = mcp251x_gpio_set_multiple; |
|---|
| 599 | + gpio->base = -1; |
|---|
| 600 | + gpio->ngpio = ARRAY_SIZE(mcp251x_gpio_names); |
|---|
| 601 | + gpio->names = mcp251x_gpio_names; |
|---|
| 602 | + gpio->can_sleep = true; |
|---|
| 603 | +#ifdef CONFIG_OF_GPIO |
|---|
| 604 | + gpio->of_node = priv->spi->dev.of_node; |
|---|
| 605 | +#endif |
|---|
| 606 | + |
|---|
| 607 | + return devm_gpiochip_add_data(&priv->spi->dev, gpio, priv); |
|---|
| 608 | +} |
|---|
| 609 | +#else |
|---|
| 610 | +static inline void mcp251x_gpio_restore(struct spi_device *spi) |
|---|
| 611 | +{ |
|---|
| 612 | +} |
|---|
| 613 | + |
|---|
| 614 | +static inline int mcp251x_gpio_setup(struct mcp251x_priv *priv) |
|---|
| 615 | +{ |
|---|
| 616 | + return 0; |
|---|
| 617 | +} |
|---|
| 618 | +#endif |
|---|
| 391 | 619 | |
|---|
| 392 | 620 | static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, |
|---|
| 393 | 621 | int len, int tx_buf_idx) |
|---|
| .. | .. |
|---|
| 402 | 630 | buf[i]); |
|---|
| 403 | 631 | } else { |
|---|
| 404 | 632 | memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len); |
|---|
| 405 | | - mcp251x_spi_trans(spi, TXBDAT_OFF + len); |
|---|
| 633 | + mcp251x_spi_write(spi, TXBDAT_OFF + len); |
|---|
| 406 | 634 | } |
|---|
| 407 | 635 | } |
|---|
| 408 | 636 | |
|---|
| .. | .. |
|---|
| 434 | 662 | |
|---|
| 435 | 663 | /* use INSTRUCTION_RTS, to avoid "repeated frame problem" */ |
|---|
| 436 | 664 | priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx); |
|---|
| 437 | | - mcp251x_spi_trans(priv->spi, 1); |
|---|
| 665 | + mcp251x_spi_write(priv->spi, 1); |
|---|
| 438 | 666 | } |
|---|
| 439 | 667 | |
|---|
| 440 | 668 | static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf, |
|---|
| .. | .. |
|---|
| 453 | 681 | buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); |
|---|
| 454 | 682 | } else { |
|---|
| 455 | 683 | priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx); |
|---|
| 456 | | - mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); |
|---|
| 457 | | - memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN); |
|---|
| 684 | + if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { |
|---|
| 685 | + spi_write_then_read(spi, priv->spi_tx_buf, 1, |
|---|
| 686 | + priv->spi_rx_buf, |
|---|
| 687 | + SPI_TRANSFER_BUF_LEN); |
|---|
| 688 | + memcpy(buf + 1, priv->spi_rx_buf, |
|---|
| 689 | + SPI_TRANSFER_BUF_LEN - 1); |
|---|
| 690 | + } else { |
|---|
| 691 | + mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); |
|---|
| 692 | + memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN); |
|---|
| 693 | + } |
|---|
| 458 | 694 | } |
|---|
| 459 | 695 | } |
|---|
| 460 | 696 | |
|---|
| .. | .. |
|---|
| 512 | 748 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP); |
|---|
| 513 | 749 | } |
|---|
| 514 | 750 | |
|---|
| 751 | +/* May only be called when device is sleeping! */ |
|---|
| 752 | +static int mcp251x_hw_wake(struct spi_device *spi) |
|---|
| 753 | +{ |
|---|
| 754 | + u8 value; |
|---|
| 755 | + int ret; |
|---|
| 756 | + |
|---|
| 757 | + /* Force wakeup interrupt to wake device, but don't execute IST */ |
|---|
| 758 | + disable_irq(spi->irq); |
|---|
| 759 | + mcp251x_write_2regs(spi, CANINTE, CANINTE_WAKIE, CANINTF_WAKIF); |
|---|
| 760 | + |
|---|
| 761 | + /* Wait for oscillator startup timer after wake up */ |
|---|
| 762 | + mdelay(MCP251X_OST_DELAY_MS); |
|---|
| 763 | + |
|---|
| 764 | + /* Put device into config mode */ |
|---|
| 765 | + mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_CONF); |
|---|
| 766 | + |
|---|
| 767 | + /* Wait for the device to enter config mode */ |
|---|
| 768 | + ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, |
|---|
| 769 | + MCP251X_OST_DELAY_MS * 1000, |
|---|
| 770 | + USEC_PER_SEC); |
|---|
| 771 | + if (ret) { |
|---|
| 772 | + dev_err(&spi->dev, "MCP251x didn't enter in config mode\n"); |
|---|
| 773 | + return ret; |
|---|
| 774 | + } |
|---|
| 775 | + |
|---|
| 776 | + /* Disable and clear pending interrupts */ |
|---|
| 777 | + mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); |
|---|
| 778 | + enable_irq(spi->irq); |
|---|
| 779 | + |
|---|
| 780 | + return 0; |
|---|
| 781 | +} |
|---|
| 782 | + |
|---|
| 515 | 783 | static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb, |
|---|
| 516 | 784 | struct net_device *net) |
|---|
| 517 | 785 | { |
|---|
| .. | .. |
|---|
| 557 | 825 | static int mcp251x_set_normal_mode(struct spi_device *spi) |
|---|
| 558 | 826 | { |
|---|
| 559 | 827 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 560 | | - unsigned long timeout; |
|---|
| 828 | + u8 value; |
|---|
| 829 | + int ret; |
|---|
| 561 | 830 | |
|---|
| 562 | 831 | /* Enable interrupts */ |
|---|
| 563 | 832 | mcp251x_write_reg(spi, CANINTE, |
|---|
| .. | .. |
|---|
| 575 | 844 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL); |
|---|
| 576 | 845 | |
|---|
| 577 | 846 | /* Wait for the device to enter normal mode */ |
|---|
| 578 | | - timeout = jiffies + HZ; |
|---|
| 579 | | - while (mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) { |
|---|
| 580 | | - schedule(); |
|---|
| 581 | | - if (time_after(jiffies, timeout)) { |
|---|
| 582 | | - dev_err(&spi->dev, "MCP251x didn't" |
|---|
| 583 | | - " enter in normal mode\n"); |
|---|
| 584 | | - return -EBUSY; |
|---|
| 585 | | - } |
|---|
| 847 | + ret = mcp251x_read_stat_poll_timeout(spi, value, value == 0, |
|---|
| 848 | + MCP251X_OST_DELAY_MS * 1000, |
|---|
| 849 | + USEC_PER_SEC); |
|---|
| 850 | + if (ret) { |
|---|
| 851 | + dev_err(&spi->dev, "MCP251x didn't enter in normal mode\n"); |
|---|
| 852 | + return ret; |
|---|
| 586 | 853 | } |
|---|
| 587 | 854 | } |
|---|
| 588 | 855 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
|---|
| .. | .. |
|---|
| 626 | 893 | static int mcp251x_hw_reset(struct spi_device *spi) |
|---|
| 627 | 894 | { |
|---|
| 628 | 895 | struct mcp251x_priv *priv = spi_get_drvdata(spi); |
|---|
| 629 | | - unsigned long timeout; |
|---|
| 896 | + u8 value; |
|---|
| 630 | 897 | int ret; |
|---|
| 631 | 898 | |
|---|
| 632 | 899 | /* Wait for oscillator startup timer after power up */ |
|---|
| 633 | 900 | mdelay(MCP251X_OST_DELAY_MS); |
|---|
| 634 | 901 | |
|---|
| 635 | 902 | priv->spi_tx_buf[0] = INSTRUCTION_RESET; |
|---|
| 636 | | - ret = mcp251x_spi_trans(spi, 1); |
|---|
| 903 | + ret = mcp251x_spi_write(spi, 1); |
|---|
| 637 | 904 | if (ret) |
|---|
| 638 | 905 | return ret; |
|---|
| 639 | 906 | |
|---|
| .. | .. |
|---|
| 641 | 908 | mdelay(MCP251X_OST_DELAY_MS); |
|---|
| 642 | 909 | |
|---|
| 643 | 910 | /* Wait for reset to finish */ |
|---|
| 644 | | - timeout = jiffies + HZ; |
|---|
| 645 | | - while ((mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) != |
|---|
| 646 | | - CANCTRL_REQOP_CONF) { |
|---|
| 647 | | - usleep_range(MCP251X_OST_DELAY_MS * 1000, |
|---|
| 648 | | - MCP251X_OST_DELAY_MS * 1000 * 2); |
|---|
| 649 | | - |
|---|
| 650 | | - if (time_after(jiffies, timeout)) { |
|---|
| 651 | | - dev_err(&spi->dev, |
|---|
| 652 | | - "MCP251x didn't enter in conf mode after reset\n"); |
|---|
| 653 | | - return -EBUSY; |
|---|
| 654 | | - } |
|---|
| 655 | | - } |
|---|
| 656 | | - return 0; |
|---|
| 911 | + ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, |
|---|
| 912 | + MCP251X_OST_DELAY_MS * 1000, |
|---|
| 913 | + USEC_PER_SEC); |
|---|
| 914 | + if (ret) |
|---|
| 915 | + dev_err(&spi->dev, "MCP251x didn't enter in conf mode after reset\n"); |
|---|
| 916 | + return ret; |
|---|
| 657 | 917 | } |
|---|
| 658 | 918 | |
|---|
| 659 | 919 | static int mcp251x_hw_probe(struct spi_device *spi) |
|---|
| .. | .. |
|---|
| 696 | 956 | |
|---|
| 697 | 957 | priv->force_quit = 1; |
|---|
| 698 | 958 | free_irq(spi->irq, priv); |
|---|
| 699 | | - destroy_workqueue(priv->wq); |
|---|
| 700 | | - priv->wq = NULL; |
|---|
| 701 | 959 | |
|---|
| 702 | 960 | mutex_lock(&priv->mcp_lock); |
|---|
| 703 | 961 | |
|---|
| 704 | 962 | /* Disable and clear pending interrupts */ |
|---|
| 705 | | - mcp251x_write_reg(spi, CANINTE, 0x00); |
|---|
| 706 | | - mcp251x_write_reg(spi, CANINTF, 0x00); |
|---|
| 963 | + mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); |
|---|
| 707 | 964 | |
|---|
| 708 | 965 | mcp251x_write_reg(spi, TXBCTRL(0), 0); |
|---|
| 709 | 966 | mcp251x_clean(net); |
|---|
| .. | .. |
|---|
| 771 | 1028 | |
|---|
| 772 | 1029 | mutex_lock(&priv->mcp_lock); |
|---|
| 773 | 1030 | if (priv->after_suspend) { |
|---|
| 774 | | - mcp251x_hw_reset(spi); |
|---|
| 775 | | - mcp251x_setup(net, spi); |
|---|
| 1031 | + if (priv->after_suspend & AFTER_SUSPEND_POWER) { |
|---|
| 1032 | + mcp251x_hw_reset(spi); |
|---|
| 1033 | + mcp251x_setup(net, spi); |
|---|
| 1034 | + mcp251x_gpio_restore(spi); |
|---|
| 1035 | + } else { |
|---|
| 1036 | + mcp251x_hw_wake(spi); |
|---|
| 1037 | + } |
|---|
| 776 | 1038 | priv->force_quit = 0; |
|---|
| 777 | 1039 | if (priv->after_suspend & AFTER_SUSPEND_RESTART) { |
|---|
| 778 | 1040 | mcp251x_set_normal_mode(spi); |
|---|
| .. | .. |
|---|
| 812 | 1074 | |
|---|
| 813 | 1075 | mcp251x_read_2regs(spi, CANINTF, &intf, &eflag); |
|---|
| 814 | 1076 | |
|---|
| 815 | | - /* mask out flags we don't care about */ |
|---|
| 816 | | - intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR; |
|---|
| 817 | | - |
|---|
| 818 | 1077 | /* receive buffer 0 */ |
|---|
| 819 | 1078 | if (intf & CANINTF_RX0IF) { |
|---|
| 820 | 1079 | mcp251x_hw_rx(spi, 0); |
|---|
| .. | .. |
|---|
| 822 | 1081 | * (The MCP2515/25625 does this automatically.) |
|---|
| 823 | 1082 | */ |
|---|
| 824 | 1083 | if (mcp251x_is_2510(spi)) |
|---|
| 825 | | - mcp251x_write_bits(spi, CANINTF, CANINTF_RX0IF, 0x00); |
|---|
| 1084 | + mcp251x_write_bits(spi, CANINTF, |
|---|
| 1085 | + CANINTF_RX0IF, 0x00); |
|---|
| 1086 | + |
|---|
| 1087 | + /* check if buffer 1 is already known to be full, no need to re-read */ |
|---|
| 1088 | + if (!(intf & CANINTF_RX1IF)) { |
|---|
| 1089 | + u8 intf1, eflag1; |
|---|
| 1090 | + |
|---|
| 1091 | + /* intf needs to be read again to avoid a race condition */ |
|---|
| 1092 | + mcp251x_read_2regs(spi, CANINTF, &intf1, &eflag1); |
|---|
| 1093 | + |
|---|
| 1094 | + /* combine flags from both operations for error handling */ |
|---|
| 1095 | + intf |= intf1; |
|---|
| 1096 | + eflag |= eflag1; |
|---|
| 1097 | + } |
|---|
| 826 | 1098 | } |
|---|
| 827 | 1099 | |
|---|
| 828 | 1100 | /* receive buffer 1 */ |
|---|
| .. | .. |
|---|
| 832 | 1104 | if (mcp251x_is_2510(spi)) |
|---|
| 833 | 1105 | clear_intf |= CANINTF_RX1IF; |
|---|
| 834 | 1106 | } |
|---|
| 1107 | + |
|---|
| 1108 | + /* mask out flags we don't care about */ |
|---|
| 1109 | + intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR; |
|---|
| 835 | 1110 | |
|---|
| 836 | 1111 | /* any error or tx interrupt we need to clear? */ |
|---|
| 837 | 1112 | if (intf & (CANINTF_ERR | CANINTF_TX)) |
|---|
| .. | .. |
|---|
| 872 | 1147 | if (new_state >= CAN_STATE_ERROR_WARNING && |
|---|
| 873 | 1148 | new_state <= CAN_STATE_BUS_OFF) |
|---|
| 874 | 1149 | priv->can.can_stats.error_warning++; |
|---|
| 875 | | - case CAN_STATE_ERROR_WARNING: /* fallthrough */ |
|---|
| 1150 | + fallthrough; |
|---|
| 1151 | + case CAN_STATE_ERROR_WARNING: |
|---|
| 876 | 1152 | if (new_state >= CAN_STATE_ERROR_PASSIVE && |
|---|
| 877 | 1153 | new_state <= CAN_STATE_BUS_OFF) |
|---|
| 878 | 1154 | priv->can.can_stats.error_passive++; |
|---|
| .. | .. |
|---|
| 922 | 1198 | } |
|---|
| 923 | 1199 | netif_wake_queue(net); |
|---|
| 924 | 1200 | } |
|---|
| 925 | | - |
|---|
| 926 | 1201 | } |
|---|
| 927 | 1202 | mutex_unlock(&priv->mcp_lock); |
|---|
| 928 | 1203 | return IRQ_HANDLED; |
|---|
| .. | .. |
|---|
| 932 | 1207 | { |
|---|
| 933 | 1208 | struct mcp251x_priv *priv = netdev_priv(net); |
|---|
| 934 | 1209 | struct spi_device *spi = priv->spi; |
|---|
| 935 | | - unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING; |
|---|
| 1210 | + unsigned long flags = 0; |
|---|
| 936 | 1211 | int ret; |
|---|
| 937 | 1212 | |
|---|
| 938 | 1213 | ret = open_candev(net); |
|---|
| .. | .. |
|---|
| 948 | 1223 | priv->tx_skb = NULL; |
|---|
| 949 | 1224 | priv->tx_len = 0; |
|---|
| 950 | 1225 | |
|---|
| 1226 | + if (!dev_fwnode(&spi->dev)) |
|---|
| 1227 | + flags = IRQF_TRIGGER_FALLING; |
|---|
| 1228 | + |
|---|
| 951 | 1229 | ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist, |
|---|
| 952 | | - flags | IRQF_ONESHOT, DEVICE_NAME, priv); |
|---|
| 1230 | + flags | IRQF_ONESHOT, dev_name(&spi->dev), |
|---|
| 1231 | + priv); |
|---|
| 953 | 1232 | if (ret) { |
|---|
| 954 | 1233 | dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq); |
|---|
| 955 | 1234 | goto out_close; |
|---|
| 956 | 1235 | } |
|---|
| 957 | 1236 | |
|---|
| 958 | | - priv->wq = alloc_workqueue("mcp251x_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM, |
|---|
| 959 | | - 0); |
|---|
| 960 | | - if (!priv->wq) { |
|---|
| 961 | | - ret = -ENOMEM; |
|---|
| 962 | | - goto out_clean; |
|---|
| 963 | | - } |
|---|
| 964 | | - INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler); |
|---|
| 965 | | - INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler); |
|---|
| 966 | | - |
|---|
| 967 | | - ret = mcp251x_hw_reset(spi); |
|---|
| 1237 | + ret = mcp251x_hw_wake(spi); |
|---|
| 968 | 1238 | if (ret) |
|---|
| 969 | | - goto out_free_wq; |
|---|
| 1239 | + goto out_free_irq; |
|---|
| 970 | 1240 | ret = mcp251x_setup(net, spi); |
|---|
| 971 | 1241 | if (ret) |
|---|
| 972 | | - goto out_free_wq; |
|---|
| 1242 | + goto out_free_irq; |
|---|
| 973 | 1243 | ret = mcp251x_set_normal_mode(spi); |
|---|
| 974 | 1244 | if (ret) |
|---|
| 975 | | - goto out_free_wq; |
|---|
| 1245 | + goto out_free_irq; |
|---|
| 976 | 1246 | |
|---|
| 977 | 1247 | can_led_event(net, CAN_LED_EVENT_OPEN); |
|---|
| 978 | 1248 | |
|---|
| .. | .. |
|---|
| 981 | 1251 | |
|---|
| 982 | 1252 | return 0; |
|---|
| 983 | 1253 | |
|---|
| 984 | | -out_free_wq: |
|---|
| 985 | | - destroy_workqueue(priv->wq); |
|---|
| 986 | | -out_clean: |
|---|
| 1254 | +out_free_irq: |
|---|
| 987 | 1255 | free_irq(spi->irq, priv); |
|---|
| 988 | 1256 | mcp251x_hw_sleep(spi); |
|---|
| 989 | 1257 | out_close: |
|---|
| .. | .. |
|---|
| 1036 | 1304 | |
|---|
| 1037 | 1305 | static int mcp251x_can_probe(struct spi_device *spi) |
|---|
| 1038 | 1306 | { |
|---|
| 1039 | | - const struct of_device_id *of_id = of_match_device(mcp251x_of_match, |
|---|
| 1040 | | - &spi->dev); |
|---|
| 1041 | | - struct mcp251x_platform_data *pdata = dev_get_platdata(&spi->dev); |
|---|
| 1307 | + const void *match = device_get_match_data(&spi->dev); |
|---|
| 1042 | 1308 | struct net_device *net; |
|---|
| 1043 | 1309 | struct mcp251x_priv *priv; |
|---|
| 1044 | 1310 | struct clk *clk; |
|---|
| 1045 | | - int freq, ret; |
|---|
| 1311 | + u32 freq; |
|---|
| 1312 | + int ret; |
|---|
| 1046 | 1313 | |
|---|
| 1047 | | - clk = devm_clk_get(&spi->dev, NULL); |
|---|
| 1048 | | - if (IS_ERR(clk)) { |
|---|
| 1049 | | - if (pdata) |
|---|
| 1050 | | - freq = pdata->oscillator_frequency; |
|---|
| 1051 | | - else |
|---|
| 1052 | | - return PTR_ERR(clk); |
|---|
| 1053 | | - } else { |
|---|
| 1054 | | - freq = clk_get_rate(clk); |
|---|
| 1055 | | - } |
|---|
| 1314 | + clk = devm_clk_get_optional(&spi->dev, NULL); |
|---|
| 1315 | + if (IS_ERR(clk)) |
|---|
| 1316 | + return PTR_ERR(clk); |
|---|
| 1317 | + |
|---|
| 1318 | + freq = clk_get_rate(clk); |
|---|
| 1319 | + if (freq == 0) |
|---|
| 1320 | + device_property_read_u32(&spi->dev, "clock-frequency", &freq); |
|---|
| 1056 | 1321 | |
|---|
| 1057 | 1322 | /* Sanity check */ |
|---|
| 1058 | 1323 | if (freq < 1000000 || freq > 25000000) |
|---|
| .. | .. |
|---|
| 1063 | 1328 | if (!net) |
|---|
| 1064 | 1329 | return -ENOMEM; |
|---|
| 1065 | 1330 | |
|---|
| 1066 | | - if (!IS_ERR(clk)) { |
|---|
| 1067 | | - ret = clk_prepare_enable(clk); |
|---|
| 1068 | | - if (ret) |
|---|
| 1069 | | - goto out_free; |
|---|
| 1070 | | - } |
|---|
| 1331 | + ret = clk_prepare_enable(clk); |
|---|
| 1332 | + if (ret) |
|---|
| 1333 | + goto out_free; |
|---|
| 1071 | 1334 | |
|---|
| 1072 | 1335 | net->netdev_ops = &mcp251x_netdev_ops; |
|---|
| 1073 | 1336 | net->flags |= IFF_ECHO; |
|---|
| .. | .. |
|---|
| 1078 | 1341 | priv->can.clock.freq = freq / 2; |
|---|
| 1079 | 1342 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | |
|---|
| 1080 | 1343 | CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY; |
|---|
| 1081 | | - if (of_id) |
|---|
| 1082 | | - priv->model = (enum mcp251x_model)of_id->data; |
|---|
| 1344 | + if (match) |
|---|
| 1345 | + priv->model = (enum mcp251x_model)match; |
|---|
| 1083 | 1346 | else |
|---|
| 1084 | 1347 | priv->model = spi_get_device_id(spi)->driver_data; |
|---|
| 1085 | 1348 | priv->net = net; |
|---|
| .. | .. |
|---|
| 1109 | 1372 | if (ret) |
|---|
| 1110 | 1373 | goto out_clk; |
|---|
| 1111 | 1374 | |
|---|
| 1375 | + priv->wq = alloc_workqueue("mcp251x_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM, |
|---|
| 1376 | + 0); |
|---|
| 1377 | + if (!priv->wq) { |
|---|
| 1378 | + ret = -ENOMEM; |
|---|
| 1379 | + goto out_clk; |
|---|
| 1380 | + } |
|---|
| 1381 | + INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler); |
|---|
| 1382 | + INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler); |
|---|
| 1383 | + |
|---|
| 1112 | 1384 | priv->spi = spi; |
|---|
| 1113 | 1385 | mutex_init(&priv->mcp_lock); |
|---|
| 1114 | 1386 | |
|---|
| 1115 | | - /* If requested, allocate DMA buffers */ |
|---|
| 1116 | | - if (mcp251x_enable_dma) { |
|---|
| 1117 | | - spi->dev.coherent_dma_mask = ~0; |
|---|
| 1118 | | - |
|---|
| 1119 | | - /* |
|---|
| 1120 | | - * Minimum coherent DMA allocation is PAGE_SIZE, so allocate |
|---|
| 1121 | | - * that much and share it between Tx and Rx DMA buffers. |
|---|
| 1122 | | - */ |
|---|
| 1123 | | - priv->spi_tx_buf = dmam_alloc_coherent(&spi->dev, |
|---|
| 1124 | | - PAGE_SIZE, |
|---|
| 1125 | | - &priv->spi_tx_dma, |
|---|
| 1126 | | - GFP_DMA); |
|---|
| 1127 | | - |
|---|
| 1128 | | - if (priv->spi_tx_buf) { |
|---|
| 1129 | | - priv->spi_rx_buf = (priv->spi_tx_buf + (PAGE_SIZE / 2)); |
|---|
| 1130 | | - priv->spi_rx_dma = (dma_addr_t)(priv->spi_tx_dma + |
|---|
| 1131 | | - (PAGE_SIZE / 2)); |
|---|
| 1132 | | - } else { |
|---|
| 1133 | | - /* Fall back to non-DMA */ |
|---|
| 1134 | | - mcp251x_enable_dma = 0; |
|---|
| 1135 | | - } |
|---|
| 1387 | + priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, |
|---|
| 1388 | + GFP_KERNEL); |
|---|
| 1389 | + if (!priv->spi_tx_buf) { |
|---|
| 1390 | + ret = -ENOMEM; |
|---|
| 1391 | + goto error_probe; |
|---|
| 1136 | 1392 | } |
|---|
| 1137 | 1393 | |
|---|
| 1138 | | - /* Allocate non-DMA buffers */ |
|---|
| 1139 | | - if (!mcp251x_enable_dma) { |
|---|
| 1140 | | - priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, |
|---|
| 1141 | | - GFP_KERNEL); |
|---|
| 1142 | | - if (!priv->spi_tx_buf) { |
|---|
| 1143 | | - ret = -ENOMEM; |
|---|
| 1144 | | - goto error_probe; |
|---|
| 1145 | | - } |
|---|
| 1146 | | - priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, |
|---|
| 1147 | | - GFP_KERNEL); |
|---|
| 1148 | | - if (!priv->spi_rx_buf) { |
|---|
| 1149 | | - ret = -ENOMEM; |
|---|
| 1150 | | - goto error_probe; |
|---|
| 1151 | | - } |
|---|
| 1394 | + priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, |
|---|
| 1395 | + GFP_KERNEL); |
|---|
| 1396 | + if (!priv->spi_rx_buf) { |
|---|
| 1397 | + ret = -ENOMEM; |
|---|
| 1398 | + goto error_probe; |
|---|
| 1152 | 1399 | } |
|---|
| 1153 | 1400 | |
|---|
| 1154 | 1401 | SET_NETDEV_DEV(net, &spi->dev); |
|---|
| .. | .. |
|---|
| 1157 | 1404 | ret = mcp251x_hw_probe(spi); |
|---|
| 1158 | 1405 | if (ret) { |
|---|
| 1159 | 1406 | if (ret == -ENODEV) |
|---|
| 1160 | | - dev_err(&spi->dev, "Cannot initialize MCP%x. Wrong wiring?\n", priv->model); |
|---|
| 1407 | + dev_err(&spi->dev, "Cannot initialize MCP%x. Wrong wiring?\n", |
|---|
| 1408 | + priv->model); |
|---|
| 1161 | 1409 | goto error_probe; |
|---|
| 1162 | 1410 | } |
|---|
| 1163 | 1411 | |
|---|
| .. | .. |
|---|
| 1169 | 1417 | |
|---|
| 1170 | 1418 | devm_can_led_init(net); |
|---|
| 1171 | 1419 | |
|---|
| 1420 | + ret = mcp251x_gpio_setup(priv); |
|---|
| 1421 | + if (ret) |
|---|
| 1422 | + goto out_unregister_candev; |
|---|
| 1423 | + |
|---|
| 1172 | 1424 | netdev_info(net, "MCP%x successfully initialized.\n", priv->model); |
|---|
| 1173 | 1425 | return 0; |
|---|
| 1174 | 1426 | |
|---|
| 1427 | +out_unregister_candev: |
|---|
| 1428 | + unregister_candev(net); |
|---|
| 1429 | + |
|---|
| 1175 | 1430 | error_probe: |
|---|
| 1431 | + destroy_workqueue(priv->wq); |
|---|
| 1432 | + priv->wq = NULL; |
|---|
| 1176 | 1433 | mcp251x_power_enable(priv->power, 0); |
|---|
| 1177 | 1434 | |
|---|
| 1178 | 1435 | out_clk: |
|---|
| 1179 | | - if (!IS_ERR(clk)) |
|---|
| 1180 | | - clk_disable_unprepare(clk); |
|---|
| 1436 | + clk_disable_unprepare(clk); |
|---|
| 1181 | 1437 | |
|---|
| 1182 | 1438 | out_free: |
|---|
| 1183 | 1439 | free_candev(net); |
|---|
| .. | .. |
|---|
| 1195 | 1451 | |
|---|
| 1196 | 1452 | mcp251x_power_enable(priv->power, 0); |
|---|
| 1197 | 1453 | |
|---|
| 1198 | | - if (!IS_ERR(priv->clk)) |
|---|
| 1199 | | - clk_disable_unprepare(priv->clk); |
|---|
| 1454 | + destroy_workqueue(priv->wq); |
|---|
| 1455 | + priv->wq = NULL; |
|---|
| 1456 | + |
|---|
| 1457 | + clk_disable_unprepare(priv->clk); |
|---|
| 1200 | 1458 | |
|---|
| 1201 | 1459 | free_candev(net); |
|---|
| 1202 | 1460 | |
|---|
| .. | .. |
|---|
| 1211 | 1469 | |
|---|
| 1212 | 1470 | priv->force_quit = 1; |
|---|
| 1213 | 1471 | disable_irq(spi->irq); |
|---|
| 1214 | | - /* |
|---|
| 1215 | | - * Note: at this point neither IST nor workqueues are running. |
|---|
| 1472 | + /* Note: at this point neither IST nor workqueues are running. |
|---|
| 1216 | 1473 | * open/stop cannot be called anyway so locking is not needed |
|---|
| 1217 | 1474 | */ |
|---|
| 1218 | 1475 | if (netif_running(net)) { |
|---|
| .. | .. |
|---|
| 1225 | 1482 | priv->after_suspend = AFTER_SUSPEND_DOWN; |
|---|
| 1226 | 1483 | } |
|---|
| 1227 | 1484 | |
|---|
| 1228 | | - if (!IS_ERR_OR_NULL(priv->power)) { |
|---|
| 1229 | | - regulator_disable(priv->power); |
|---|
| 1230 | | - priv->after_suspend |= AFTER_SUSPEND_POWER; |
|---|
| 1231 | | - } |
|---|
| 1485 | + mcp251x_power_enable(priv->power, 0); |
|---|
| 1486 | + priv->after_suspend |= AFTER_SUSPEND_POWER; |
|---|
| 1232 | 1487 | |
|---|
| 1233 | 1488 | return 0; |
|---|
| 1234 | 1489 | } |
|---|
| .. | .. |
|---|
| 1240 | 1495 | |
|---|
| 1241 | 1496 | if (priv->after_suspend & AFTER_SUSPEND_POWER) |
|---|
| 1242 | 1497 | mcp251x_power_enable(priv->power, 1); |
|---|
| 1243 | | - |
|---|
| 1244 | | - if (priv->after_suspend & AFTER_SUSPEND_UP) { |
|---|
| 1498 | + if (priv->after_suspend & AFTER_SUSPEND_UP) |
|---|
| 1245 | 1499 | mcp251x_power_enable(priv->transceiver, 1); |
|---|
| 1500 | + |
|---|
| 1501 | + if (priv->after_suspend & (AFTER_SUSPEND_POWER | AFTER_SUSPEND_UP)) |
|---|
| 1246 | 1502 | queue_work(priv->wq, &priv->restart_work); |
|---|
| 1247 | | - } else { |
|---|
| 1503 | + else |
|---|
| 1248 | 1504 | priv->after_suspend = 0; |
|---|
| 1249 | | - } |
|---|
| 1250 | 1505 | |
|---|
| 1251 | 1506 | priv->force_quit = 0; |
|---|
| 1252 | 1507 | enable_irq(spi->irq); |
|---|