Lines Matching full:spi
2 /* CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface
43 #include <linux/spi/spi.h>
46 /* SPI interface instruction set */
197 /* Buffer size required for the largest SPI transfer (i.e., reading a
231 struct spi_device *spi; member
234 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); \
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()
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()
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()
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()
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()
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()
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()
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()
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()
453 mcp251x_write_bits(priv->spi, BFPCTRL, val, val); in mcp251x_gpio_request()
474 mcp251x_write_bits(priv->spi, BFPCTRL, val, 0); in mcp251x_gpio_free()
503 val = mcp251x_read_reg(priv->spi, reg); in mcp251x_gpio_get()
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()
543 mcp251x_write_bits(priv->spi, BFPCTRL, mask, val); in mcp251x_gpio_set()
567 mcp251x_write_bits(priv->spi, BFPCTRL, mask, 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()
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()
604 gpio->of_node = priv->spi->dev.of_node; in mcp251x_gpio_setup()
607 return devm_gpiochip_add_data(&priv->spi->dev, gpio, priv); in mcp251x_gpio_setup()
610 static inline void mcp251x_gpio_restore(struct spi_device *spi) in mcp251x_gpio_restore() argument
620 static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, in mcp251x_hw_tx_frame() argument
623 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_tx_frame()
625 if (mcp251x_is_2510(spi)) { in mcp251x_hw_tx_frame()
629 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i, in mcp251x_hw_tx_frame()
633 mcp251x_spi_write(spi, TXBDAT_OFF + len); in mcp251x_hw_tx_frame()
637 static void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame, in mcp251x_hw_tx() argument
640 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_tx()
661 mcp251x_hw_tx_frame(spi, buf, frame->can_dlc, tx_buf_idx); in mcp251x_hw_tx()
665 mcp251x_spi_write(priv->spi, 1); in mcp251x_hw_tx()
668 static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf, in mcp251x_hw_rx_frame() argument
671 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_rx_frame()
673 if (mcp251x_is_2510(spi)) { in mcp251x_hw_rx_frame()
677 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); in mcp251x_hw_rx_frame()
681 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); in mcp251x_hw_rx_frame()
684 if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) { in mcp251x_hw_rx_frame()
685 spi_write_then_read(spi, priv->spi_tx_buf, 1, in mcp251x_hw_rx_frame()
691 mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); in mcp251x_hw_rx_frame()
697 static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx) in mcp251x_hw_rx() argument
699 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_rx()
706 dev_err(&spi->dev, "cannot allocate RX skb\n"); in mcp251x_hw_rx()
711 mcp251x_hw_rx_frame(spi, buf, buf_idx); in mcp251x_hw_rx()
746 static void mcp251x_hw_sleep(struct spi_device *spi) in mcp251x_hw_sleep() argument
748 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP); in mcp251x_hw_sleep()
752 static int mcp251x_hw_wake(struct spi_device *spi) in mcp251x_hw_wake() argument
758 disable_irq(spi->irq); in mcp251x_hw_wake()
759 mcp251x_write_2regs(spi, CANINTE, CANINTE_WAKIE, CANINTF_WAKIF); in mcp251x_hw_wake()
765 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_CONF); in mcp251x_hw_wake()
768 ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, in mcp251x_hw_wake()
772 dev_err(&spi->dev, "MCP251x didn't enter in config mode\n"); in mcp251x_hw_wake()
777 mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); in mcp251x_hw_wake()
778 enable_irq(spi->irq); in mcp251x_hw_wake()
787 struct spi_device *spi = priv->spi; in mcp251x_hard_start_xmit() local
790 dev_warn(&spi->dev, "hard_xmit called while tx busy\n"); in mcp251x_hard_start_xmit()
811 /* We have to delay work since SPI I/O may sleep */ in mcp251x_do_set_mode()
825 static int mcp251x_set_normal_mode(struct spi_device *spi) in mcp251x_set_normal_mode() argument
827 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_set_normal_mode()
832 mcp251x_write_reg(spi, CANINTE, in mcp251x_set_normal_mode()
838 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK); in mcp251x_set_normal_mode()
841 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY); in mcp251x_set_normal_mode()
844 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL); in mcp251x_set_normal_mode()
847 ret = mcp251x_read_stat_poll_timeout(spi, value, value == 0, in mcp251x_set_normal_mode()
851 dev_err(&spi->dev, "MCP251x didn't enter in normal mode\n"); in mcp251x_set_normal_mode()
863 struct spi_device *spi = priv->spi; in mcp251x_do_set_bittiming() local
865 mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << CNF1_SJW_SHIFT) | in mcp251x_do_set_bittiming()
867 mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE | in mcp251x_do_set_bittiming()
872 mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK, in mcp251x_do_set_bittiming()
874 dev_dbg(&spi->dev, "CNF: 0x%02x 0x%02x 0x%02x\n", in mcp251x_do_set_bittiming()
875 mcp251x_read_reg(spi, CNF1), in mcp251x_do_set_bittiming()
876 mcp251x_read_reg(spi, CNF2), in mcp251x_do_set_bittiming()
877 mcp251x_read_reg(spi, CNF3)); in mcp251x_do_set_bittiming()
882 static int mcp251x_setup(struct net_device *net, struct spi_device *spi) in mcp251x_setup() argument
886 mcp251x_write_reg(spi, RXBCTRL(0), in mcp251x_setup()
888 mcp251x_write_reg(spi, RXBCTRL(1), in mcp251x_setup()
893 static int mcp251x_hw_reset(struct spi_device *spi) in mcp251x_hw_reset() argument
895 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_hw_reset()
903 ret = mcp251x_spi_write(spi, 1); in mcp251x_hw_reset()
911 ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF, in mcp251x_hw_reset()
915 dev_err(&spi->dev, "MCP251x didn't enter in conf mode after reset\n"); in mcp251x_hw_reset()
919 static int mcp251x_hw_probe(struct spi_device *spi) in mcp251x_hw_probe() argument
924 ret = mcp251x_hw_reset(spi); in mcp251x_hw_probe()
928 ctrl = mcp251x_read_reg(spi, CANCTRL); in mcp251x_hw_probe()
930 dev_dbg(&spi->dev, "CANCTRL 0x%02x\n", ctrl); in mcp251x_hw_probe()
953 struct spi_device *spi = priv->spi; in mcp251x_stop() local
958 free_irq(spi->irq, priv); in mcp251x_stop()
963 mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00); in mcp251x_stop()
965 mcp251x_write_reg(spi, TXBCTRL(0), 0); in mcp251x_stop()
968 mcp251x_hw_sleep(spi); in mcp251x_stop()
1000 struct spi_device *spi = priv->spi; in mcp251x_tx_work_handler() local
1013 mcp251x_hw_tx(spi, frame, 0); in mcp251x_tx_work_handler()
1026 struct spi_device *spi = priv->spi; in mcp251x_restart_work_handler() local
1032 mcp251x_hw_reset(spi); in mcp251x_restart_work_handler()
1033 mcp251x_setup(net, spi); in mcp251x_restart_work_handler()
1034 mcp251x_gpio_restore(spi); in mcp251x_restart_work_handler()
1036 mcp251x_hw_wake(spi); in mcp251x_restart_work_handler()
1040 mcp251x_set_normal_mode(spi); in mcp251x_restart_work_handler()
1044 mcp251x_set_normal_mode(spi); in mcp251x_restart_work_handler()
1047 mcp251x_hw_sleep(spi); in mcp251x_restart_work_handler()
1054 mcp251x_write_reg(spi, TXBCTRL(0), 0); in mcp251x_restart_work_handler()
1065 struct spi_device *spi = priv->spi; in mcp251x_can_ist() local
1075 mcp251x_read_2regs(spi, CANINTF, &intf, &eflag); in mcp251x_can_ist()
1082 mcp251x_hw_rx(spi, 0); in mcp251x_can_ist()
1086 if (mcp251x_is_2510(spi)) in mcp251x_can_ist()
1087 mcp251x_write_bits(spi, CANINTF, in mcp251x_can_ist()
1093 mcp251x_hw_rx(spi, 1); in mcp251x_can_ist()
1095 if (mcp251x_is_2510(spi)) in mcp251x_can_ist()
1103 mcp251x_write_bits(spi, CANINTF, clear_intf, 0x00); in mcp251x_can_ist()
1106 mcp251x_write_bits(spi, EFLG, eflag, 0x00); in mcp251x_can_ist()
1171 mcp251x_hw_sleep(spi); in mcp251x_can_ist()
1197 struct spi_device *spi = priv->spi; in mcp251x_open() local
1203 dev_err(&spi->dev, "unable to set initial baudrate!\n"); in mcp251x_open()
1214 if (!dev_fwnode(&spi->dev)) in mcp251x_open()
1217 ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist, in mcp251x_open()
1218 flags | IRQF_ONESHOT, dev_name(&spi->dev), in mcp251x_open()
1221 dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq); in mcp251x_open()
1225 ret = mcp251x_hw_wake(spi); in mcp251x_open()
1228 ret = mcp251x_setup(net, spi); in mcp251x_open()
1231 ret = mcp251x_set_normal_mode(spi); in mcp251x_open()
1243 free_irq(spi->irq, priv); in mcp251x_open()
1244 mcp251x_hw_sleep(spi); in mcp251x_open()
1291 MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
1293 static int mcp251x_can_probe(struct spi_device *spi) in mcp251x_can_probe() argument
1295 const void *match = device_get_match_data(&spi->dev); in mcp251x_can_probe()
1302 clk = devm_clk_get_optional(&spi->dev, NULL); in mcp251x_can_probe()
1308 device_property_read_u32(&spi->dev, "clock-frequency", &freq); in mcp251x_can_probe()
1335 priv->model = spi_get_device_id(spi)->driver_data; in mcp251x_can_probe()
1339 spi_set_drvdata(spi, priv); in mcp251x_can_probe()
1341 /* Configure the SPI bus */ in mcp251x_can_probe()
1342 spi->bits_per_word = 8; in mcp251x_can_probe()
1343 if (mcp251x_is_2510(spi)) in mcp251x_can_probe()
1344 spi->max_speed_hz = spi->max_speed_hz ? : 5 * 1000 * 1000; in mcp251x_can_probe()
1346 spi->max_speed_hz = spi->max_speed_hz ? : 10 * 1000 * 1000; in mcp251x_can_probe()
1347 ret = spi_setup(spi); in mcp251x_can_probe()
1351 priv->power = devm_regulator_get_optional(&spi->dev, "vdd"); in mcp251x_can_probe()
1352 priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver"); in mcp251x_can_probe()
1372 priv->spi = spi; in mcp251x_can_probe()
1375 priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, in mcp251x_can_probe()
1382 priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN, in mcp251x_can_probe()
1389 SET_NETDEV_DEV(net, &spi->dev); in mcp251x_can_probe()
1392 ret = mcp251x_hw_probe(spi); in mcp251x_can_probe()
1395 dev_err(&spi->dev, "Cannot initialize MCP%x. Wrong wiring?\n", in mcp251x_can_probe()
1400 mcp251x_hw_sleep(spi); in mcp251x_can_probe()
1426 dev_err(&spi->dev, "Probe failed, err=%d\n", -ret); in mcp251x_can_probe()
1430 static int mcp251x_can_remove(struct spi_device *spi) in mcp251x_can_remove() argument
1432 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_remove()
1451 struct spi_device *spi = to_spi_device(dev); in mcp251x_can_suspend() local
1452 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_suspend()
1456 disable_irq(spi->irq); in mcp251x_can_suspend()
1463 mcp251x_hw_sleep(spi); in mcp251x_can_suspend()
1478 struct spi_device *spi = to_spi_device(dev); in mcp251x_can_resume() local
1479 struct mcp251x_priv *priv = spi_get_drvdata(spi); in mcp251x_can_resume()
1492 enable_irq(spi->irq); in mcp251x_can_resume()