18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* CAN bus driver for Microchip 251x/25625 CAN Controller with SPI Interface
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * MCP2510 support and bug fixes by Christian Pellegrin
58c2ecf20Sopenharmony_ci * <chripell@evolware.org>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright 2009 Christian Pellegrin EVOL S.r.l.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright 2007 Raymarine UK, Ltd. All Rights Reserved.
108c2ecf20Sopenharmony_ci * Written under contract by:
118c2ecf20Sopenharmony_ci *   Chris Elston, Katalix Systems, Ltd.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Based on Microchip MCP251x CAN controller driver written by
148c2ecf20Sopenharmony_ci * David Vrabel, Copyright 2006 Arcom Control Systems Ltd.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Based on CAN bus driver for the CCAN controller written by
178c2ecf20Sopenharmony_ci * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
188c2ecf20Sopenharmony_ci * - Simon Kallweit, intefo AG
198c2ecf20Sopenharmony_ci * Copyright 2007
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
238c2ecf20Sopenharmony_ci#include <linux/can/core.h>
248c2ecf20Sopenharmony_ci#include <linux/can/dev.h>
258c2ecf20Sopenharmony_ci#include <linux/can/led.h>
268c2ecf20Sopenharmony_ci#include <linux/clk.h>
278c2ecf20Sopenharmony_ci#include <linux/completion.h>
288c2ecf20Sopenharmony_ci#include <linux/delay.h>
298c2ecf20Sopenharmony_ci#include <linux/device.h>
308c2ecf20Sopenharmony_ci#include <linux/freezer.h>
318c2ecf20Sopenharmony_ci#include <linux/gpio.h>
328c2ecf20Sopenharmony_ci#include <linux/gpio/driver.h>
338c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
348c2ecf20Sopenharmony_ci#include <linux/io.h>
358c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
368c2ecf20Sopenharmony_ci#include <linux/kernel.h>
378c2ecf20Sopenharmony_ci#include <linux/module.h>
388c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
398c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
408c2ecf20Sopenharmony_ci#include <linux/property.h>
418c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
428c2ecf20Sopenharmony_ci#include <linux/slab.h>
438c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
448c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* SPI interface instruction set */
478c2ecf20Sopenharmony_ci#define INSTRUCTION_WRITE	0x02
488c2ecf20Sopenharmony_ci#define INSTRUCTION_READ	0x03
498c2ecf20Sopenharmony_ci#define INSTRUCTION_BIT_MODIFY	0x05
508c2ecf20Sopenharmony_ci#define INSTRUCTION_LOAD_TXB(n)	(0x40 + 2 * (n))
518c2ecf20Sopenharmony_ci#define INSTRUCTION_READ_RXB(n)	(((n) == 0) ? 0x90 : 0x94)
528c2ecf20Sopenharmony_ci#define INSTRUCTION_RESET	0xC0
538c2ecf20Sopenharmony_ci#define RTS_TXB0		0x01
548c2ecf20Sopenharmony_ci#define RTS_TXB1		0x02
558c2ecf20Sopenharmony_ci#define RTS_TXB2		0x04
568c2ecf20Sopenharmony_ci#define INSTRUCTION_RTS(n)	(0x80 | ((n) & 0x07))
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* MPC251x registers */
598c2ecf20Sopenharmony_ci#define BFPCTRL			0x0c
608c2ecf20Sopenharmony_ci#  define BFPCTRL_B0BFM		BIT(0)
618c2ecf20Sopenharmony_ci#  define BFPCTRL_B1BFM		BIT(1)
628c2ecf20Sopenharmony_ci#  define BFPCTRL_BFM(n)	(BFPCTRL_B0BFM << (n))
638c2ecf20Sopenharmony_ci#  define BFPCTRL_BFM_MASK	GENMASK(1, 0)
648c2ecf20Sopenharmony_ci#  define BFPCTRL_B0BFE		BIT(2)
658c2ecf20Sopenharmony_ci#  define BFPCTRL_B1BFE		BIT(3)
668c2ecf20Sopenharmony_ci#  define BFPCTRL_BFE(n)	(BFPCTRL_B0BFE << (n))
678c2ecf20Sopenharmony_ci#  define BFPCTRL_BFE_MASK	GENMASK(3, 2)
688c2ecf20Sopenharmony_ci#  define BFPCTRL_B0BFS		BIT(4)
698c2ecf20Sopenharmony_ci#  define BFPCTRL_B1BFS		BIT(5)
708c2ecf20Sopenharmony_ci#  define BFPCTRL_BFS(n)	(BFPCTRL_B0BFS << (n))
718c2ecf20Sopenharmony_ci#  define BFPCTRL_BFS_MASK	GENMASK(5, 4)
728c2ecf20Sopenharmony_ci#define TXRTSCTRL		0x0d
738c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B0RTSM	BIT(0)
748c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B1RTSM	BIT(1)
758c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B2RTSM	BIT(2)
768c2ecf20Sopenharmony_ci#  define TXRTSCTRL_RTSM(n)	(TXRTSCTRL_B0RTSM << (n))
778c2ecf20Sopenharmony_ci#  define TXRTSCTRL_RTSM_MASK	GENMASK(2, 0)
788c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B0RTS	BIT(3)
798c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B1RTS	BIT(4)
808c2ecf20Sopenharmony_ci#  define TXRTSCTRL_B2RTS	BIT(5)
818c2ecf20Sopenharmony_ci#  define TXRTSCTRL_RTS(n)	(TXRTSCTRL_B0RTS << (n))
828c2ecf20Sopenharmony_ci#  define TXRTSCTRL_RTS_MASK	GENMASK(5, 3)
838c2ecf20Sopenharmony_ci#define CANSTAT	      0x0e
848c2ecf20Sopenharmony_ci#define CANCTRL	      0x0f
858c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_MASK	    0xe0
868c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_CONF	    0x80
878c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_LISTEN_ONLY 0x60
888c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_LOOPBACK    0x40
898c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_SLEEP	    0x20
908c2ecf20Sopenharmony_ci#  define CANCTRL_REQOP_NORMAL	    0x00
918c2ecf20Sopenharmony_ci#  define CANCTRL_OSM		    0x08
928c2ecf20Sopenharmony_ci#  define CANCTRL_ABAT		    0x10
938c2ecf20Sopenharmony_ci#define TEC	      0x1c
948c2ecf20Sopenharmony_ci#define REC	      0x1d
958c2ecf20Sopenharmony_ci#define CNF1	      0x2a
968c2ecf20Sopenharmony_ci#  define CNF1_SJW_SHIFT   6
978c2ecf20Sopenharmony_ci#define CNF2	      0x29
988c2ecf20Sopenharmony_ci#  define CNF2_BTLMODE	   0x80
998c2ecf20Sopenharmony_ci#  define CNF2_SAM         0x40
1008c2ecf20Sopenharmony_ci#  define CNF2_PS1_SHIFT   3
1018c2ecf20Sopenharmony_ci#define CNF3	      0x28
1028c2ecf20Sopenharmony_ci#  define CNF3_SOF	   0x08
1038c2ecf20Sopenharmony_ci#  define CNF3_WAKFIL	   0x04
1048c2ecf20Sopenharmony_ci#  define CNF3_PHSEG2_MASK 0x07
1058c2ecf20Sopenharmony_ci#define CANINTE	      0x2b
1068c2ecf20Sopenharmony_ci#  define CANINTE_MERRE 0x80
1078c2ecf20Sopenharmony_ci#  define CANINTE_WAKIE 0x40
1088c2ecf20Sopenharmony_ci#  define CANINTE_ERRIE 0x20
1098c2ecf20Sopenharmony_ci#  define CANINTE_TX2IE 0x10
1108c2ecf20Sopenharmony_ci#  define CANINTE_TX1IE 0x08
1118c2ecf20Sopenharmony_ci#  define CANINTE_TX0IE 0x04
1128c2ecf20Sopenharmony_ci#  define CANINTE_RX1IE 0x02
1138c2ecf20Sopenharmony_ci#  define CANINTE_RX0IE 0x01
1148c2ecf20Sopenharmony_ci#define CANINTF	      0x2c
1158c2ecf20Sopenharmony_ci#  define CANINTF_MERRF 0x80
1168c2ecf20Sopenharmony_ci#  define CANINTF_WAKIF 0x40
1178c2ecf20Sopenharmony_ci#  define CANINTF_ERRIF 0x20
1188c2ecf20Sopenharmony_ci#  define CANINTF_TX2IF 0x10
1198c2ecf20Sopenharmony_ci#  define CANINTF_TX1IF 0x08
1208c2ecf20Sopenharmony_ci#  define CANINTF_TX0IF 0x04
1218c2ecf20Sopenharmony_ci#  define CANINTF_RX1IF 0x02
1228c2ecf20Sopenharmony_ci#  define CANINTF_RX0IF 0x01
1238c2ecf20Sopenharmony_ci#  define CANINTF_RX (CANINTF_RX0IF | CANINTF_RX1IF)
1248c2ecf20Sopenharmony_ci#  define CANINTF_TX (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)
1258c2ecf20Sopenharmony_ci#  define CANINTF_ERR (CANINTF_ERRIF)
1268c2ecf20Sopenharmony_ci#define EFLG	      0x2d
1278c2ecf20Sopenharmony_ci#  define EFLG_EWARN	0x01
1288c2ecf20Sopenharmony_ci#  define EFLG_RXWAR	0x02
1298c2ecf20Sopenharmony_ci#  define EFLG_TXWAR	0x04
1308c2ecf20Sopenharmony_ci#  define EFLG_RXEP	0x08
1318c2ecf20Sopenharmony_ci#  define EFLG_TXEP	0x10
1328c2ecf20Sopenharmony_ci#  define EFLG_TXBO	0x20
1338c2ecf20Sopenharmony_ci#  define EFLG_RX0OVR	0x40
1348c2ecf20Sopenharmony_ci#  define EFLG_RX1OVR	0x80
1358c2ecf20Sopenharmony_ci#define TXBCTRL(n)  (((n) * 0x10) + 0x30 + TXBCTRL_OFF)
1368c2ecf20Sopenharmony_ci#  define TXBCTRL_ABTF	0x40
1378c2ecf20Sopenharmony_ci#  define TXBCTRL_MLOA	0x20
1388c2ecf20Sopenharmony_ci#  define TXBCTRL_TXERR 0x10
1398c2ecf20Sopenharmony_ci#  define TXBCTRL_TXREQ 0x08
1408c2ecf20Sopenharmony_ci#define TXBSIDH(n)  (((n) * 0x10) + 0x30 + TXBSIDH_OFF)
1418c2ecf20Sopenharmony_ci#  define SIDH_SHIFT    3
1428c2ecf20Sopenharmony_ci#define TXBSIDL(n)  (((n) * 0x10) + 0x30 + TXBSIDL_OFF)
1438c2ecf20Sopenharmony_ci#  define SIDL_SID_MASK    7
1448c2ecf20Sopenharmony_ci#  define SIDL_SID_SHIFT   5
1458c2ecf20Sopenharmony_ci#  define SIDL_EXIDE_SHIFT 3
1468c2ecf20Sopenharmony_ci#  define SIDL_EID_SHIFT   16
1478c2ecf20Sopenharmony_ci#  define SIDL_EID_MASK    3
1488c2ecf20Sopenharmony_ci#define TXBEID8(n)  (((n) * 0x10) + 0x30 + TXBEID8_OFF)
1498c2ecf20Sopenharmony_ci#define TXBEID0(n)  (((n) * 0x10) + 0x30 + TXBEID0_OFF)
1508c2ecf20Sopenharmony_ci#define TXBDLC(n)   (((n) * 0x10) + 0x30 + TXBDLC_OFF)
1518c2ecf20Sopenharmony_ci#  define DLC_RTR_SHIFT    6
1528c2ecf20Sopenharmony_ci#define TXBCTRL_OFF 0
1538c2ecf20Sopenharmony_ci#define TXBSIDH_OFF 1
1548c2ecf20Sopenharmony_ci#define TXBSIDL_OFF 2
1558c2ecf20Sopenharmony_ci#define TXBEID8_OFF 3
1568c2ecf20Sopenharmony_ci#define TXBEID0_OFF 4
1578c2ecf20Sopenharmony_ci#define TXBDLC_OFF  5
1588c2ecf20Sopenharmony_ci#define TXBDAT_OFF  6
1598c2ecf20Sopenharmony_ci#define RXBCTRL(n)  (((n) * 0x10) + 0x60 + RXBCTRL_OFF)
1608c2ecf20Sopenharmony_ci#  define RXBCTRL_BUKT	0x04
1618c2ecf20Sopenharmony_ci#  define RXBCTRL_RXM0	0x20
1628c2ecf20Sopenharmony_ci#  define RXBCTRL_RXM1	0x40
1638c2ecf20Sopenharmony_ci#define RXBSIDH(n)  (((n) * 0x10) + 0x60 + RXBSIDH_OFF)
1648c2ecf20Sopenharmony_ci#  define RXBSIDH_SHIFT 3
1658c2ecf20Sopenharmony_ci#define RXBSIDL(n)  (((n) * 0x10) + 0x60 + RXBSIDL_OFF)
1668c2ecf20Sopenharmony_ci#  define RXBSIDL_IDE   0x08
1678c2ecf20Sopenharmony_ci#  define RXBSIDL_SRR   0x10
1688c2ecf20Sopenharmony_ci#  define RXBSIDL_EID   3
1698c2ecf20Sopenharmony_ci#  define RXBSIDL_SHIFT 5
1708c2ecf20Sopenharmony_ci#define RXBEID8(n)  (((n) * 0x10) + 0x60 + RXBEID8_OFF)
1718c2ecf20Sopenharmony_ci#define RXBEID0(n)  (((n) * 0x10) + 0x60 + RXBEID0_OFF)
1728c2ecf20Sopenharmony_ci#define RXBDLC(n)   (((n) * 0x10) + 0x60 + RXBDLC_OFF)
1738c2ecf20Sopenharmony_ci#  define RXBDLC_LEN_MASK  0x0f
1748c2ecf20Sopenharmony_ci#  define RXBDLC_RTR       0x40
1758c2ecf20Sopenharmony_ci#define RXBCTRL_OFF 0
1768c2ecf20Sopenharmony_ci#define RXBSIDH_OFF 1
1778c2ecf20Sopenharmony_ci#define RXBSIDL_OFF 2
1788c2ecf20Sopenharmony_ci#define RXBEID8_OFF 3
1798c2ecf20Sopenharmony_ci#define RXBEID0_OFF 4
1808c2ecf20Sopenharmony_ci#define RXBDLC_OFF  5
1818c2ecf20Sopenharmony_ci#define RXBDAT_OFF  6
1828c2ecf20Sopenharmony_ci#define RXFSID(n) ((n < 3) ? 0 : 4)
1838c2ecf20Sopenharmony_ci#define RXFSIDH(n) ((n) * 4 + RXFSID(n))
1848c2ecf20Sopenharmony_ci#define RXFSIDL(n) ((n) * 4 + 1 + RXFSID(n))
1858c2ecf20Sopenharmony_ci#define RXFEID8(n) ((n) * 4 + 2 + RXFSID(n))
1868c2ecf20Sopenharmony_ci#define RXFEID0(n) ((n) * 4 + 3 + RXFSID(n))
1878c2ecf20Sopenharmony_ci#define RXMSIDH(n) ((n) * 4 + 0x20)
1888c2ecf20Sopenharmony_ci#define RXMSIDL(n) ((n) * 4 + 0x21)
1898c2ecf20Sopenharmony_ci#define RXMEID8(n) ((n) * 4 + 0x22)
1908c2ecf20Sopenharmony_ci#define RXMEID0(n) ((n) * 4 + 0x23)
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci#define GET_BYTE(val, byte)			\
1938c2ecf20Sopenharmony_ci	(((val) >> ((byte) * 8)) & 0xff)
1948c2ecf20Sopenharmony_ci#define SET_BYTE(val, byte)			\
1958c2ecf20Sopenharmony_ci	(((val) & 0xff) << ((byte) * 8))
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/* Buffer size required for the largest SPI transfer (i.e., reading a
1988c2ecf20Sopenharmony_ci * frame)
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ci#define CAN_FRAME_MAX_DATA_LEN	8
2018c2ecf20Sopenharmony_ci#define SPI_TRANSFER_BUF_LEN	(6 + CAN_FRAME_MAX_DATA_LEN)
2028c2ecf20Sopenharmony_ci#define CAN_FRAME_MAX_BITS	128
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define TX_ECHO_SKB_MAX	1
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#define MCP251X_OST_DELAY_MS	(5)
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#define DEVICE_NAME "mcp251x"
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic const struct can_bittiming_const mcp251x_bittiming_const = {
2118c2ecf20Sopenharmony_ci	.name = DEVICE_NAME,
2128c2ecf20Sopenharmony_ci	.tseg1_min = 3,
2138c2ecf20Sopenharmony_ci	.tseg1_max = 16,
2148c2ecf20Sopenharmony_ci	.tseg2_min = 2,
2158c2ecf20Sopenharmony_ci	.tseg2_max = 8,
2168c2ecf20Sopenharmony_ci	.sjw_max = 4,
2178c2ecf20Sopenharmony_ci	.brp_min = 1,
2188c2ecf20Sopenharmony_ci	.brp_max = 64,
2198c2ecf20Sopenharmony_ci	.brp_inc = 1,
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cienum mcp251x_model {
2238c2ecf20Sopenharmony_ci	CAN_MCP251X_MCP2510	= 0x2510,
2248c2ecf20Sopenharmony_ci	CAN_MCP251X_MCP2515	= 0x2515,
2258c2ecf20Sopenharmony_ci	CAN_MCP251X_MCP25625	= 0x25625,
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistruct mcp251x_priv {
2298c2ecf20Sopenharmony_ci	struct can_priv	   can;
2308c2ecf20Sopenharmony_ci	struct net_device *net;
2318c2ecf20Sopenharmony_ci	struct spi_device *spi;
2328c2ecf20Sopenharmony_ci	enum mcp251x_model model;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	struct mutex mcp_lock; /* SPI device lock */
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	u8 *spi_tx_buf;
2378c2ecf20Sopenharmony_ci	u8 *spi_rx_buf;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb;
2408c2ecf20Sopenharmony_ci	int tx_len;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	struct workqueue_struct *wq;
2438c2ecf20Sopenharmony_ci	struct work_struct tx_work;
2448c2ecf20Sopenharmony_ci	struct work_struct restart_work;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	int force_quit;
2478c2ecf20Sopenharmony_ci	int after_suspend;
2488c2ecf20Sopenharmony_ci#define AFTER_SUSPEND_UP 1
2498c2ecf20Sopenharmony_ci#define AFTER_SUSPEND_DOWN 2
2508c2ecf20Sopenharmony_ci#define AFTER_SUSPEND_POWER 4
2518c2ecf20Sopenharmony_ci#define AFTER_SUSPEND_RESTART 8
2528c2ecf20Sopenharmony_ci	int restart_tx;
2538c2ecf20Sopenharmony_ci	struct regulator *power;
2548c2ecf20Sopenharmony_ci	struct regulator *transceiver;
2558c2ecf20Sopenharmony_ci	struct clk *clk;
2568c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
2578c2ecf20Sopenharmony_ci	struct gpio_chip gpio;
2588c2ecf20Sopenharmony_ci	u8 reg_bfpctrl;
2598c2ecf20Sopenharmony_ci#endif
2608c2ecf20Sopenharmony_ci};
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci#define MCP251X_IS(_model) \
2638c2ecf20Sopenharmony_cistatic inline int mcp251x_is_##_model(struct spi_device *spi) \
2648c2ecf20Sopenharmony_ci{ \
2658c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi); \
2668c2ecf20Sopenharmony_ci	return priv->model == CAN_MCP251X_MCP##_model; \
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ciMCP251X_IS(2510);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic void mcp251x_clean(struct net_device *net)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (priv->tx_skb || priv->tx_len)
2768c2ecf20Sopenharmony_ci		net->stats.tx_errors++;
2778c2ecf20Sopenharmony_ci	dev_kfree_skb(priv->tx_skb);
2788c2ecf20Sopenharmony_ci	if (priv->tx_len)
2798c2ecf20Sopenharmony_ci		can_free_echo_skb(priv->net, 0);
2808c2ecf20Sopenharmony_ci	priv->tx_skb = NULL;
2818c2ecf20Sopenharmony_ci	priv->tx_len = 0;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* Note about handling of error return of mcp251x_spi_trans: accessing
2858c2ecf20Sopenharmony_ci * registers via SPI is not really different conceptually than using
2868c2ecf20Sopenharmony_ci * normal I/O assembler instructions, although it's much more
2878c2ecf20Sopenharmony_ci * complicated from a practical POV. So it's not advisable to always
2888c2ecf20Sopenharmony_ci * check the return value of this function. Imagine that every
2898c2ecf20Sopenharmony_ci * read{b,l}, write{b,l} and friends would be bracketed in "if ( < 0)
2908c2ecf20Sopenharmony_ci * error();", it would be a great mess (well there are some situation
2918c2ecf20Sopenharmony_ci * when exception handling C++ like could be useful after all). So we
2928c2ecf20Sopenharmony_ci * just check that transfers are OK at the beginning of our
2938c2ecf20Sopenharmony_ci * conversation with the chip and to avoid doing really nasty things
2948c2ecf20Sopenharmony_ci * (like injecting bogus packets in the network stack).
2958c2ecf20Sopenharmony_ci */
2968c2ecf20Sopenharmony_cistatic int mcp251x_spi_trans(struct spi_device *spi, int len)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
2998c2ecf20Sopenharmony_ci	struct spi_transfer t = {
3008c2ecf20Sopenharmony_ci		.tx_buf = priv->spi_tx_buf,
3018c2ecf20Sopenharmony_ci		.rx_buf = priv->spi_rx_buf,
3028c2ecf20Sopenharmony_ci		.len = len,
3038c2ecf20Sopenharmony_ci		.cs_change = 0,
3048c2ecf20Sopenharmony_ci	};
3058c2ecf20Sopenharmony_ci	struct spi_message m;
3068c2ecf20Sopenharmony_ci	int ret;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	spi_message_init(&m);
3098c2ecf20Sopenharmony_ci	spi_message_add_tail(&t, &m);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	ret = spi_sync(spi, &m);
3128c2ecf20Sopenharmony_ci	if (ret)
3138c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret);
3148c2ecf20Sopenharmony_ci	return ret;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int mcp251x_spi_write(struct spi_device *spi, int len)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3208c2ecf20Sopenharmony_ci	int ret;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	ret = spi_write(spi, priv->spi_tx_buf, len);
3238c2ecf20Sopenharmony_ci	if (ret)
3248c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "spi write failed: ret = %d\n", ret);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	return ret;
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_cistatic u8 mcp251x_read_reg(struct spi_device *spi, u8 reg)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3328c2ecf20Sopenharmony_ci	u8 val = 0;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_READ;
3358c2ecf20Sopenharmony_ci	priv->spi_tx_buf[1] = reg;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
3388c2ecf20Sopenharmony_ci		spi_write_then_read(spi, priv->spi_tx_buf, 2, &val, 1);
3398c2ecf20Sopenharmony_ci	} else {
3408c2ecf20Sopenharmony_ci		mcp251x_spi_trans(spi, 3);
3418c2ecf20Sopenharmony_ci		val = priv->spi_rx_buf[2];
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return val;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic void mcp251x_read_2regs(struct spi_device *spi, u8 reg, u8 *v1, u8 *v2)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_READ;
3528c2ecf20Sopenharmony_ci	priv->spi_tx_buf[1] = reg;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
3558c2ecf20Sopenharmony_ci		u8 val[2] = { 0 };
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		spi_write_then_read(spi, priv->spi_tx_buf, 2, val, 2);
3588c2ecf20Sopenharmony_ci		*v1 = val[0];
3598c2ecf20Sopenharmony_ci		*v2 = val[1];
3608c2ecf20Sopenharmony_ci	} else {
3618c2ecf20Sopenharmony_ci		mcp251x_spi_trans(spi, 4);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		*v1 = priv->spi_rx_buf[2];
3648c2ecf20Sopenharmony_ci		*v2 = priv->spi_rx_buf[3];
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_WRITE;
3738c2ecf20Sopenharmony_ci	priv->spi_tx_buf[1] = reg;
3748c2ecf20Sopenharmony_ci	priv->spi_tx_buf[2] = val;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	mcp251x_spi_write(spi, 3);
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic void mcp251x_write_2regs(struct spi_device *spi, u8 reg, u8 v1, u8 v2)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_WRITE;
3848c2ecf20Sopenharmony_ci	priv->spi_tx_buf[1] = reg;
3858c2ecf20Sopenharmony_ci	priv->spi_tx_buf[2] = v1;
3868c2ecf20Sopenharmony_ci	priv->spi_tx_buf[3] = v2;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	mcp251x_spi_write(spi, 4);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic void mcp251x_write_bits(struct spi_device *spi, u8 reg,
3928c2ecf20Sopenharmony_ci			       u8 mask, u8 val)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY;
3978c2ecf20Sopenharmony_ci	priv->spi_tx_buf[1] = reg;
3988c2ecf20Sopenharmony_ci	priv->spi_tx_buf[2] = mask;
3998c2ecf20Sopenharmony_ci	priv->spi_tx_buf[3] = val;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	mcp251x_spi_write(spi, 4);
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic u8 mcp251x_read_stat(struct spi_device *spi)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	return mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK;
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci#define mcp251x_read_stat_poll_timeout(addr, val, cond, delay_us, timeout_us) \
4108c2ecf20Sopenharmony_ci	readx_poll_timeout(mcp251x_read_stat, addr, val, cond, \
4118c2ecf20Sopenharmony_ci			   delay_us, timeout_us)
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci#ifdef CONFIG_GPIOLIB
4148c2ecf20Sopenharmony_cienum {
4158c2ecf20Sopenharmony_ci	MCP251X_GPIO_TX0RTS = 0,		/* inputs */
4168c2ecf20Sopenharmony_ci	MCP251X_GPIO_TX1RTS,
4178c2ecf20Sopenharmony_ci	MCP251X_GPIO_TX2RTS,
4188c2ecf20Sopenharmony_ci	MCP251X_GPIO_RX0BF,			/* outputs */
4198c2ecf20Sopenharmony_ci	MCP251X_GPIO_RX1BF,
4208c2ecf20Sopenharmony_ci};
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci#define MCP251X_GPIO_INPUT_MASK \
4238c2ecf20Sopenharmony_ci	GENMASK(MCP251X_GPIO_TX2RTS, MCP251X_GPIO_TX0RTS)
4248c2ecf20Sopenharmony_ci#define MCP251X_GPIO_OUTPUT_MASK \
4258c2ecf20Sopenharmony_ci	GENMASK(MCP251X_GPIO_RX1BF, MCP251X_GPIO_RX0BF)
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic const char * const mcp251x_gpio_names[] = {
4288c2ecf20Sopenharmony_ci	[MCP251X_GPIO_TX0RTS] = "TX0RTS",	/* inputs */
4298c2ecf20Sopenharmony_ci	[MCP251X_GPIO_TX1RTS] = "TX1RTS",
4308c2ecf20Sopenharmony_ci	[MCP251X_GPIO_TX2RTS] = "TX2RTS",
4318c2ecf20Sopenharmony_ci	[MCP251X_GPIO_RX0BF] = "RX0BF",		/* outputs */
4328c2ecf20Sopenharmony_ci	[MCP251X_GPIO_RX1BF] = "RX1BF",
4338c2ecf20Sopenharmony_ci};
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic inline bool mcp251x_gpio_is_input(unsigned int offset)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	return offset <= MCP251X_GPIO_TX2RTS;
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic int mcp251x_gpio_request(struct gpio_chip *chip,
4418c2ecf20Sopenharmony_ci				unsigned int offset)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
4448c2ecf20Sopenharmony_ci	u8 val;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	/* nothing to be done for inputs */
4478c2ecf20Sopenharmony_ci	if (mcp251x_gpio_is_input(offset))
4488c2ecf20Sopenharmony_ci		return 0;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
4538c2ecf20Sopenharmony_ci	mcp251x_write_bits(priv->spi, BFPCTRL, val, val);
4548c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	priv->reg_bfpctrl |= val;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return 0;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic void mcp251x_gpio_free(struct gpio_chip *chip,
4628c2ecf20Sopenharmony_ci			      unsigned int offset)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
4658c2ecf20Sopenharmony_ci	u8 val;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	/* nothing to be done for inputs */
4688c2ecf20Sopenharmony_ci	if (mcp251x_gpio_is_input(offset))
4698c2ecf20Sopenharmony_ci		return;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	val = BFPCTRL_BFE(offset - MCP251X_GPIO_RX0BF);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
4748c2ecf20Sopenharmony_ci	mcp251x_write_bits(priv->spi, BFPCTRL, val, 0);
4758c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	priv->reg_bfpctrl &= ~val;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_cistatic int mcp251x_gpio_get_direction(struct gpio_chip *chip,
4818c2ecf20Sopenharmony_ci				      unsigned int offset)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	if (mcp251x_gpio_is_input(offset))
4848c2ecf20Sopenharmony_ci		return GPIOF_DIR_IN;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	return GPIOF_DIR_OUT;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic int mcp251x_gpio_get(struct gpio_chip *chip, unsigned int offset)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
4928c2ecf20Sopenharmony_ci	u8 reg, mask, val;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if (mcp251x_gpio_is_input(offset)) {
4958c2ecf20Sopenharmony_ci		reg = TXRTSCTRL;
4968c2ecf20Sopenharmony_ci		mask = TXRTSCTRL_RTS(offset);
4978c2ecf20Sopenharmony_ci	} else {
4988c2ecf20Sopenharmony_ci		reg = BFPCTRL;
4998c2ecf20Sopenharmony_ci		mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF);
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
5038c2ecf20Sopenharmony_ci	val = mcp251x_read_reg(priv->spi, reg);
5048c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	return !!(val & mask);
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic int mcp251x_gpio_get_multiple(struct gpio_chip *chip,
5108c2ecf20Sopenharmony_ci				     unsigned long *maskp, unsigned long *bitsp)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
5138c2ecf20Sopenharmony_ci	unsigned long bits = 0;
5148c2ecf20Sopenharmony_ci	u8 val;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
5178c2ecf20Sopenharmony_ci	if (maskp[0] & MCP251X_GPIO_INPUT_MASK) {
5188c2ecf20Sopenharmony_ci		val = mcp251x_read_reg(priv->spi, TXRTSCTRL);
5198c2ecf20Sopenharmony_ci		val = FIELD_GET(TXRTSCTRL_RTS_MASK, val);
5208c2ecf20Sopenharmony_ci		bits |= FIELD_PREP(MCP251X_GPIO_INPUT_MASK, val);
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci	if (maskp[0] & MCP251X_GPIO_OUTPUT_MASK) {
5238c2ecf20Sopenharmony_ci		val = mcp251x_read_reg(priv->spi, BFPCTRL);
5248c2ecf20Sopenharmony_ci		val = FIELD_GET(BFPCTRL_BFS_MASK, val);
5258c2ecf20Sopenharmony_ci		bits |= FIELD_PREP(MCP251X_GPIO_OUTPUT_MASK, val);
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	bitsp[0] = bits;
5308c2ecf20Sopenharmony_ci	return 0;
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic void mcp251x_gpio_set(struct gpio_chip *chip, unsigned int offset,
5348c2ecf20Sopenharmony_ci			     int value)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
5378c2ecf20Sopenharmony_ci	u8 mask, val;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	mask = BFPCTRL_BFS(offset - MCP251X_GPIO_RX0BF);
5408c2ecf20Sopenharmony_ci	val = value ? mask : 0;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
5438c2ecf20Sopenharmony_ci	mcp251x_write_bits(priv->spi, BFPCTRL, mask, val);
5448c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	priv->reg_bfpctrl &= ~mask;
5478c2ecf20Sopenharmony_ci	priv->reg_bfpctrl |= val;
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic void
5518c2ecf20Sopenharmony_cimcp251x_gpio_set_multiple(struct gpio_chip *chip,
5528c2ecf20Sopenharmony_ci			  unsigned long *maskp, unsigned long *bitsp)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = gpiochip_get_data(chip);
5558c2ecf20Sopenharmony_ci	u8 mask, val;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	mask = FIELD_GET(MCP251X_GPIO_OUTPUT_MASK, maskp[0]);
5588c2ecf20Sopenharmony_ci	mask = FIELD_PREP(BFPCTRL_BFS_MASK, mask);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	val = FIELD_GET(MCP251X_GPIO_OUTPUT_MASK, bitsp[0]);
5618c2ecf20Sopenharmony_ci	val = FIELD_PREP(BFPCTRL_BFS_MASK, val);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	if (!mask)
5648c2ecf20Sopenharmony_ci		return;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
5678c2ecf20Sopenharmony_ci	mcp251x_write_bits(priv->spi, BFPCTRL, mask, val);
5688c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	priv->reg_bfpctrl &= ~mask;
5718c2ecf20Sopenharmony_ci	priv->reg_bfpctrl |= val;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic void mcp251x_gpio_restore(struct spi_device *spi)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, BFPCTRL, priv->reg_bfpctrl);
5798c2ecf20Sopenharmony_ci}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_cistatic int mcp251x_gpio_setup(struct mcp251x_priv *priv)
5828c2ecf20Sopenharmony_ci{
5838c2ecf20Sopenharmony_ci	struct gpio_chip *gpio = &priv->gpio;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	if (!device_property_present(&priv->spi->dev, "gpio-controller"))
5868c2ecf20Sopenharmony_ci		return 0;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	/* gpiochip handles TX[0..2]RTS and RX[0..1]BF */
5898c2ecf20Sopenharmony_ci	gpio->label = priv->spi->modalias;
5908c2ecf20Sopenharmony_ci	gpio->parent = &priv->spi->dev;
5918c2ecf20Sopenharmony_ci	gpio->owner = THIS_MODULE;
5928c2ecf20Sopenharmony_ci	gpio->request = mcp251x_gpio_request;
5938c2ecf20Sopenharmony_ci	gpio->free = mcp251x_gpio_free;
5948c2ecf20Sopenharmony_ci	gpio->get_direction = mcp251x_gpio_get_direction;
5958c2ecf20Sopenharmony_ci	gpio->get = mcp251x_gpio_get;
5968c2ecf20Sopenharmony_ci	gpio->get_multiple = mcp251x_gpio_get_multiple;
5978c2ecf20Sopenharmony_ci	gpio->set = mcp251x_gpio_set;
5988c2ecf20Sopenharmony_ci	gpio->set_multiple = mcp251x_gpio_set_multiple;
5998c2ecf20Sopenharmony_ci	gpio->base = -1;
6008c2ecf20Sopenharmony_ci	gpio->ngpio = ARRAY_SIZE(mcp251x_gpio_names);
6018c2ecf20Sopenharmony_ci	gpio->names = mcp251x_gpio_names;
6028c2ecf20Sopenharmony_ci	gpio->can_sleep = true;
6038c2ecf20Sopenharmony_ci#ifdef CONFIG_OF_GPIO
6048c2ecf20Sopenharmony_ci	gpio->of_node = priv->spi->dev.of_node;
6058c2ecf20Sopenharmony_ci#endif
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	return devm_gpiochip_add_data(&priv->spi->dev, gpio, priv);
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci#else
6108c2ecf20Sopenharmony_cistatic inline void mcp251x_gpio_restore(struct spi_device *spi)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic inline int mcp251x_gpio_setup(struct mcp251x_priv *priv)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	return 0;
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci#endif
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf,
6218c2ecf20Sopenharmony_ci				int len, int tx_buf_idx)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if (mcp251x_is_2510(spi)) {
6268c2ecf20Sopenharmony_ci		int i;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		for (i = 1; i < TXBDAT_OFF + len; i++)
6298c2ecf20Sopenharmony_ci			mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i,
6308c2ecf20Sopenharmony_ci					  buf[i]);
6318c2ecf20Sopenharmony_ci	} else {
6328c2ecf20Sopenharmony_ci		memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len);
6338c2ecf20Sopenharmony_ci		mcp251x_spi_write(spi, TXBDAT_OFF + len);
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame,
6388c2ecf20Sopenharmony_ci			  int tx_buf_idx)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
6418c2ecf20Sopenharmony_ci	u32 sid, eid, exide, rtr;
6428c2ecf20Sopenharmony_ci	u8 buf[SPI_TRANSFER_BUF_LEN];
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	exide = (frame->can_id & CAN_EFF_FLAG) ? 1 : 0; /* Extended ID Enable */
6458c2ecf20Sopenharmony_ci	if (exide)
6468c2ecf20Sopenharmony_ci		sid = (frame->can_id & CAN_EFF_MASK) >> 18;
6478c2ecf20Sopenharmony_ci	else
6488c2ecf20Sopenharmony_ci		sid = frame->can_id & CAN_SFF_MASK; /* Standard ID */
6498c2ecf20Sopenharmony_ci	eid = frame->can_id & CAN_EFF_MASK; /* Extended ID */
6508c2ecf20Sopenharmony_ci	rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0; /* Remote transmission */
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	buf[TXBCTRL_OFF] = INSTRUCTION_LOAD_TXB(tx_buf_idx);
6538c2ecf20Sopenharmony_ci	buf[TXBSIDH_OFF] = sid >> SIDH_SHIFT;
6548c2ecf20Sopenharmony_ci	buf[TXBSIDL_OFF] = ((sid & SIDL_SID_MASK) << SIDL_SID_SHIFT) |
6558c2ecf20Sopenharmony_ci		(exide << SIDL_EXIDE_SHIFT) |
6568c2ecf20Sopenharmony_ci		((eid >> SIDL_EID_SHIFT) & SIDL_EID_MASK);
6578c2ecf20Sopenharmony_ci	buf[TXBEID8_OFF] = GET_BYTE(eid, 1);
6588c2ecf20Sopenharmony_ci	buf[TXBEID0_OFF] = GET_BYTE(eid, 0);
6598c2ecf20Sopenharmony_ci	buf[TXBDLC_OFF] = (rtr << DLC_RTR_SHIFT) | frame->can_dlc;
6608c2ecf20Sopenharmony_ci	memcpy(buf + TXBDAT_OFF, frame->data, frame->can_dlc);
6618c2ecf20Sopenharmony_ci	mcp251x_hw_tx_frame(spi, buf, frame->can_dlc, tx_buf_idx);
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	/* use INSTRUCTION_RTS, to avoid "repeated frame problem" */
6648c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx);
6658c2ecf20Sopenharmony_ci	mcp251x_spi_write(priv->spi, 1);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
6698c2ecf20Sopenharmony_ci				int buf_idx)
6708c2ecf20Sopenharmony_ci{
6718c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	if (mcp251x_is_2510(spi)) {
6748c2ecf20Sopenharmony_ci		int i, len;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		for (i = 1; i < RXBDAT_OFF; i++)
6778c2ecf20Sopenharmony_ci			buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		len = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
6808c2ecf20Sopenharmony_ci		for (; i < (RXBDAT_OFF + len); i++)
6818c2ecf20Sopenharmony_ci			buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
6828c2ecf20Sopenharmony_ci	} else {
6838c2ecf20Sopenharmony_ci		priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx);
6848c2ecf20Sopenharmony_ci		if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
6858c2ecf20Sopenharmony_ci			spi_write_then_read(spi, priv->spi_tx_buf, 1,
6868c2ecf20Sopenharmony_ci					    priv->spi_rx_buf,
6878c2ecf20Sopenharmony_ci					    SPI_TRANSFER_BUF_LEN);
6888c2ecf20Sopenharmony_ci			memcpy(buf + 1, priv->spi_rx_buf,
6898c2ecf20Sopenharmony_ci			       SPI_TRANSFER_BUF_LEN - 1);
6908c2ecf20Sopenharmony_ci		} else {
6918c2ecf20Sopenharmony_ci			mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
6928c2ecf20Sopenharmony_ci			memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
6938c2ecf20Sopenharmony_ci		}
6948c2ecf20Sopenharmony_ci	}
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic void mcp251x_hw_rx(struct spi_device *spi, int buf_idx)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
7008c2ecf20Sopenharmony_ci	struct sk_buff *skb;
7018c2ecf20Sopenharmony_ci	struct can_frame *frame;
7028c2ecf20Sopenharmony_ci	u8 buf[SPI_TRANSFER_BUF_LEN];
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	skb = alloc_can_skb(priv->net, &frame);
7058c2ecf20Sopenharmony_ci	if (!skb) {
7068c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "cannot allocate RX skb\n");
7078c2ecf20Sopenharmony_ci		priv->net->stats.rx_dropped++;
7088c2ecf20Sopenharmony_ci		return;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	mcp251x_hw_rx_frame(spi, buf, buf_idx);
7128c2ecf20Sopenharmony_ci	if (buf[RXBSIDL_OFF] & RXBSIDL_IDE) {
7138c2ecf20Sopenharmony_ci		/* Extended ID format */
7148c2ecf20Sopenharmony_ci		frame->can_id = CAN_EFF_FLAG;
7158c2ecf20Sopenharmony_ci		frame->can_id |=
7168c2ecf20Sopenharmony_ci			/* Extended ID part */
7178c2ecf20Sopenharmony_ci			SET_BYTE(buf[RXBSIDL_OFF] & RXBSIDL_EID, 2) |
7188c2ecf20Sopenharmony_ci			SET_BYTE(buf[RXBEID8_OFF], 1) |
7198c2ecf20Sopenharmony_ci			SET_BYTE(buf[RXBEID0_OFF], 0) |
7208c2ecf20Sopenharmony_ci			/* Standard ID part */
7218c2ecf20Sopenharmony_ci			(((buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
7228c2ecf20Sopenharmony_ci			  (buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT)) << 18);
7238c2ecf20Sopenharmony_ci		/* Remote transmission request */
7248c2ecf20Sopenharmony_ci		if (buf[RXBDLC_OFF] & RXBDLC_RTR)
7258c2ecf20Sopenharmony_ci			frame->can_id |= CAN_RTR_FLAG;
7268c2ecf20Sopenharmony_ci	} else {
7278c2ecf20Sopenharmony_ci		/* Standard ID format */
7288c2ecf20Sopenharmony_ci		frame->can_id =
7298c2ecf20Sopenharmony_ci			(buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
7308c2ecf20Sopenharmony_ci			(buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT);
7318c2ecf20Sopenharmony_ci		if (buf[RXBSIDL_OFF] & RXBSIDL_SRR)
7328c2ecf20Sopenharmony_ci			frame->can_id |= CAN_RTR_FLAG;
7338c2ecf20Sopenharmony_ci	}
7348c2ecf20Sopenharmony_ci	/* Data length */
7358c2ecf20Sopenharmony_ci	frame->can_dlc = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
7368c2ecf20Sopenharmony_ci	memcpy(frame->data, buf + RXBDAT_OFF, frame->can_dlc);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	priv->net->stats.rx_packets++;
7398c2ecf20Sopenharmony_ci	priv->net->stats.rx_bytes += frame->can_dlc;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	can_led_event(priv->net, CAN_LED_EVENT_RX);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	netif_rx_ni(skb);
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic void mcp251x_hw_sleep(struct spi_device *spi)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP);
7498c2ecf20Sopenharmony_ci}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci/* May only be called when device is sleeping! */
7528c2ecf20Sopenharmony_cistatic int mcp251x_hw_wake(struct spi_device *spi)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	u8 value;
7558c2ecf20Sopenharmony_ci	int ret;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	/* Force wakeup interrupt to wake device, but don't execute IST */
7588c2ecf20Sopenharmony_ci	disable_irq_nosync(spi->irq);
7598c2ecf20Sopenharmony_ci	mcp251x_write_2regs(spi, CANINTE, CANINTE_WAKIE, CANINTF_WAKIF);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Wait for oscillator startup timer after wake up */
7628c2ecf20Sopenharmony_ci	mdelay(MCP251X_OST_DELAY_MS);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* Put device into config mode */
7658c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_CONF);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/* Wait for the device to enter config mode */
7688c2ecf20Sopenharmony_ci	ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF,
7698c2ecf20Sopenharmony_ci					     MCP251X_OST_DELAY_MS * 1000,
7708c2ecf20Sopenharmony_ci					     USEC_PER_SEC);
7718c2ecf20Sopenharmony_ci	if (ret) {
7728c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "MCP251x didn't enter in config mode\n");
7738c2ecf20Sopenharmony_ci		return ret;
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	/* Disable and clear pending interrupts */
7778c2ecf20Sopenharmony_ci	mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00);
7788c2ecf20Sopenharmony_ci	enable_irq(spi->irq);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	return 0;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb,
7848c2ecf20Sopenharmony_ci					   struct net_device *net)
7858c2ecf20Sopenharmony_ci{
7868c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
7878c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	if (priv->tx_skb || priv->tx_len) {
7908c2ecf20Sopenharmony_ci		dev_warn(&spi->dev, "hard_xmit called while tx busy\n");
7918c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (can_dropped_invalid_skb(net, skb))
7958c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	netif_stop_queue(net);
7988c2ecf20Sopenharmony_ci	priv->tx_skb = skb;
7998c2ecf20Sopenharmony_ci	queue_work(priv->wq, &priv->tx_work);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistatic int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode)
8058c2ecf20Sopenharmony_ci{
8068c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	switch (mode) {
8098c2ecf20Sopenharmony_ci	case CAN_MODE_START:
8108c2ecf20Sopenharmony_ci		mcp251x_clean(net);
8118c2ecf20Sopenharmony_ci		/* We have to delay work since SPI I/O may sleep */
8128c2ecf20Sopenharmony_ci		priv->can.state = CAN_STATE_ERROR_ACTIVE;
8138c2ecf20Sopenharmony_ci		priv->restart_tx = 1;
8148c2ecf20Sopenharmony_ci		if (priv->can.restart_ms == 0)
8158c2ecf20Sopenharmony_ci			priv->after_suspend = AFTER_SUSPEND_RESTART;
8168c2ecf20Sopenharmony_ci		queue_work(priv->wq, &priv->restart_work);
8178c2ecf20Sopenharmony_ci		break;
8188c2ecf20Sopenharmony_ci	default:
8198c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	return 0;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic int mcp251x_set_normal_mode(struct spi_device *spi)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
8288c2ecf20Sopenharmony_ci	u8 value;
8298c2ecf20Sopenharmony_ci	int ret;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	/* Enable interrupts */
8328c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, CANINTE,
8338c2ecf20Sopenharmony_ci			  CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE |
8348c2ecf20Sopenharmony_ci			  CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
8378c2ecf20Sopenharmony_ci		/* Put device into loopback mode */
8388c2ecf20Sopenharmony_ci		mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK);
8398c2ecf20Sopenharmony_ci	} else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
8408c2ecf20Sopenharmony_ci		/* Put device into listen-only mode */
8418c2ecf20Sopenharmony_ci		mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY);
8428c2ecf20Sopenharmony_ci	} else {
8438c2ecf20Sopenharmony_ci		/* Put device into normal mode */
8448c2ecf20Sopenharmony_ci		mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		/* Wait for the device to enter normal mode */
8478c2ecf20Sopenharmony_ci		ret = mcp251x_read_stat_poll_timeout(spi, value, value == 0,
8488c2ecf20Sopenharmony_ci						     MCP251X_OST_DELAY_MS * 1000,
8498c2ecf20Sopenharmony_ci						     USEC_PER_SEC);
8508c2ecf20Sopenharmony_ci		if (ret) {
8518c2ecf20Sopenharmony_ci			dev_err(&spi->dev, "MCP251x didn't enter in normal mode\n");
8528c2ecf20Sopenharmony_ci			return ret;
8538c2ecf20Sopenharmony_ci		}
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	priv->can.state = CAN_STATE_ERROR_ACTIVE;
8568c2ecf20Sopenharmony_ci	return 0;
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int mcp251x_do_set_bittiming(struct net_device *net)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
8628c2ecf20Sopenharmony_ci	struct can_bittiming *bt = &priv->can.bittiming;
8638c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << CNF1_SJW_SHIFT) |
8668c2ecf20Sopenharmony_ci			  (bt->brp - 1));
8678c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE |
8688c2ecf20Sopenharmony_ci			  (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
8698c2ecf20Sopenharmony_ci			   CNF2_SAM : 0) |
8708c2ecf20Sopenharmony_ci			  ((bt->phase_seg1 - 1) << CNF2_PS1_SHIFT) |
8718c2ecf20Sopenharmony_ci			  (bt->prop_seg - 1));
8728c2ecf20Sopenharmony_ci	mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK,
8738c2ecf20Sopenharmony_ci			   (bt->phase_seg2 - 1));
8748c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "CNF: 0x%02x 0x%02x 0x%02x\n",
8758c2ecf20Sopenharmony_ci		mcp251x_read_reg(spi, CNF1),
8768c2ecf20Sopenharmony_ci		mcp251x_read_reg(spi, CNF2),
8778c2ecf20Sopenharmony_ci		mcp251x_read_reg(spi, CNF3));
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	return 0;
8808c2ecf20Sopenharmony_ci}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_cistatic int mcp251x_setup(struct net_device *net, struct spi_device *spi)
8838c2ecf20Sopenharmony_ci{
8848c2ecf20Sopenharmony_ci	mcp251x_do_set_bittiming(net);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, RXBCTRL(0),
8878c2ecf20Sopenharmony_ci			  RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1);
8888c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, RXBCTRL(1),
8898c2ecf20Sopenharmony_ci			  RXBCTRL_RXM0 | RXBCTRL_RXM1);
8908c2ecf20Sopenharmony_ci	return 0;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic int mcp251x_hw_reset(struct spi_device *spi)
8948c2ecf20Sopenharmony_ci{
8958c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
8968c2ecf20Sopenharmony_ci	u8 value;
8978c2ecf20Sopenharmony_ci	int ret;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/* Wait for oscillator startup timer after power up */
9008c2ecf20Sopenharmony_ci	mdelay(MCP251X_OST_DELAY_MS);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	priv->spi_tx_buf[0] = INSTRUCTION_RESET;
9038c2ecf20Sopenharmony_ci	ret = mcp251x_spi_write(spi, 1);
9048c2ecf20Sopenharmony_ci	if (ret)
9058c2ecf20Sopenharmony_ci		return ret;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	/* Wait for oscillator startup timer after reset */
9088c2ecf20Sopenharmony_ci	mdelay(MCP251X_OST_DELAY_MS);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	/* Wait for reset to finish */
9118c2ecf20Sopenharmony_ci	ret = mcp251x_read_stat_poll_timeout(spi, value, value == CANCTRL_REQOP_CONF,
9128c2ecf20Sopenharmony_ci					     MCP251X_OST_DELAY_MS * 1000,
9138c2ecf20Sopenharmony_ci					     USEC_PER_SEC);
9148c2ecf20Sopenharmony_ci	if (ret)
9158c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "MCP251x didn't enter in conf mode after reset\n");
9168c2ecf20Sopenharmony_ci	return ret;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic int mcp251x_hw_probe(struct spi_device *spi)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	u8 ctrl;
9228c2ecf20Sopenharmony_ci	int ret;
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	ret = mcp251x_hw_reset(spi);
9258c2ecf20Sopenharmony_ci	if (ret)
9268c2ecf20Sopenharmony_ci		return ret;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	ctrl = mcp251x_read_reg(spi, CANCTRL);
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "CANCTRL 0x%02x\n", ctrl);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/* Check for power up default value */
9338c2ecf20Sopenharmony_ci	if ((ctrl & 0x17) != 0x07)
9348c2ecf20Sopenharmony_ci		return -ENODEV;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	return 0;
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic int mcp251x_power_enable(struct regulator *reg, int enable)
9408c2ecf20Sopenharmony_ci{
9418c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(reg))
9428c2ecf20Sopenharmony_ci		return 0;
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	if (enable)
9458c2ecf20Sopenharmony_ci		return regulator_enable(reg);
9468c2ecf20Sopenharmony_ci	else
9478c2ecf20Sopenharmony_ci		return regulator_disable(reg);
9488c2ecf20Sopenharmony_ci}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_cistatic int mcp251x_stop(struct net_device *net)
9518c2ecf20Sopenharmony_ci{
9528c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
9538c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	close_candev(net);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	priv->force_quit = 1;
9588c2ecf20Sopenharmony_ci	free_irq(spi->irq, priv);
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	/* Disable and clear pending interrupts */
9638c2ecf20Sopenharmony_ci	mcp251x_write_2regs(spi, CANINTE, 0x00, 0x00);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	mcp251x_write_reg(spi, TXBCTRL(0), 0);
9668c2ecf20Sopenharmony_ci	mcp251x_clean(net);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	mcp251x_hw_sleep(spi);
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->transceiver, 0);
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	priv->can.state = CAN_STATE_STOPPED;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	can_led_event(net, CAN_LED_EVENT_STOP);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	return 0;
9798c2ecf20Sopenharmony_ci}
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cistatic void mcp251x_error_skb(struct net_device *net, int can_id, int data1)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct sk_buff *skb;
9848c2ecf20Sopenharmony_ci	struct can_frame *frame;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	skb = alloc_can_err_skb(net, &frame);
9878c2ecf20Sopenharmony_ci	if (skb) {
9888c2ecf20Sopenharmony_ci		frame->can_id |= can_id;
9898c2ecf20Sopenharmony_ci		frame->data[1] = data1;
9908c2ecf20Sopenharmony_ci		netif_rx_ni(skb);
9918c2ecf20Sopenharmony_ci	} else {
9928c2ecf20Sopenharmony_ci		netdev_err(net, "cannot allocate error skb\n");
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_cistatic void mcp251x_tx_work_handler(struct work_struct *ws)
9978c2ecf20Sopenharmony_ci{
9988c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
9998c2ecf20Sopenharmony_ci						 tx_work);
10008c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
10018c2ecf20Sopenharmony_ci	struct net_device *net = priv->net;
10028c2ecf20Sopenharmony_ci	struct can_frame *frame;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
10058c2ecf20Sopenharmony_ci	if (priv->tx_skb) {
10068c2ecf20Sopenharmony_ci		if (priv->can.state == CAN_STATE_BUS_OFF) {
10078c2ecf20Sopenharmony_ci			mcp251x_clean(net);
10088c2ecf20Sopenharmony_ci		} else {
10098c2ecf20Sopenharmony_ci			frame = (struct can_frame *)priv->tx_skb->data;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci			if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN)
10128c2ecf20Sopenharmony_ci				frame->can_dlc = CAN_FRAME_MAX_DATA_LEN;
10138c2ecf20Sopenharmony_ci			mcp251x_hw_tx(spi, frame, 0);
10148c2ecf20Sopenharmony_ci			priv->tx_len = 1 + frame->can_dlc;
10158c2ecf20Sopenharmony_ci			can_put_echo_skb(priv->tx_skb, net, 0);
10168c2ecf20Sopenharmony_ci			priv->tx_skb = NULL;
10178c2ecf20Sopenharmony_ci		}
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_cistatic void mcp251x_restart_work_handler(struct work_struct *ws)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
10258c2ecf20Sopenharmony_ci						 restart_work);
10268c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
10278c2ecf20Sopenharmony_ci	struct net_device *net = priv->net;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
10308c2ecf20Sopenharmony_ci	if (priv->after_suspend) {
10318c2ecf20Sopenharmony_ci		if (priv->after_suspend & AFTER_SUSPEND_POWER) {
10328c2ecf20Sopenharmony_ci			mcp251x_hw_reset(spi);
10338c2ecf20Sopenharmony_ci			mcp251x_setup(net, spi);
10348c2ecf20Sopenharmony_ci			mcp251x_gpio_restore(spi);
10358c2ecf20Sopenharmony_ci		} else {
10368c2ecf20Sopenharmony_ci			mcp251x_hw_wake(spi);
10378c2ecf20Sopenharmony_ci		}
10388c2ecf20Sopenharmony_ci		priv->force_quit = 0;
10398c2ecf20Sopenharmony_ci		if (priv->after_suspend & AFTER_SUSPEND_RESTART) {
10408c2ecf20Sopenharmony_ci			mcp251x_set_normal_mode(spi);
10418c2ecf20Sopenharmony_ci		} else if (priv->after_suspend & AFTER_SUSPEND_UP) {
10428c2ecf20Sopenharmony_ci			netif_device_attach(net);
10438c2ecf20Sopenharmony_ci			mcp251x_clean(net);
10448c2ecf20Sopenharmony_ci			mcp251x_set_normal_mode(spi);
10458c2ecf20Sopenharmony_ci			netif_wake_queue(net);
10468c2ecf20Sopenharmony_ci		} else {
10478c2ecf20Sopenharmony_ci			mcp251x_hw_sleep(spi);
10488c2ecf20Sopenharmony_ci		}
10498c2ecf20Sopenharmony_ci		priv->after_suspend = 0;
10508c2ecf20Sopenharmony_ci	}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	if (priv->restart_tx) {
10538c2ecf20Sopenharmony_ci		priv->restart_tx = 0;
10548c2ecf20Sopenharmony_ci		mcp251x_write_reg(spi, TXBCTRL(0), 0);
10558c2ecf20Sopenharmony_ci		mcp251x_clean(net);
10568c2ecf20Sopenharmony_ci		netif_wake_queue(net);
10578c2ecf20Sopenharmony_ci		mcp251x_error_skb(net, CAN_ERR_RESTARTED, 0);
10588c2ecf20Sopenharmony_ci	}
10598c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
10608c2ecf20Sopenharmony_ci}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_cistatic irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
10638c2ecf20Sopenharmony_ci{
10648c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = dev_id;
10658c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
10668c2ecf20Sopenharmony_ci	struct net_device *net = priv->net;
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
10698c2ecf20Sopenharmony_ci	while (!priv->force_quit) {
10708c2ecf20Sopenharmony_ci		enum can_state new_state;
10718c2ecf20Sopenharmony_ci		u8 intf, eflag;
10728c2ecf20Sopenharmony_ci		u8 clear_intf = 0;
10738c2ecf20Sopenharmony_ci		int can_id = 0, data1 = 0;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci		mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci		/* receive buffer 0 */
10788c2ecf20Sopenharmony_ci		if (intf & CANINTF_RX0IF) {
10798c2ecf20Sopenharmony_ci			mcp251x_hw_rx(spi, 0);
10808c2ecf20Sopenharmony_ci			/* Free one buffer ASAP
10818c2ecf20Sopenharmony_ci			 * (The MCP2515/25625 does this automatically.)
10828c2ecf20Sopenharmony_ci			 */
10838c2ecf20Sopenharmony_ci			if (mcp251x_is_2510(spi))
10848c2ecf20Sopenharmony_ci				mcp251x_write_bits(spi, CANINTF,
10858c2ecf20Sopenharmony_ci						   CANINTF_RX0IF, 0x00);
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci			/* check if buffer 1 is already known to be full, no need to re-read */
10888c2ecf20Sopenharmony_ci			if (!(intf & CANINTF_RX1IF)) {
10898c2ecf20Sopenharmony_ci				u8 intf1, eflag1;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci				/* intf needs to be read again to avoid a race condition */
10928c2ecf20Sopenharmony_ci				mcp251x_read_2regs(spi, CANINTF, &intf1, &eflag1);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci				/* combine flags from both operations for error handling */
10958c2ecf20Sopenharmony_ci				intf |= intf1;
10968c2ecf20Sopenharmony_ci				eflag |= eflag1;
10978c2ecf20Sopenharmony_ci			}
10988c2ecf20Sopenharmony_ci		}
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci		/* receive buffer 1 */
11018c2ecf20Sopenharmony_ci		if (intf & CANINTF_RX1IF) {
11028c2ecf20Sopenharmony_ci			mcp251x_hw_rx(spi, 1);
11038c2ecf20Sopenharmony_ci			/* The MCP2515/25625 does this automatically. */
11048c2ecf20Sopenharmony_ci			if (mcp251x_is_2510(spi))
11058c2ecf20Sopenharmony_ci				clear_intf |= CANINTF_RX1IF;
11068c2ecf20Sopenharmony_ci		}
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci		/* mask out flags we don't care about */
11098c2ecf20Sopenharmony_ci		intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		/* any error or tx interrupt we need to clear? */
11128c2ecf20Sopenharmony_ci		if (intf & (CANINTF_ERR | CANINTF_TX))
11138c2ecf20Sopenharmony_ci			clear_intf |= intf & (CANINTF_ERR | CANINTF_TX);
11148c2ecf20Sopenharmony_ci		if (clear_intf)
11158c2ecf20Sopenharmony_ci			mcp251x_write_bits(spi, CANINTF, clear_intf, 0x00);
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci		if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR))
11188c2ecf20Sopenharmony_ci			mcp251x_write_bits(spi, EFLG, eflag, 0x00);
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		/* Update can state */
11218c2ecf20Sopenharmony_ci		if (eflag & EFLG_TXBO) {
11228c2ecf20Sopenharmony_ci			new_state = CAN_STATE_BUS_OFF;
11238c2ecf20Sopenharmony_ci			can_id |= CAN_ERR_BUSOFF;
11248c2ecf20Sopenharmony_ci		} else if (eflag & EFLG_TXEP) {
11258c2ecf20Sopenharmony_ci			new_state = CAN_STATE_ERROR_PASSIVE;
11268c2ecf20Sopenharmony_ci			can_id |= CAN_ERR_CRTL;
11278c2ecf20Sopenharmony_ci			data1 |= CAN_ERR_CRTL_TX_PASSIVE;
11288c2ecf20Sopenharmony_ci		} else if (eflag & EFLG_RXEP) {
11298c2ecf20Sopenharmony_ci			new_state = CAN_STATE_ERROR_PASSIVE;
11308c2ecf20Sopenharmony_ci			can_id |= CAN_ERR_CRTL;
11318c2ecf20Sopenharmony_ci			data1 |= CAN_ERR_CRTL_RX_PASSIVE;
11328c2ecf20Sopenharmony_ci		} else if (eflag & EFLG_TXWAR) {
11338c2ecf20Sopenharmony_ci			new_state = CAN_STATE_ERROR_WARNING;
11348c2ecf20Sopenharmony_ci			can_id |= CAN_ERR_CRTL;
11358c2ecf20Sopenharmony_ci			data1 |= CAN_ERR_CRTL_TX_WARNING;
11368c2ecf20Sopenharmony_ci		} else if (eflag & EFLG_RXWAR) {
11378c2ecf20Sopenharmony_ci			new_state = CAN_STATE_ERROR_WARNING;
11388c2ecf20Sopenharmony_ci			can_id |= CAN_ERR_CRTL;
11398c2ecf20Sopenharmony_ci			data1 |= CAN_ERR_CRTL_RX_WARNING;
11408c2ecf20Sopenharmony_ci		} else {
11418c2ecf20Sopenharmony_ci			new_state = CAN_STATE_ERROR_ACTIVE;
11428c2ecf20Sopenharmony_ci		}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci		/* Update can state statistics */
11458c2ecf20Sopenharmony_ci		switch (priv->can.state) {
11468c2ecf20Sopenharmony_ci		case CAN_STATE_ERROR_ACTIVE:
11478c2ecf20Sopenharmony_ci			if (new_state >= CAN_STATE_ERROR_WARNING &&
11488c2ecf20Sopenharmony_ci			    new_state <= CAN_STATE_BUS_OFF)
11498c2ecf20Sopenharmony_ci				priv->can.can_stats.error_warning++;
11508c2ecf20Sopenharmony_ci			fallthrough;
11518c2ecf20Sopenharmony_ci		case CAN_STATE_ERROR_WARNING:
11528c2ecf20Sopenharmony_ci			if (new_state >= CAN_STATE_ERROR_PASSIVE &&
11538c2ecf20Sopenharmony_ci			    new_state <= CAN_STATE_BUS_OFF)
11548c2ecf20Sopenharmony_ci				priv->can.can_stats.error_passive++;
11558c2ecf20Sopenharmony_ci			break;
11568c2ecf20Sopenharmony_ci		default:
11578c2ecf20Sopenharmony_ci			break;
11588c2ecf20Sopenharmony_ci		}
11598c2ecf20Sopenharmony_ci		priv->can.state = new_state;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci		if (intf & CANINTF_ERRIF) {
11628c2ecf20Sopenharmony_ci			/* Handle overflow counters */
11638c2ecf20Sopenharmony_ci			if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) {
11648c2ecf20Sopenharmony_ci				if (eflag & EFLG_RX0OVR) {
11658c2ecf20Sopenharmony_ci					net->stats.rx_over_errors++;
11668c2ecf20Sopenharmony_ci					net->stats.rx_errors++;
11678c2ecf20Sopenharmony_ci				}
11688c2ecf20Sopenharmony_ci				if (eflag & EFLG_RX1OVR) {
11698c2ecf20Sopenharmony_ci					net->stats.rx_over_errors++;
11708c2ecf20Sopenharmony_ci					net->stats.rx_errors++;
11718c2ecf20Sopenharmony_ci				}
11728c2ecf20Sopenharmony_ci				can_id |= CAN_ERR_CRTL;
11738c2ecf20Sopenharmony_ci				data1 |= CAN_ERR_CRTL_RX_OVERFLOW;
11748c2ecf20Sopenharmony_ci			}
11758c2ecf20Sopenharmony_ci			mcp251x_error_skb(net, can_id, data1);
11768c2ecf20Sopenharmony_ci		}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci		if (priv->can.state == CAN_STATE_BUS_OFF) {
11798c2ecf20Sopenharmony_ci			if (priv->can.restart_ms == 0) {
11808c2ecf20Sopenharmony_ci				priv->force_quit = 1;
11818c2ecf20Sopenharmony_ci				priv->can.can_stats.bus_off++;
11828c2ecf20Sopenharmony_ci				can_bus_off(net);
11838c2ecf20Sopenharmony_ci				mcp251x_hw_sleep(spi);
11848c2ecf20Sopenharmony_ci				break;
11858c2ecf20Sopenharmony_ci			}
11868c2ecf20Sopenharmony_ci		}
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci		if (intf == 0)
11898c2ecf20Sopenharmony_ci			break;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci		if (intf & CANINTF_TX) {
11928c2ecf20Sopenharmony_ci			net->stats.tx_packets++;
11938c2ecf20Sopenharmony_ci			net->stats.tx_bytes += priv->tx_len - 1;
11948c2ecf20Sopenharmony_ci			can_led_event(net, CAN_LED_EVENT_TX);
11958c2ecf20Sopenharmony_ci			if (priv->tx_len) {
11968c2ecf20Sopenharmony_ci				can_get_echo_skb(net, 0);
11978c2ecf20Sopenharmony_ci				priv->tx_len = 0;
11988c2ecf20Sopenharmony_ci			}
11998c2ecf20Sopenharmony_ci			netif_wake_queue(net);
12008c2ecf20Sopenharmony_ci		}
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
12038c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
12048c2ecf20Sopenharmony_ci}
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_cistatic int mcp251x_open(struct net_device *net)
12078c2ecf20Sopenharmony_ci{
12088c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = netdev_priv(net);
12098c2ecf20Sopenharmony_ci	struct spi_device *spi = priv->spi;
12108c2ecf20Sopenharmony_ci	unsigned long flags = 0;
12118c2ecf20Sopenharmony_ci	int ret;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	ret = open_candev(net);
12148c2ecf20Sopenharmony_ci	if (ret) {
12158c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "unable to set initial baudrate!\n");
12168c2ecf20Sopenharmony_ci		return ret;
12178c2ecf20Sopenharmony_ci	}
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	mutex_lock(&priv->mcp_lock);
12208c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->transceiver, 1);
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	priv->force_quit = 0;
12238c2ecf20Sopenharmony_ci	priv->tx_skb = NULL;
12248c2ecf20Sopenharmony_ci	priv->tx_len = 0;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	if (!dev_fwnode(&spi->dev))
12278c2ecf20Sopenharmony_ci		flags = IRQF_TRIGGER_FALLING;
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci	ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist,
12308c2ecf20Sopenharmony_ci				   flags | IRQF_ONESHOT, dev_name(&spi->dev),
12318c2ecf20Sopenharmony_ci				   priv);
12328c2ecf20Sopenharmony_ci	if (ret) {
12338c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
12348c2ecf20Sopenharmony_ci		goto out_close;
12358c2ecf20Sopenharmony_ci	}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	ret = mcp251x_hw_wake(spi);
12388c2ecf20Sopenharmony_ci	if (ret)
12398c2ecf20Sopenharmony_ci		goto out_free_irq;
12408c2ecf20Sopenharmony_ci	ret = mcp251x_setup(net, spi);
12418c2ecf20Sopenharmony_ci	if (ret)
12428c2ecf20Sopenharmony_ci		goto out_free_irq;
12438c2ecf20Sopenharmony_ci	ret = mcp251x_set_normal_mode(spi);
12448c2ecf20Sopenharmony_ci	if (ret)
12458c2ecf20Sopenharmony_ci		goto out_free_irq;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	can_led_event(net, CAN_LED_EVENT_OPEN);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	netif_wake_queue(net);
12508c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	return 0;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ciout_free_irq:
12558c2ecf20Sopenharmony_ci	free_irq(spi->irq, priv);
12568c2ecf20Sopenharmony_ci	mcp251x_hw_sleep(spi);
12578c2ecf20Sopenharmony_ciout_close:
12588c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->transceiver, 0);
12598c2ecf20Sopenharmony_ci	close_candev(net);
12608c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mcp_lock);
12618c2ecf20Sopenharmony_ci	return ret;
12628c2ecf20Sopenharmony_ci}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_cistatic const struct net_device_ops mcp251x_netdev_ops = {
12658c2ecf20Sopenharmony_ci	.ndo_open = mcp251x_open,
12668c2ecf20Sopenharmony_ci	.ndo_stop = mcp251x_stop,
12678c2ecf20Sopenharmony_ci	.ndo_start_xmit = mcp251x_hard_start_xmit,
12688c2ecf20Sopenharmony_ci	.ndo_change_mtu = can_change_mtu,
12698c2ecf20Sopenharmony_ci};
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_cistatic const struct of_device_id mcp251x_of_match[] = {
12728c2ecf20Sopenharmony_ci	{
12738c2ecf20Sopenharmony_ci		.compatible	= "microchip,mcp2510",
12748c2ecf20Sopenharmony_ci		.data		= (void *)CAN_MCP251X_MCP2510,
12758c2ecf20Sopenharmony_ci	},
12768c2ecf20Sopenharmony_ci	{
12778c2ecf20Sopenharmony_ci		.compatible	= "microchip,mcp2515",
12788c2ecf20Sopenharmony_ci		.data		= (void *)CAN_MCP251X_MCP2515,
12798c2ecf20Sopenharmony_ci	},
12808c2ecf20Sopenharmony_ci	{
12818c2ecf20Sopenharmony_ci		.compatible	= "microchip,mcp25625",
12828c2ecf20Sopenharmony_ci		.data		= (void *)CAN_MCP251X_MCP25625,
12838c2ecf20Sopenharmony_ci	},
12848c2ecf20Sopenharmony_ci	{ }
12858c2ecf20Sopenharmony_ci};
12868c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mcp251x_of_match);
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_cistatic const struct spi_device_id mcp251x_id_table[] = {
12898c2ecf20Sopenharmony_ci	{
12908c2ecf20Sopenharmony_ci		.name		= "mcp2510",
12918c2ecf20Sopenharmony_ci		.driver_data	= (kernel_ulong_t)CAN_MCP251X_MCP2510,
12928c2ecf20Sopenharmony_ci	},
12938c2ecf20Sopenharmony_ci	{
12948c2ecf20Sopenharmony_ci		.name		= "mcp2515",
12958c2ecf20Sopenharmony_ci		.driver_data	= (kernel_ulong_t)CAN_MCP251X_MCP2515,
12968c2ecf20Sopenharmony_ci	},
12978c2ecf20Sopenharmony_ci	{
12988c2ecf20Sopenharmony_ci		.name		= "mcp25625",
12998c2ecf20Sopenharmony_ci		.driver_data	= (kernel_ulong_t)CAN_MCP251X_MCP25625,
13008c2ecf20Sopenharmony_ci	},
13018c2ecf20Sopenharmony_ci	{ }
13028c2ecf20Sopenharmony_ci};
13038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, mcp251x_id_table);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_cistatic int mcp251x_can_probe(struct spi_device *spi)
13068c2ecf20Sopenharmony_ci{
13078c2ecf20Sopenharmony_ci	const void *match = device_get_match_data(&spi->dev);
13088c2ecf20Sopenharmony_ci	struct net_device *net;
13098c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv;
13108c2ecf20Sopenharmony_ci	struct clk *clk;
13118c2ecf20Sopenharmony_ci	u32 freq;
13128c2ecf20Sopenharmony_ci	int ret;
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	clk = devm_clk_get_optional(&spi->dev, NULL);
13158c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
13168c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	freq = clk_get_rate(clk);
13198c2ecf20Sopenharmony_ci	if (freq == 0)
13208c2ecf20Sopenharmony_ci		device_property_read_u32(&spi->dev, "clock-frequency", &freq);
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	/* Sanity check */
13238c2ecf20Sopenharmony_ci	if (freq < 1000000 || freq > 25000000)
13248c2ecf20Sopenharmony_ci		return -ERANGE;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	/* Allocate can/net device */
13278c2ecf20Sopenharmony_ci	net = alloc_candev(sizeof(struct mcp251x_priv), TX_ECHO_SKB_MAX);
13288c2ecf20Sopenharmony_ci	if (!net)
13298c2ecf20Sopenharmony_ci		return -ENOMEM;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(clk);
13328c2ecf20Sopenharmony_ci	if (ret)
13338c2ecf20Sopenharmony_ci		goto out_free;
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	net->netdev_ops = &mcp251x_netdev_ops;
13368c2ecf20Sopenharmony_ci	net->flags |= IFF_ECHO;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	priv = netdev_priv(net);
13398c2ecf20Sopenharmony_ci	priv->can.bittiming_const = &mcp251x_bittiming_const;
13408c2ecf20Sopenharmony_ci	priv->can.do_set_mode = mcp251x_do_set_mode;
13418c2ecf20Sopenharmony_ci	priv->can.clock.freq = freq / 2;
13428c2ecf20Sopenharmony_ci	priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
13438c2ecf20Sopenharmony_ci		CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY;
13448c2ecf20Sopenharmony_ci	if (match)
13458c2ecf20Sopenharmony_ci		priv->model = (enum mcp251x_model)match;
13468c2ecf20Sopenharmony_ci	else
13478c2ecf20Sopenharmony_ci		priv->model = spi_get_device_id(spi)->driver_data;
13488c2ecf20Sopenharmony_ci	priv->net = net;
13498c2ecf20Sopenharmony_ci	priv->clk = clk;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, priv);
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	/* Configure the SPI bus */
13548c2ecf20Sopenharmony_ci	spi->bits_per_word = 8;
13558c2ecf20Sopenharmony_ci	if (mcp251x_is_2510(spi))
13568c2ecf20Sopenharmony_ci		spi->max_speed_hz = spi->max_speed_hz ? : 5 * 1000 * 1000;
13578c2ecf20Sopenharmony_ci	else
13588c2ecf20Sopenharmony_ci		spi->max_speed_hz = spi->max_speed_hz ? : 10 * 1000 * 1000;
13598c2ecf20Sopenharmony_ci	ret = spi_setup(spi);
13608c2ecf20Sopenharmony_ci	if (ret)
13618c2ecf20Sopenharmony_ci		goto out_clk;
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	priv->power = devm_regulator_get_optional(&spi->dev, "vdd");
13648c2ecf20Sopenharmony_ci	priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver");
13658c2ecf20Sopenharmony_ci	if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
13668c2ecf20Sopenharmony_ci	    (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
13678c2ecf20Sopenharmony_ci		ret = -EPROBE_DEFER;
13688c2ecf20Sopenharmony_ci		goto out_clk;
13698c2ecf20Sopenharmony_ci	}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	ret = mcp251x_power_enable(priv->power, 1);
13728c2ecf20Sopenharmony_ci	if (ret)
13738c2ecf20Sopenharmony_ci		goto out_clk;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	priv->wq = alloc_workqueue("mcp251x_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM,
13768c2ecf20Sopenharmony_ci				   0);
13778c2ecf20Sopenharmony_ci	if (!priv->wq) {
13788c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13798c2ecf20Sopenharmony_ci		goto out_clk;
13808c2ecf20Sopenharmony_ci	}
13818c2ecf20Sopenharmony_ci	INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler);
13828c2ecf20Sopenharmony_ci	INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler);
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci	priv->spi = spi;
13858c2ecf20Sopenharmony_ci	mutex_init(&priv->mcp_lock);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
13888c2ecf20Sopenharmony_ci					GFP_KERNEL);
13898c2ecf20Sopenharmony_ci	if (!priv->spi_tx_buf) {
13908c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13918c2ecf20Sopenharmony_ci		goto error_probe;
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
13958c2ecf20Sopenharmony_ci					GFP_KERNEL);
13968c2ecf20Sopenharmony_ci	if (!priv->spi_rx_buf) {
13978c2ecf20Sopenharmony_ci		ret = -ENOMEM;
13988c2ecf20Sopenharmony_ci		goto error_probe;
13998c2ecf20Sopenharmony_ci	}
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(net, &spi->dev);
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	/* Here is OK to not lock the MCP, no one knows about it yet */
14048c2ecf20Sopenharmony_ci	ret = mcp251x_hw_probe(spi);
14058c2ecf20Sopenharmony_ci	if (ret) {
14068c2ecf20Sopenharmony_ci		if (ret == -ENODEV)
14078c2ecf20Sopenharmony_ci			dev_err(&spi->dev, "Cannot initialize MCP%x. Wrong wiring?\n",
14088c2ecf20Sopenharmony_ci				priv->model);
14098c2ecf20Sopenharmony_ci		goto error_probe;
14108c2ecf20Sopenharmony_ci	}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	mcp251x_hw_sleep(spi);
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	ret = register_candev(net);
14158c2ecf20Sopenharmony_ci	if (ret)
14168c2ecf20Sopenharmony_ci		goto error_probe;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	devm_can_led_init(net);
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	ret = mcp251x_gpio_setup(priv);
14218c2ecf20Sopenharmony_ci	if (ret)
14228c2ecf20Sopenharmony_ci		goto out_unregister_candev;
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ci	netdev_info(net, "MCP%x successfully initialized.\n", priv->model);
14258c2ecf20Sopenharmony_ci	return 0;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ciout_unregister_candev:
14288c2ecf20Sopenharmony_ci	unregister_candev(net);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_cierror_probe:
14318c2ecf20Sopenharmony_ci	destroy_workqueue(priv->wq);
14328c2ecf20Sopenharmony_ci	priv->wq = NULL;
14338c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->power, 0);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ciout_clk:
14368c2ecf20Sopenharmony_ci	clk_disable_unprepare(clk);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ciout_free:
14398c2ecf20Sopenharmony_ci	free_candev(net);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	dev_err(&spi->dev, "Probe failed, err=%d\n", -ret);
14428c2ecf20Sopenharmony_ci	return ret;
14438c2ecf20Sopenharmony_ci}
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic int mcp251x_can_remove(struct spi_device *spi)
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
14488c2ecf20Sopenharmony_ci	struct net_device *net = priv->net;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	unregister_candev(net);
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->power, 0);
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci	destroy_workqueue(priv->wq);
14558c2ecf20Sopenharmony_ci	priv->wq = NULL;
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	clk_disable_unprepare(priv->clk);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	free_candev(net);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	return 0;
14628c2ecf20Sopenharmony_ci}
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_cistatic int __maybe_unused mcp251x_can_suspend(struct device *dev)
14658c2ecf20Sopenharmony_ci{
14668c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
14678c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
14688c2ecf20Sopenharmony_ci	struct net_device *net = priv->net;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	priv->force_quit = 1;
14718c2ecf20Sopenharmony_ci	disable_irq(spi->irq);
14728c2ecf20Sopenharmony_ci	/* Note: at this point neither IST nor workqueues are running.
14738c2ecf20Sopenharmony_ci	 * open/stop cannot be called anyway so locking is not needed
14748c2ecf20Sopenharmony_ci	 */
14758c2ecf20Sopenharmony_ci	if (netif_running(net)) {
14768c2ecf20Sopenharmony_ci		netif_device_detach(net);
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci		mcp251x_hw_sleep(spi);
14798c2ecf20Sopenharmony_ci		mcp251x_power_enable(priv->transceiver, 0);
14808c2ecf20Sopenharmony_ci		priv->after_suspend = AFTER_SUSPEND_UP;
14818c2ecf20Sopenharmony_ci	} else {
14828c2ecf20Sopenharmony_ci		priv->after_suspend = AFTER_SUSPEND_DOWN;
14838c2ecf20Sopenharmony_ci	}
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	mcp251x_power_enable(priv->power, 0);
14868c2ecf20Sopenharmony_ci	priv->after_suspend |= AFTER_SUSPEND_POWER;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	return 0;
14898c2ecf20Sopenharmony_ci}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_cistatic int __maybe_unused mcp251x_can_resume(struct device *dev)
14928c2ecf20Sopenharmony_ci{
14938c2ecf20Sopenharmony_ci	struct spi_device *spi = to_spi_device(dev);
14948c2ecf20Sopenharmony_ci	struct mcp251x_priv *priv = spi_get_drvdata(spi);
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	if (priv->after_suspend & AFTER_SUSPEND_POWER)
14978c2ecf20Sopenharmony_ci		mcp251x_power_enable(priv->power, 1);
14988c2ecf20Sopenharmony_ci	if (priv->after_suspend & AFTER_SUSPEND_UP)
14998c2ecf20Sopenharmony_ci		mcp251x_power_enable(priv->transceiver, 1);
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	if (priv->after_suspend & (AFTER_SUSPEND_POWER | AFTER_SUSPEND_UP))
15028c2ecf20Sopenharmony_ci		queue_work(priv->wq, &priv->restart_work);
15038c2ecf20Sopenharmony_ci	else
15048c2ecf20Sopenharmony_ci		priv->after_suspend = 0;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	priv->force_quit = 0;
15078c2ecf20Sopenharmony_ci	enable_irq(spi->irq);
15088c2ecf20Sopenharmony_ci	return 0;
15098c2ecf20Sopenharmony_ci}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(mcp251x_can_pm_ops, mcp251x_can_suspend,
15128c2ecf20Sopenharmony_ci	mcp251x_can_resume);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_cistatic struct spi_driver mcp251x_can_driver = {
15158c2ecf20Sopenharmony_ci	.driver = {
15168c2ecf20Sopenharmony_ci		.name = DEVICE_NAME,
15178c2ecf20Sopenharmony_ci		.of_match_table = mcp251x_of_match,
15188c2ecf20Sopenharmony_ci		.pm = &mcp251x_can_pm_ops,
15198c2ecf20Sopenharmony_ci	},
15208c2ecf20Sopenharmony_ci	.id_table = mcp251x_id_table,
15218c2ecf20Sopenharmony_ci	.probe = mcp251x_can_probe,
15228c2ecf20Sopenharmony_ci	.remove = mcp251x_can_remove,
15238c2ecf20Sopenharmony_ci};
15248c2ecf20Sopenharmony_cimodule_spi_driver(mcp251x_can_driver);
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chris Elston <celston@katalix.com>, "
15278c2ecf20Sopenharmony_ci	      "Christian Pellegrin <chripell@evolware.org>");
15288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Microchip 251x/25625 CAN driver");
15298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1530