Lines Matching +full:- +full:spi

1 // SPDX-License-Identifier: GPL-2.0-only
2 /* CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface
17 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
18 * - Simon Kallweit, intefo AG
42 #include <linux/spi/spi.h>
45 /* SPI interface instruction set */
196 /* Buffer size required for the largest SPI transfer (i.e., reading a
230 struct spi_device *spi; member
233 struct mutex mcp_lock; /* SPI device lock */
263 static inline int mcp251x_is_##_model(struct spi_device *spi) \
265 struct mcp251x_priv *priv = spi_get_drvdata(spi); \
266 return priv->model == CAN_MCP251X_MCP##_model; \
275 if (priv->tx_skb || priv->tx_busy) in mcp251x_clean()
276 net->stats.tx_errors++; in mcp251x_clean()
277 dev_kfree_skb(priv->tx_skb); in mcp251x_clean()
278 if (priv->tx_busy) in mcp251x_clean()
279 can_free_echo_skb(priv->net, 0, NULL); in mcp251x_clean()
280 priv->tx_skb = NULL; in mcp251x_clean()
281 priv->tx_busy = false; in mcp251x_clean()
285 * registers via SPI is not really different conceptually than using
296 static int mcp251x_spi_trans(struct spi_device *spi, int len) in mcp251x_spi_trans() argument
298 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_spi_trans()
300 .tx_buf = priv->spi_tx_buf, in mcp251x_spi_trans()
301 .rx_buf = priv->spi_rx_buf, in mcp251x_spi_trans()
311 ret = spi_sync(spi, &m); in mcp251x_spi_trans()
313 dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret); in mcp251x_spi_trans()
317 static int mcp251x_spi_write(struct spi_device *spi, int len) in mcp251x_spi_write() argument
319 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_spi_write()
322 ret = spi_write(spi, priv->spi_tx_buf, len); in mcp251x_spi_write()
324 dev_err(&spi->dev, "spi write failed: ret = %d\n", ret); in mcp251x_spi_write()
329 static u8 mcp251x_read_reg(struct spi_device *spi, u8 reg) in mcp251x_read_reg() argument
331 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_read_reg()
334 priv->spi_tx_buf[0] = INSTRUCTION_READ; in mcp251x_read_reg()
335 priv->spi_tx_buf[1] = reg; in mcp251x_read_reg()
337 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { in mcp251x_read_reg()
338 spi_write_then_read(spi, priv->spi_tx_buf, 2, &val, 1); in mcp251x_read_reg()
340 mcp251x_spi_trans(spi, 3); in mcp251x_read_reg()
341 val = priv->spi_rx_buf[2]; in mcp251x_read_reg()
347 static void mcp251x_read_2regs(struct spi_device *spi, u8 reg, u8 *v1, u8 *v2) in mcp251x_read_2regs() argument
349 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_read_2regs()
351 priv->spi_tx_buf[0] = INSTRUCTION_READ; in mcp251x_read_2regs()
352 priv->spi_tx_buf[1] = reg; in mcp251x_read_2regs()
354 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { in mcp251x_read_2regs()
357 spi_write_then_read(spi, priv->spi_tx_buf, 2, val, 2); in mcp251x_read_2regs()
361 mcp251x_spi_trans(spi, 4); in mcp251x_read_2regs()
363 *v1 = priv->spi_rx_buf[2]; in mcp251x_read_2regs()
364 *v2 = priv->spi_rx_buf[3]; in mcp251x_read_2regs()
368 static void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val) in mcp251x_write_reg() argument
370 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_write_reg()
372 priv->spi_tx_buf[0] = INSTRUCTION_WRITE; in mcp251x_write_reg()
373 priv->spi_tx_buf[1] = reg; in mcp251x_write_reg()
374 priv->spi_tx_buf[2] = val; in mcp251x_write_reg()
376 mcp251x_spi_write(spi, 3); in mcp251x_write_reg()
379 static void mcp251x_write_2regs(struct spi_device *spi, u8 reg, u8 v1, u8 v2) in mcp251x_write_2regs() argument
381 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_write_2regs()
383 priv->spi_tx_buf[0] = INSTRUCTION_WRITE; in mcp251x_write_2regs()
384 priv->spi_tx_buf[1] = reg; in mcp251x_write_2regs()
385 priv->spi_tx_buf[2] = v1; in mcp251x_write_2regs()
386 priv->spi_tx_buf[3] = v2; in mcp251x_write_2regs()
388 mcp251x_spi_write(spi, 4); in mcp251x_write_2regs()
391 static void mcp251x_write_bits(struct spi_device *spi, u8 reg, in mcp251x_write_bits() argument
394 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_write_bits()
396 priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY; in mcp251x_write_bits()
397 priv->spi_tx_buf[1] = reg; in mcp251x_write_bits()
398 priv->spi_tx_buf[2] = mask; in mcp251x_write_bits()
399 priv->spi_tx_buf[3] = val; in mcp251x_write_bits()
401 mcp251x_spi_write(spi, 4); in mcp251x_write_bits()
404 static u8 mcp251x_read_stat(struct spi_device *spi) in mcp251x_read_stat() argument
406 return mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK; in mcp251x_read_stat()
450 val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF); in mcp251x_gpio_request()
452 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_request()
453 mcp251x_write_bits(priv->spi, BFPCTRL, val, val); in mcp251x_gpio_request()
454 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_request()
456 priv->reg_bfpctrl |= val; in mcp251x_gpio_request()
471 val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF); in mcp251x_gpio_free()
473 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_free()
474 mcp251x_write_bits(priv->spi, BFPCTRL, val, 0); in mcp251x_gpio_free()
475 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_free()
477 priv->reg_bfpctrl &= ~val; in mcp251x_gpio_free()
499 mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF); in mcp251x_gpio_get()
502 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_get()
503 val = mcp251x_read_reg(priv->spi, reg); in mcp251x_gpio_get()
504 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_get()
516 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_get_multiple()
518 val = mcp251x_read_reg(priv->spi, TXRTSCTRL); in mcp251x_gpio_get_multiple()
523 val = mcp251x_read_reg(priv->spi, BFPCTRL); in mcp251x_gpio_get_multiple()
527 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_get_multiple()
539 mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF); in mcp251x_gpio_set()
542 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_set()
543 mcp251x_write_bits(priv->spi, BFPCTRL, mask, val); in mcp251x_gpio_set()
544 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_set()
546 priv->reg_bfpctrl &= ~mask; in mcp251x_gpio_set()
547 priv->reg_bfpctrl |= val; in mcp251x_gpio_set()
566 mutex_lock(&priv->mcp_lock); in mcp251x_gpio_set_multiple()
567 mcp251x_write_bits(priv->spi, BFPCTRL, mask, val); in mcp251x_gpio_set_multiple()
568 mutex_unlock(&priv->mcp_lock); in mcp251x_gpio_set_multiple()
570 priv->reg_bfpctrl &= ~mask; in mcp251x_gpio_set_multiple()
571 priv->reg_bfpctrl |= val; in mcp251x_gpio_set_multiple()
574 static void mcp251x_gpio_restore(struct spi_device *spi) in mcp251x_gpio_restore() argument
576 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_gpio_restore()
578 mcp251x_write_reg(spi, BFPCTRL, priv->reg_bfpctrl); in mcp251x_gpio_restore()
583 struct gpio_chip *gpio = &priv->gpio; in mcp251x_gpio_setup()
585 if (!device_property_present(&priv->spi->dev, "gpio-controller")) in mcp251x_gpio_setup()
589 gpio->label = priv->spi->modalias; in mcp251x_gpio_setup()
590 gpio->parent = &priv->spi->dev; in mcp251x_gpio_setup()
591 gpio->owner = THIS_MODULE; in mcp251x_gpio_setup()
592 gpio->request = mcp251x_gpio_request; in mcp251x_gpio_setup()
593 gpio->free = mcp251x_gpio_free; in mcp251x_gpio_setup()
594 gpio->get_direction = mcp251x_gpio_get_direction; in mcp251x_gpio_setup()
595 gpio->get = mcp251x_gpio_get; in mcp251x_gpio_setup()
596 gpio->get_multiple = mcp251x_gpio_get_multiple; in mcp251x_gpio_setup()
597 gpio->set = mcp251x_gpio_set; in mcp251x_gpio_setup()
598 gpio->set_multiple = mcp251x_gpio_set_multiple; in mcp251x_gpio_setup()
599 gpio->base = -1; in mcp251x_gpio_setup()
600 gpio->ngpio = ARRAY_SIZE(mcp251x_gpio_names); in mcp251x_gpio_setup()
601 gpio->names = mcp251x_gpio_names; in mcp251x_gpio_setup()
602 gpio->can_sleep = true; in mcp251x_gpio_setup()
604 return devm_gpiochip_add_data(&priv->spi->dev, gpio, priv); in mcp251x_gpio_setup()
607 static inline void mcp251x_gpio_restore(struct spi_device *spi) in mcp251x_gpio_restore() argument
617 static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, in mcp251x_hw_tx_frame() argument
620 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_tx_frame()
622 if (mcp251x_is_2510(spi)) { in mcp251x_hw_tx_frame()
626 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i, in mcp251x_hw_tx_frame()
629 memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len); in mcp251x_hw_tx_frame()
630 mcp251x_spi_write(spi, TXBDAT_OFF + len); in mcp251x_hw_tx_frame()
634 static void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame, in mcp251x_hw_tx() argument
637 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_tx()
641 exide = (frame->can_id & CAN_EFF_FLAG) ? 1 : 0; /* Extended ID Enable */ in mcp251x_hw_tx()
643 sid = (frame->can_id & CAN_EFF_MASK) >> 18; in mcp251x_hw_tx()
645 sid = frame->can_id & CAN_SFF_MASK; /* Standard ID */ in mcp251x_hw_tx()
646 eid = frame->can_id & CAN_EFF_MASK; /* Extended ID */ in mcp251x_hw_tx()
647 rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0; /* Remote transmission */ in mcp251x_hw_tx()
656 buf[TXBDLC_OFF] = (rtr << DLC_RTR_SHIFT) | frame->len; in mcp251x_hw_tx()
657 memcpy(buf + TXBDAT_OFF, frame->data, frame->len); in mcp251x_hw_tx()
658 mcp251x_hw_tx_frame(spi, buf, frame->len, tx_buf_idx); in mcp251x_hw_tx()
661 priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx); in mcp251x_hw_tx()
662 mcp251x_spi_write(priv->spi, 1); in mcp251x_hw_tx()
665 static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf, in mcp251x_hw_rx_frame() argument
668 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_rx_frame()
670 if (mcp251x_is_2510(spi)) { in mcp251x_hw_rx_frame()
674 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); in mcp251x_hw_rx_frame()
678 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); in mcp251x_hw_rx_frame()
680 priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx); in mcp251x_hw_rx_frame()
681 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { in mcp251x_hw_rx_frame()
682 spi_write_then_read(spi, priv->spi_tx_buf, 1, in mcp251x_hw_rx_frame()
683 priv->spi_rx_buf, in mcp251x_hw_rx_frame()
685 memcpy(buf + 1, priv->spi_rx_buf, in mcp251x_hw_rx_frame()
686 SPI_TRANSFER_BUF_LEN - 1); in mcp251x_hw_rx_frame()
688 mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); in mcp251x_hw_rx_frame()
689 memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN); in mcp251x_hw_rx_frame()
694 static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx) in mcp251x_hw_rx() argument
696 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_rx()
701 skb = alloc_can_skb(priv->net, &frame); in mcp251x_hw_rx()
703 dev_err(&spi->dev, "cannot allocate RX skb\n"); in mcp251x_hw_rx()
704 priv->net->stats.rx_dropped++; in mcp251x_hw_rx()
708 mcp251x_hw_rx_frame(spi, buf, buf_idx); in mcp251x_hw_rx()
711 frame->can_id = CAN_EFF_FLAG; in mcp251x_hw_rx()
712 frame->can_id |= in mcp251x_hw_rx()
722 frame->can_id |= CAN_RTR_FLAG; in mcp251x_hw_rx()
725 frame->can_id = in mcp251x_hw_rx()
729 frame->can_id |= CAN_RTR_FLAG; in mcp251x_hw_rx()
732 frame->len = can_cc_dlc2len(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK); in mcp251x_hw_rx()
733 if (!(frame->can_id & CAN_RTR_FLAG)) { in mcp251x_hw_rx()
734 memcpy(frame->data, buf + RXBDAT_OFF, frame->len); in mcp251x_hw_rx()
736 priv->net->stats.rx_bytes += frame->len; in mcp251x_hw_rx()
738 priv->net->stats.rx_packets++; in mcp251x_hw_rx()
743 static void mcp251x_hw_sleep(struct spi_device *spi) in mcp251x_hw_sleep() argument
745 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP); in mcp251x_hw_sleep()
749 static int mcp251x_hw_wake(struct spi_device *spi) in mcp251x_hw_wake() argument
755 disable_irq_nosync(spi->irq); in mcp251x_hw_wake()
756 mcp251x_write_2regs(spi, CANINTE, CANINTE_WAKIE, CANINTF_WAKIF); in mcp251x_hw_wake()
762 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_CONF); in mcp251x_hw_wake()
765 ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, in mcp251x_hw_wake()
769 dev_err(&spi->dev, "MCP251x didn't enter in config mode\n"); in mcp251x_hw_wake()
774 mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); in mcp251x_hw_wake()
775 enable_irq(spi->irq); in mcp251x_hw_wake()
784 struct spi_device *spi = priv->spi; in mcp251x_hard_start_xmit() local
786 if (priv->tx_skb || priv->tx_busy) { in mcp251x_hard_start_xmit()
787 dev_warn(&spi->dev, "hard_xmit called while tx busy\n"); in mcp251x_hard_start_xmit()
795 priv->tx_skb = skb; in mcp251x_hard_start_xmit()
796 queue_work(priv->wq, &priv->tx_work); in mcp251x_hard_start_xmit()
808 /* We have to delay work since SPI I/O may sleep */ in mcp251x_do_set_mode()
809 priv->can.state = CAN_STATE_ERROR_ACTIVE; in mcp251x_do_set_mode()
810 priv->restart_tx = 1; in mcp251x_do_set_mode()
811 if (priv->can.restart_ms == 0) in mcp251x_do_set_mode()
812 priv->after_suspend = AFTER_SUSPEND_RESTART; in mcp251x_do_set_mode()
813 queue_work(priv->wq, &priv->restart_work); in mcp251x_do_set_mode()
816 return -EOPNOTSUPP; in mcp251x_do_set_mode()
822 static int mcp251x_set_normal_mode(struct spi_device *spi) in mcp251x_set_normal_mode() argument
824 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_set_normal_mode()
829 mcp251x_write_reg(spi, CANINTE, in mcp251x_set_normal_mode()
833 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { in mcp251x_set_normal_mode()
835 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK); in mcp251x_set_normal_mode()
836 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) { in mcp251x_set_normal_mode()
837 /* Put device into listen-only mode */ in mcp251x_set_normal_mode()
838 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY); in mcp251x_set_normal_mode()
841 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL); in mcp251x_set_normal_mode()
844 ret = mcp251x_read_stat_poll_timeout(spi, value, value == 0, in mcp251x_set_normal_mode()
848 dev_err(&spi->dev, "MCP251x didn't enter in normal mode\n"); in mcp251x_set_normal_mode()
852 priv->can.state = CAN_STATE_ERROR_ACTIVE; in mcp251x_set_normal_mode()
859 struct can_bittiming *bt = &priv->can.bittiming; in mcp251x_do_set_bittiming()
860 struct spi_device *spi = priv->spi; in mcp251x_do_set_bittiming() local
862 mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << CNF1_SJW_SHIFT) | in mcp251x_do_set_bittiming()
863 (bt->brp - 1)); in mcp251x_do_set_bittiming()
864 mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE | in mcp251x_do_set_bittiming()
865 (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ? in mcp251x_do_set_bittiming()
867 ((bt->phase_seg1 - 1) << CNF2_PS1_SHIFT) | in mcp251x_do_set_bittiming()
868 (bt->prop_seg - 1)); in mcp251x_do_set_bittiming()
869 mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK, in mcp251x_do_set_bittiming()
870 (bt->phase_seg2 - 1)); in mcp251x_do_set_bittiming()
871 dev_dbg(&spi->dev, "CNF: 0x%02x 0x%02x 0x%02x\n", in mcp251x_do_set_bittiming()
872 mcp251x_read_reg(spi, CNF1), in mcp251x_do_set_bittiming()
873 mcp251x_read_reg(spi, CNF2), in mcp251x_do_set_bittiming()
874 mcp251x_read_reg(spi, CNF3)); in mcp251x_do_set_bittiming()
879 static int mcp251x_setup(struct net_device *net, struct spi_device *spi) in mcp251x_setup() argument
883 mcp251x_write_reg(spi, RXBCTRL(0), in mcp251x_setup()
885 mcp251x_write_reg(spi, RXBCTRL(1), in mcp251x_setup()
890 static int mcp251x_hw_reset(struct spi_device *spi) in mcp251x_hw_reset() argument
892 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_reset()
899 priv->spi_tx_buf[0] = INSTRUCTION_RESET; in mcp251x_hw_reset()
900 ret = mcp251x_spi_write(spi, 1); in mcp251x_hw_reset()
908 ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, in mcp251x_hw_reset()
912 dev_err(&spi->dev, "MCP251x didn't enter in conf mode after reset\n"); in mcp251x_hw_reset()
916 static int mcp251x_hw_probe(struct spi_device *spi) in mcp251x_hw_probe() argument
921 ret = mcp251x_hw_reset(spi); in mcp251x_hw_probe()
925 ctrl = mcp251x_read_reg(spi, CANCTRL); in mcp251x_hw_probe()
927 dev_dbg(&spi->dev, "CANCTRL 0x%02x\n", ctrl); in mcp251x_hw_probe()
931 return -ENODEV; in mcp251x_hw_probe()
950 struct spi_device *spi = priv->spi; in mcp251x_stop() local
954 priv->force_quit = 1; in mcp251x_stop()
955 free_irq(spi->irq, priv); in mcp251x_stop()
957 mutex_lock(&priv->mcp_lock); in mcp251x_stop()
960 mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); in mcp251x_stop()
962 mcp251x_write_reg(spi, TXBCTRL(0), 0); in mcp251x_stop()
965 mcp251x_hw_sleep(spi); in mcp251x_stop()
967 mcp251x_power_enable(priv->transceiver, 0); in mcp251x_stop()
969 priv->can.state = CAN_STATE_STOPPED; in mcp251x_stop()
971 mutex_unlock(&priv->mcp_lock); in mcp251x_stop()
983 frame->can_id |= can_id; in mcp251x_error_skb()
984 frame->data[1] = data1; in mcp251x_error_skb()
995 struct spi_device *spi = priv->spi; in mcp251x_tx_work_handler() local
996 struct net_device *net = priv->net; in mcp251x_tx_work_handler()
999 mutex_lock(&priv->mcp_lock); in mcp251x_tx_work_handler()
1000 if (priv->tx_skb) { in mcp251x_tx_work_handler()
1001 if (priv->can.state == CAN_STATE_BUS_OFF) { in mcp251x_tx_work_handler()
1004 frame = (struct can_frame *)priv->tx_skb->data; in mcp251x_tx_work_handler()
1006 if (frame->len > CAN_FRAME_MAX_DATA_LEN) in mcp251x_tx_work_handler()
1007 frame->len = CAN_FRAME_MAX_DATA_LEN; in mcp251x_tx_work_handler()
1008 mcp251x_hw_tx(spi, frame, 0); in mcp251x_tx_work_handler()
1009 priv->tx_busy = true; in mcp251x_tx_work_handler()
1010 can_put_echo_skb(priv->tx_skb, net, 0, 0); in mcp251x_tx_work_handler()
1011 priv->tx_skb = NULL; in mcp251x_tx_work_handler()
1014 mutex_unlock(&priv->mcp_lock); in mcp251x_tx_work_handler()
1021 struct spi_device *spi = priv->spi; in mcp251x_restart_work_handler() local
1022 struct net_device *net = priv->net; in mcp251x_restart_work_handler()
1024 mutex_lock(&priv->mcp_lock); in mcp251x_restart_work_handler()
1025 if (priv->after_suspend) { in mcp251x_restart_work_handler()
1026 if (priv->after_suspend & AFTER_SUSPEND_POWER) { in mcp251x_restart_work_handler()
1027 mcp251x_hw_reset(spi); in mcp251x_restart_work_handler()
1028 mcp251x_setup(net, spi); in mcp251x_restart_work_handler()
1029 mcp251x_gpio_restore(spi); in mcp251x_restart_work_handler()
1031 mcp251x_hw_wake(spi); in mcp251x_restart_work_handler()
1033 priv->force_quit = 0; in mcp251x_restart_work_handler()
1034 if (priv->after_suspend & AFTER_SUSPEND_RESTART) { in mcp251x_restart_work_handler()
1035 mcp251x_set_normal_mode(spi); in mcp251x_restart_work_handler()
1036 } else if (priv->after_suspend & AFTER_SUSPEND_UP) { in mcp251x_restart_work_handler()
1039 mcp251x_set_normal_mode(spi); in mcp251x_restart_work_handler()
1042 mcp251x_hw_sleep(spi); in mcp251x_restart_work_handler()
1044 priv->after_suspend = 0; in mcp251x_restart_work_handler()
1047 if (priv->restart_tx) { in mcp251x_restart_work_handler()
1048 priv->restart_tx = 0; in mcp251x_restart_work_handler()
1049 mcp251x_write_reg(spi, TXBCTRL(0), 0); in mcp251x_restart_work_handler()
1054 mutex_unlock(&priv->mcp_lock); in mcp251x_restart_work_handler()
1060 struct spi_device *spi = priv->spi; in mcp251x_can_ist() local
1061 struct net_device *net = priv->net; in mcp251x_can_ist()
1063 mutex_lock(&priv->mcp_lock); in mcp251x_can_ist()
1064 while (!priv->force_quit) { in mcp251x_can_ist()
1070 mcp251x_read_2regs(spi, CANINTF, &intf, &eflag); in mcp251x_can_ist()
1074 mcp251x_hw_rx(spi, 0); in mcp251x_can_ist()
1078 if (mcp251x_is_2510(spi)) in mcp251x_can_ist()
1079 mcp251x_write_bits(spi, CANINTF, in mcp251x_can_ist()
1082 /* check if buffer 1 is already known to be full, no need to re-read */ in mcp251x_can_ist()
1087 mcp251x_read_2regs(spi, CANINTF, &intf1, &eflag1); in mcp251x_can_ist()
1097 mcp251x_hw_rx(spi, 1); in mcp251x_can_ist()
1099 if (mcp251x_is_2510(spi)) in mcp251x_can_ist()
1110 mcp251x_write_bits(spi, CANINTF, clear_intf, 0x00); in mcp251x_can_ist()
1113 mcp251x_write_bits(spi, EFLG, eflag, 0x00); in mcp251x_can_ist()
1140 switch (priv->can.state) { in mcp251x_can_ist()
1144 priv->can.can_stats.error_warning++; in mcp251x_can_ist()
1149 priv->can.can_stats.error_passive++; in mcp251x_can_ist()
1154 priv->can.state = new_state; in mcp251x_can_ist()
1160 net->stats.rx_over_errors++; in mcp251x_can_ist()
1161 net->stats.rx_errors++; in mcp251x_can_ist()
1164 net->stats.rx_over_errors++; in mcp251x_can_ist()
1165 net->stats.rx_errors++; in mcp251x_can_ist()
1173 if (priv->can.state == CAN_STATE_BUS_OFF) { in mcp251x_can_ist()
1174 if (priv->can.restart_ms == 0) { in mcp251x_can_ist()
1175 priv->force_quit = 1; in mcp251x_can_ist()
1176 priv->can.can_stats.bus_off++; in mcp251x_can_ist()
1178 mcp251x_hw_sleep(spi); in mcp251x_can_ist()
1187 if (priv->tx_busy) { in mcp251x_can_ist()
1188 net->stats.tx_packets++; in mcp251x_can_ist()
1189 net->stats.tx_bytes += can_get_echo_skb(net, 0, in mcp251x_can_ist()
1191 priv->tx_busy = false; in mcp251x_can_ist()
1196 mutex_unlock(&priv->mcp_lock); in mcp251x_can_ist()
1203 struct spi_device *spi = priv->spi; in mcp251x_open() local
1209 dev_err(&spi->dev, "unable to set initial baudrate!\n"); in mcp251x_open()
1213 mutex_lock(&priv->mcp_lock); in mcp251x_open()
1214 mcp251x_power_enable(priv->transceiver, 1); in mcp251x_open()
1216 priv->force_quit = 0; in mcp251x_open()
1217 priv->tx_skb = NULL; in mcp251x_open()
1218 priv->tx_busy = false; in mcp251x_open()
1220 if (!dev_fwnode(&spi->dev)) in mcp251x_open()
1223 ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist, in mcp251x_open()
1224 flags | IRQF_ONESHOT, dev_name(&spi->dev), in mcp251x_open()
1227 dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq); in mcp251x_open()
1231 ret = mcp251x_hw_wake(spi); in mcp251x_open()
1234 ret = mcp251x_setup(net, spi); in mcp251x_open()
1237 ret = mcp251x_set_normal_mode(spi); in mcp251x_open()
1242 mutex_unlock(&priv->mcp_lock); in mcp251x_open()
1247 free_irq(spi->irq, priv); in mcp251x_open()
1248 mcp251x_hw_sleep(spi); in mcp251x_open()
1250 mcp251x_power_enable(priv->transceiver, 0); in mcp251x_open()
1252 mutex_unlock(&priv->mcp_lock); in mcp251x_open()
1299 MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
1301 static int mcp251x_can_probe(struct spi_device *spi) in mcp251x_can_probe() argument
1309 clk = devm_clk_get_optional(&spi->dev, NULL); in mcp251x_can_probe()
1315 device_property_read_u32(&spi->dev, "clock-frequency", &freq); in mcp251x_can_probe()
1319 return -ERANGE; in mcp251x_can_probe()
1324 return -ENOMEM; in mcp251x_can_probe()
1330 net->netdev_ops = &mcp251x_netdev_ops; in mcp251x_can_probe()
1331 net->ethtool_ops = &mcp251x_ethtool_ops; in mcp251x_can_probe()
1332 net->flags |= IFF_ECHO; in mcp251x_can_probe()
1335 priv->can.bittiming_const = &mcp251x_bittiming_const; in mcp251x_can_probe()
1336 priv->can.do_set_mode = mcp251x_do_set_mode; in mcp251x_can_probe()
1337 priv->can.clock.freq = freq / 2; in mcp251x_can_probe()
1338 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | in mcp251x_can_probe()
1340 priv->model = (enum mcp251x_model)(uintptr_t)spi_get_device_match_data(spi); in mcp251x_can_probe()
1341 priv->net = net; in mcp251x_can_probe()
1342 priv->clk = clk; in mcp251x_can_probe()
1344 spi_set_drvdata(spi, priv); in mcp251x_can_probe()
1346 /* Configure the SPI bus */ in mcp251x_can_probe()
1347 spi->bits_per_word = 8; in mcp251x_can_probe()
1348 if (mcp251x_is_2510(spi)) in mcp251x_can_probe()
1349 spi->max_speed_hz = spi->max_speed_hz ? : 5 * 1000 * 1000; in mcp251x_can_probe()
1351 spi->max_speed_hz = spi->max_speed_hz ? : 10 * 1000 * 1000; in mcp251x_can_probe()
1352 ret = spi_setup(spi); in mcp251x_can_probe()
1356 priv->power = devm_regulator_get_optional(&spi->dev, "vdd"); in mcp251x_can_probe()
1357 priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver"); in mcp251x_can_probe()
1358 if ((PTR_ERR(priv->power) == -EPROBE_DEFER) || in mcp251x_can_probe()
1359 (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) { in mcp251x_can_probe()
1360 ret = -EPROBE_DEFER; in mcp251x_can_probe()
1364 ret = mcp251x_power_enable(priv->power, 1); in mcp251x_can_probe()
1368 priv->wq = alloc_workqueue("mcp251x_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM, in mcp251x_can_probe()
1370 if (!priv->wq) { in mcp251x_can_probe()
1371 ret = -ENOMEM; in mcp251x_can_probe()
1374 INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler); in mcp251x_can_probe()
1375 INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler); in mcp251x_can_probe()
1377 priv->spi = spi; in mcp251x_can_probe()
1378 mutex_init(&priv->mcp_lock); in mcp251x_can_probe()
1380 priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, in mcp251x_can_probe()
1382 if (!priv->spi_tx_buf) { in mcp251x_can_probe()
1383 ret = -ENOMEM; in mcp251x_can_probe()
1387 priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, in mcp251x_can_probe()
1389 if (!priv->spi_rx_buf) { in mcp251x_can_probe()
1390 ret = -ENOMEM; in mcp251x_can_probe()
1394 SET_NETDEV_DEV(net, &spi->dev); in mcp251x_can_probe()
1397 ret = mcp251x_hw_probe(spi); in mcp251x_can_probe()
1399 if (ret == -ENODEV) in mcp251x_can_probe()
1400 dev_err(&spi->dev, "Cannot initialize MCP%x. Wrong wiring?\n", in mcp251x_can_probe()
1401 priv->model); in mcp251x_can_probe()
1405 mcp251x_hw_sleep(spi); in mcp251x_can_probe()
1415 netdev_info(net, "MCP%x successfully initialized.\n", priv->model); in mcp251x_can_probe()
1422 destroy_workqueue(priv->wq); in mcp251x_can_probe()
1423 priv->wq = NULL; in mcp251x_can_probe()
1424 mcp251x_power_enable(priv->power, 0); in mcp251x_can_probe()
1432 dev_err(&spi->dev, "Probe failed, err=%d\n", -ret); in mcp251x_can_probe()
1436 static void mcp251x_can_remove(struct spi_device *spi) in mcp251x_can_remove() argument
1438 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_remove()
1439 struct net_device *net = priv->net; in mcp251x_can_remove()
1443 mcp251x_power_enable(priv->power, 0); in mcp251x_can_remove()
1445 destroy_workqueue(priv->wq); in mcp251x_can_remove()
1446 priv->wq = NULL; in mcp251x_can_remove()
1448 clk_disable_unprepare(priv->clk); in mcp251x_can_remove()
1455 struct spi_device *spi = to_spi_device(dev); in mcp251x_can_suspend() local
1456 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_suspend()
1457 struct net_device *net = priv->net; in mcp251x_can_suspend()
1459 priv->force_quit = 1; in mcp251x_can_suspend()
1460 disable_irq(spi->irq); in mcp251x_can_suspend()
1467 mcp251x_hw_sleep(spi); in mcp251x_can_suspend()
1468 mcp251x_power_enable(priv->transceiver, 0); in mcp251x_can_suspend()
1469 priv->after_suspend = AFTER_SUSPEND_UP; in mcp251x_can_suspend()
1471 priv->after_suspend = AFTER_SUSPEND_DOWN; in mcp251x_can_suspend()
1474 mcp251x_power_enable(priv->power, 0); in mcp251x_can_suspend()
1475 priv->after_suspend |= AFTER_SUSPEND_POWER; in mcp251x_can_suspend()
1482 struct spi_device *spi = to_spi_device(dev); in mcp251x_can_resume() local
1483 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_resume()
1485 if (priv->after_suspend & AFTER_SUSPEND_POWER) in mcp251x_can_resume()
1486 mcp251x_power_enable(priv->power, 1); in mcp251x_can_resume()
1487 if (priv->after_suspend & AFTER_SUSPEND_UP) in mcp251x_can_resume()
1488 mcp251x_power_enable(priv->transceiver, 1); in mcp251x_can_resume()
1490 if (priv->after_suspend & (AFTER_SUSPEND_POWER | AFTER_SUSPEND_UP)) in mcp251x_can_resume()
1491 queue_work(priv->wq, &priv->restart_work); in mcp251x_can_resume()
1493 priv->after_suspend = 0; in mcp251x_can_resume()
1495 priv->force_quit = 0; in mcp251x_can_resume()
1496 enable_irq(spi->irq); in mcp251x_can_resume()