18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Analog Devices ADF7242 Low-Power IEEE 802.15.4 Transceiver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2009-2017 Analog Devices Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * https://www.analog.com/ADF7242
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
168c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
178c2ecf20Sopenharmony_ci#include <linux/firmware.h>
188c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
198c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
208c2ecf20Sopenharmony_ci#include <linux/of.h>
218c2ecf20Sopenharmony_ci#include <linux/irq.h>
228c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
238c2ecf20Sopenharmony_ci#include <linux/bitops.h>
248c2ecf20Sopenharmony_ci#include <linux/ieee802154.h>
258c2ecf20Sopenharmony_ci#include <net/mac802154.h>
268c2ecf20Sopenharmony_ci#include <net/cfg802154.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define FIRMWARE "adf7242_firmware.bin"
298c2ecf20Sopenharmony_ci#define MAX_POLL_LOOPS 200
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* All Registers */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define REG_EXT_CTRL	0x100	/* RW External LNA/PA and internal PA control */
348c2ecf20Sopenharmony_ci#define REG_TX_FSK_TEST 0x101	/* RW TX FSK test mode configuration */
358c2ecf20Sopenharmony_ci#define REG_CCA1	0x105	/* RW RSSI threshold for CCA */
368c2ecf20Sopenharmony_ci#define REG_CCA2	0x106	/* RW CCA mode configuration */
378c2ecf20Sopenharmony_ci#define REG_BUFFERCFG	0x107	/* RW RX_BUFFER overwrite control */
388c2ecf20Sopenharmony_ci#define REG_PKT_CFG	0x108	/* RW FCS evaluation configuration */
398c2ecf20Sopenharmony_ci#define REG_DELAYCFG0	0x109	/* RW RC_RX command to SFD or sync word delay */
408c2ecf20Sopenharmony_ci#define REG_DELAYCFG1	0x10A	/* RW RC_TX command to TX state */
418c2ecf20Sopenharmony_ci#define REG_DELAYCFG2	0x10B	/* RW Mac delay extension */
428c2ecf20Sopenharmony_ci#define REG_SYNC_WORD0	0x10C	/* RW sync word bits [7:0] of [23:0]  */
438c2ecf20Sopenharmony_ci#define REG_SYNC_WORD1	0x10D	/* RW sync word bits [15:8] of [23:0]  */
448c2ecf20Sopenharmony_ci#define REG_SYNC_WORD2	0x10E	/* RW sync word bits [23:16] of [23:0]	*/
458c2ecf20Sopenharmony_ci#define REG_SYNC_CONFIG	0x10F	/* RW sync word configuration */
468c2ecf20Sopenharmony_ci#define REG_RC_CFG	0x13E	/* RW RX / TX packet configuration */
478c2ecf20Sopenharmony_ci#define REG_RC_VAR44	0x13F	/* RW RESERVED */
488c2ecf20Sopenharmony_ci#define REG_CH_FREQ0	0x300	/* RW Channel Frequency Settings - Low */
498c2ecf20Sopenharmony_ci#define REG_CH_FREQ1	0x301	/* RW Channel Frequency Settings - Middle */
508c2ecf20Sopenharmony_ci#define REG_CH_FREQ2	0x302	/* RW Channel Frequency Settings - High */
518c2ecf20Sopenharmony_ci#define REG_TX_FD	0x304	/* RW TX Frequency Deviation Register */
528c2ecf20Sopenharmony_ci#define REG_DM_CFG0	0x305	/* RW RX Discriminator BW Register */
538c2ecf20Sopenharmony_ci#define REG_TX_M	0x306	/* RW TX Mode Register */
548c2ecf20Sopenharmony_ci#define REG_RX_M	0x307	/* RW RX Mode Register */
558c2ecf20Sopenharmony_ci#define REG_RRB		0x30C	/* R RSSI Readback Register */
568c2ecf20Sopenharmony_ci#define REG_LRB		0x30D	/* R Link Quality Readback Register */
578c2ecf20Sopenharmony_ci#define REG_DR0		0x30E	/* RW bits [15:8] of [15:0] data rate setting */
588c2ecf20Sopenharmony_ci#define REG_DR1		0x30F	/* RW bits [7:0] of [15:0] data rate setting */
598c2ecf20Sopenharmony_ci#define REG_PRAMPG	0x313	/* RW RESERVED */
608c2ecf20Sopenharmony_ci#define REG_TXPB	0x314	/* RW TX Packet Storage Base Address */
618c2ecf20Sopenharmony_ci#define REG_RXPB	0x315	/* RW RX Packet Storage Base Address */
628c2ecf20Sopenharmony_ci#define REG_TMR_CFG0	0x316	/* RW Wake up Timer Conf Register - High */
638c2ecf20Sopenharmony_ci#define REG_TMR_CFG1	0x317	/* RW Wake up Timer Conf Register - Low */
648c2ecf20Sopenharmony_ci#define REG_TMR_RLD0	0x318	/* RW Wake up Timer Value Register - High */
658c2ecf20Sopenharmony_ci#define REG_TMR_RLD1	0x319	/* RW Wake up Timer Value Register - Low  */
668c2ecf20Sopenharmony_ci#define REG_TMR_CTRL	0x31A	/* RW Wake up Timer Timeout flag */
678c2ecf20Sopenharmony_ci#define REG_PD_AUX	0x31E	/* RW Battmon enable */
688c2ecf20Sopenharmony_ci#define REG_GP_CFG	0x32C	/* RW GPIO Configuration */
698c2ecf20Sopenharmony_ci#define REG_GP_OUT	0x32D	/* RW GPIO Configuration */
708c2ecf20Sopenharmony_ci#define REG_GP_IN	0x32E	/* R GPIO Configuration */
718c2ecf20Sopenharmony_ci#define REG_SYNT	0x335	/* RW bandwidth calibration timers */
728c2ecf20Sopenharmony_ci#define REG_CAL_CFG	0x33D	/* RW Calibration Settings */
738c2ecf20Sopenharmony_ci#define REG_PA_BIAS	0x36E	/* RW PA BIAS */
748c2ecf20Sopenharmony_ci#define REG_SYNT_CAL	0x371	/* RW Oscillator and Doubler Configuration */
758c2ecf20Sopenharmony_ci#define REG_IIRF_CFG	0x389	/* RW BB Filter Decimation Rate */
768c2ecf20Sopenharmony_ci#define REG_CDR_CFG	0x38A	/* RW CDR kVCO */
778c2ecf20Sopenharmony_ci#define REG_DM_CFG1	0x38B	/* RW Postdemodulator Filter */
788c2ecf20Sopenharmony_ci#define REG_AGCSTAT	0x38E	/* R RXBB Ref Osc Calibration Engine Readback */
798c2ecf20Sopenharmony_ci#define REG_RXCAL0	0x395	/* RW RX BB filter tuning, LSB */
808c2ecf20Sopenharmony_ci#define REG_RXCAL1	0x396	/* RW RX BB filter tuning, MSB */
818c2ecf20Sopenharmony_ci#define REG_RXFE_CFG	0x39B	/* RW RXBB Ref Osc & RXFE Calibration */
828c2ecf20Sopenharmony_ci#define REG_PA_RR	0x3A7	/* RW Set PA ramp rate */
838c2ecf20Sopenharmony_ci#define REG_PA_CFG	0x3A8	/* RW PA enable */
848c2ecf20Sopenharmony_ci#define REG_EXTPA_CFG	0x3A9	/* RW External PA BIAS DAC */
858c2ecf20Sopenharmony_ci#define REG_EXTPA_MSC	0x3AA	/* RW PA Bias Mode */
868c2ecf20Sopenharmony_ci#define REG_ADC_RBK	0x3AE	/* R Readback temp */
878c2ecf20Sopenharmony_ci#define REG_AGC_CFG1	0x3B2	/* RW GC Parameters */
888c2ecf20Sopenharmony_ci#define REG_AGC_MAX	0x3B4	/* RW Slew rate	 */
898c2ecf20Sopenharmony_ci#define REG_AGC_CFG2	0x3B6	/* RW RSSI Parameters */
908c2ecf20Sopenharmony_ci#define REG_AGC_CFG3	0x3B7	/* RW RSSI Parameters */
918c2ecf20Sopenharmony_ci#define REG_AGC_CFG4	0x3B8	/* RW RSSI Parameters */
928c2ecf20Sopenharmony_ci#define REG_AGC_CFG5	0x3B9	/* RW RSSI & NDEC Parameters */
938c2ecf20Sopenharmony_ci#define REG_AGC_CFG6	0x3BA	/* RW NDEC Parameters */
948c2ecf20Sopenharmony_ci#define REG_OCL_CFG1	0x3C4	/* RW OCL System Parameters */
958c2ecf20Sopenharmony_ci#define REG_IRQ1_EN0	0x3C7	/* RW Interrupt Mask set bits for IRQ1 */
968c2ecf20Sopenharmony_ci#define REG_IRQ1_EN1	0x3C8	/* RW Interrupt Mask set bits for IRQ1 */
978c2ecf20Sopenharmony_ci#define REG_IRQ2_EN0	0x3C9	/* RW Interrupt Mask set bits for IRQ2 */
988c2ecf20Sopenharmony_ci#define REG_IRQ2_EN1	0x3CA	/* RW Interrupt Mask set bits for IRQ2 */
998c2ecf20Sopenharmony_ci#define REG_IRQ1_SRC0	0x3CB	/* RW Interrupt Source bits for IRQ */
1008c2ecf20Sopenharmony_ci#define REG_IRQ1_SRC1	0x3CC	/* RW Interrupt Source bits for IRQ */
1018c2ecf20Sopenharmony_ci#define REG_OCL_BW0	0x3D2	/* RW OCL System Parameters */
1028c2ecf20Sopenharmony_ci#define REG_OCL_BW1	0x3D3	/* RW OCL System Parameters */
1038c2ecf20Sopenharmony_ci#define REG_OCL_BW2	0x3D4	/* RW OCL System Parameters */
1048c2ecf20Sopenharmony_ci#define REG_OCL_BW3	0x3D5	/* RW OCL System Parameters */
1058c2ecf20Sopenharmony_ci#define REG_OCL_BW4	0x3D6	/* RW OCL System Parameters */
1068c2ecf20Sopenharmony_ci#define REG_OCL_BWS	0x3D7	/* RW OCL System Parameters */
1078c2ecf20Sopenharmony_ci#define REG_OCL_CFG13	0x3E0	/* RW OCL System Parameters */
1088c2ecf20Sopenharmony_ci#define REG_GP_DRV	0x3E3	/* RW I/O pads Configuration and bg trim */
1098c2ecf20Sopenharmony_ci#define REG_BM_CFG	0x3E6	/* RW Batt. Monitor Threshold Voltage setting */
1108c2ecf20Sopenharmony_ci#define REG_SFD_15_4	0x3F4	/* RW Option to set non standard SFD */
1118c2ecf20Sopenharmony_ci#define REG_AFC_CFG	0x3F7	/* RW AFC mode and polarity */
1128c2ecf20Sopenharmony_ci#define REG_AFC_KI_KP	0x3F8	/* RW AFC ki and kp */
1138c2ecf20Sopenharmony_ci#define REG_AFC_RANGE	0x3F9	/* RW AFC range */
1148c2ecf20Sopenharmony_ci#define REG_AFC_READ	0x3FA	/* RW Readback frequency error */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* REG_EXTPA_MSC */
1178c2ecf20Sopenharmony_ci#define PA_PWR(x)		(((x) & 0xF) << 4)
1188c2ecf20Sopenharmony_ci#define EXTPA_BIAS_SRC		BIT(3)
1198c2ecf20Sopenharmony_ci#define EXTPA_BIAS_MODE(x)	(((x) & 0x7) << 0)
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/* REG_PA_CFG */
1228c2ecf20Sopenharmony_ci#define PA_BRIDGE_DBIAS(x)	(((x) & 0x1F) << 0)
1238c2ecf20Sopenharmony_ci#define PA_DBIAS_HIGH_POWER	21
1248c2ecf20Sopenharmony_ci#define PA_DBIAS_LOW_POWER	13
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* REG_PA_BIAS */
1278c2ecf20Sopenharmony_ci#define PA_BIAS_CTRL(x)		(((x) & 0x1F) << 1)
1288c2ecf20Sopenharmony_ci#define REG_PA_BIAS_DFL		BIT(0)
1298c2ecf20Sopenharmony_ci#define PA_BIAS_HIGH_POWER	63
1308c2ecf20Sopenharmony_ci#define PA_BIAS_LOW_POWER	55
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define REG_PAN_ID0		0x112
1338c2ecf20Sopenharmony_ci#define REG_PAN_ID1		0x113
1348c2ecf20Sopenharmony_ci#define REG_SHORT_ADDR_0	0x114
1358c2ecf20Sopenharmony_ci#define REG_SHORT_ADDR_1	0x115
1368c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_0		0x116
1378c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_1		0x117
1388c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_2		0x118
1398c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_3		0x119
1408c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_4		0x11A
1418c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_5		0x11B
1428c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_6		0x11C
1438c2ecf20Sopenharmony_ci#define REG_IEEE_ADDR_7		0x11D
1448c2ecf20Sopenharmony_ci#define REG_FFILT_CFG		0x11E
1458c2ecf20Sopenharmony_ci#define REG_AUTO_CFG		0x11F
1468c2ecf20Sopenharmony_ci#define REG_AUTO_TX1		0x120
1478c2ecf20Sopenharmony_ci#define REG_AUTO_TX2		0x121
1488c2ecf20Sopenharmony_ci#define REG_AUTO_STATUS		0x122
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* REG_FFILT_CFG */
1518c2ecf20Sopenharmony_ci#define ACCEPT_BEACON_FRAMES   BIT(0)
1528c2ecf20Sopenharmony_ci#define ACCEPT_DATA_FRAMES     BIT(1)
1538c2ecf20Sopenharmony_ci#define ACCEPT_ACK_FRAMES      BIT(2)
1548c2ecf20Sopenharmony_ci#define ACCEPT_MACCMD_FRAMES   BIT(3)
1558c2ecf20Sopenharmony_ci#define ACCEPT_RESERVED_FRAMES BIT(4)
1568c2ecf20Sopenharmony_ci#define ACCEPT_ALL_ADDRESS     BIT(5)
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* REG_AUTO_CFG */
1598c2ecf20Sopenharmony_ci#define AUTO_ACK_FRAMEPEND     BIT(0)
1608c2ecf20Sopenharmony_ci#define IS_PANCOORD	       BIT(1)
1618c2ecf20Sopenharmony_ci#define RX_AUTO_ACK_EN	       BIT(3)
1628c2ecf20Sopenharmony_ci#define CSMA_CA_RX_TURNAROUND  BIT(4)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* REG_AUTO_TX1 */
1658c2ecf20Sopenharmony_ci#define MAX_FRAME_RETRIES(x)   ((x) & 0xF)
1668c2ecf20Sopenharmony_ci#define MAX_CCA_RETRIES(x)     (((x) & 0x7) << 4)
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/* REG_AUTO_TX2 */
1698c2ecf20Sopenharmony_ci#define CSMA_MAX_BE(x)	       ((x) & 0xF)
1708c2ecf20Sopenharmony_ci#define CSMA_MIN_BE(x)	       (((x) & 0xF) << 4)
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define CMD_SPI_NOP		0xFF /* No operation. Use for dummy writes */
1738c2ecf20Sopenharmony_ci#define CMD_SPI_PKT_WR		0x10 /* Write telegram to the Packet RAM
1748c2ecf20Sopenharmony_ci				      * starting from the TX packet base address
1758c2ecf20Sopenharmony_ci				      * pointer tx_packet_base
1768c2ecf20Sopenharmony_ci				      */
1778c2ecf20Sopenharmony_ci#define CMD_SPI_PKT_RD		0x30 /* Read telegram from the Packet RAM
1788c2ecf20Sopenharmony_ci				      * starting from RX packet base address
1798c2ecf20Sopenharmony_ci				      * pointer rxpb.rx_packet_base
1808c2ecf20Sopenharmony_ci				      */
1818c2ecf20Sopenharmony_ci#define CMD_SPI_MEM_WR(x)	(0x18 + (x >> 8)) /* Write data to MCR or
1828c2ecf20Sopenharmony_ci						   * Packet RAM sequentially
1838c2ecf20Sopenharmony_ci						   */
1848c2ecf20Sopenharmony_ci#define CMD_SPI_MEM_RD(x)	(0x38 + (x >> 8)) /* Read data from MCR or
1858c2ecf20Sopenharmony_ci						   * Packet RAM sequentially
1868c2ecf20Sopenharmony_ci						   */
1878c2ecf20Sopenharmony_ci#define CMD_SPI_MEMR_WR(x)	(0x08 + (x >> 8)) /* Write data to MCR or Packet
1888c2ecf20Sopenharmony_ci						   * RAM as random block
1898c2ecf20Sopenharmony_ci						   */
1908c2ecf20Sopenharmony_ci#define CMD_SPI_MEMR_RD(x)	(0x28 + (x >> 8)) /* Read data from MCR or
1918c2ecf20Sopenharmony_ci						   * Packet RAM random block
1928c2ecf20Sopenharmony_ci						   */
1938c2ecf20Sopenharmony_ci#define CMD_SPI_PRAM_WR		0x1E /* Write data sequentially to current
1948c2ecf20Sopenharmony_ci				      * PRAM page selected
1958c2ecf20Sopenharmony_ci				      */
1968c2ecf20Sopenharmony_ci#define CMD_SPI_PRAM_RD		0x3E /* Read data sequentially from current
1978c2ecf20Sopenharmony_ci				      * PRAM page selected
1988c2ecf20Sopenharmony_ci				      */
1998c2ecf20Sopenharmony_ci#define CMD_RC_SLEEP		0xB1 /* Invoke transition of radio controller
2008c2ecf20Sopenharmony_ci				      * into SLEEP state
2018c2ecf20Sopenharmony_ci				      */
2028c2ecf20Sopenharmony_ci#define CMD_RC_IDLE		0xB2 /* Invoke transition of radio controller
2038c2ecf20Sopenharmony_ci				      * into IDLE state
2048c2ecf20Sopenharmony_ci				      */
2058c2ecf20Sopenharmony_ci#define CMD_RC_PHY_RDY		0xB3 /* Invoke transition of radio controller
2068c2ecf20Sopenharmony_ci				      * into PHY_RDY state
2078c2ecf20Sopenharmony_ci				      */
2088c2ecf20Sopenharmony_ci#define CMD_RC_RX		0xB4 /* Invoke transition of radio controller
2098c2ecf20Sopenharmony_ci				      * into RX state
2108c2ecf20Sopenharmony_ci				      */
2118c2ecf20Sopenharmony_ci#define CMD_RC_TX		0xB5 /* Invoke transition of radio controller
2128c2ecf20Sopenharmony_ci				      * into TX state
2138c2ecf20Sopenharmony_ci				      */
2148c2ecf20Sopenharmony_ci#define CMD_RC_MEAS		0xB6 /* Invoke transition of radio controller
2158c2ecf20Sopenharmony_ci				      * into MEAS state
2168c2ecf20Sopenharmony_ci				      */
2178c2ecf20Sopenharmony_ci#define CMD_RC_CCA		0xB7 /* Invoke Clear channel assessment */
2188c2ecf20Sopenharmony_ci#define CMD_RC_CSMACA		0xC1 /* initiates CSMA-CA channel access
2198c2ecf20Sopenharmony_ci				      * sequence and frame transmission
2208c2ecf20Sopenharmony_ci				      */
2218c2ecf20Sopenharmony_ci#define CMD_RC_PC_RESET		0xC7 /* Program counter reset */
2228c2ecf20Sopenharmony_ci#define CMD_RC_RESET		0xC8 /* Resets the ADF7242 and puts it in
2238c2ecf20Sopenharmony_ci				      * the sleep state
2248c2ecf20Sopenharmony_ci				      */
2258c2ecf20Sopenharmony_ci#define CMD_RC_PC_RESET_NO_WAIT (CMD_RC_PC_RESET | BIT(31))
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/* STATUS */
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#define STAT_SPI_READY		BIT(7)
2308c2ecf20Sopenharmony_ci#define STAT_IRQ_STATUS		BIT(6)
2318c2ecf20Sopenharmony_ci#define STAT_RC_READY		BIT(5)
2328c2ecf20Sopenharmony_ci#define STAT_CCA_RESULT		BIT(4)
2338c2ecf20Sopenharmony_ci#define RC_STATUS_IDLE		1
2348c2ecf20Sopenharmony_ci#define RC_STATUS_MEAS		2
2358c2ecf20Sopenharmony_ci#define RC_STATUS_PHY_RDY	3
2368c2ecf20Sopenharmony_ci#define RC_STATUS_RX		4
2378c2ecf20Sopenharmony_ci#define RC_STATUS_TX		5
2388c2ecf20Sopenharmony_ci#define RC_STATUS_MASK		0xF
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/* AUTO_STATUS */
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci#define SUCCESS			0
2438c2ecf20Sopenharmony_ci#define SUCCESS_DATPEND		1
2448c2ecf20Sopenharmony_ci#define FAILURE_CSMACA		2
2458c2ecf20Sopenharmony_ci#define FAILURE_NOACK		3
2468c2ecf20Sopenharmony_ci#define AUTO_STATUS_MASK	0x3
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci#define PRAM_PAGESIZE		256
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* IRQ1 */
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci#define IRQ_CCA_COMPLETE	BIT(0)
2538c2ecf20Sopenharmony_ci#define IRQ_SFD_RX		BIT(1)
2548c2ecf20Sopenharmony_ci#define IRQ_SFD_TX		BIT(2)
2558c2ecf20Sopenharmony_ci#define IRQ_RX_PKT_RCVD		BIT(3)
2568c2ecf20Sopenharmony_ci#define IRQ_TX_PKT_SENT		BIT(4)
2578c2ecf20Sopenharmony_ci#define IRQ_FRAME_VALID		BIT(5)
2588c2ecf20Sopenharmony_ci#define IRQ_ADDRESS_VALID	BIT(6)
2598c2ecf20Sopenharmony_ci#define IRQ_CSMA_CA		BIT(7)
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#define AUTO_TX_TURNAROUND	BIT(3)
2628c2ecf20Sopenharmony_ci#define ADDON_EN		BIT(4)
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci#define FLAG_XMIT		0
2658c2ecf20Sopenharmony_ci#define FLAG_START		1
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci#define ADF7242_REPORT_CSMA_CA_STAT 0 /* framework doesn't handle yet */
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistruct adf7242_local {
2708c2ecf20Sopenharmony_ci	struct spi_device *spi;
2718c2ecf20Sopenharmony_ci	struct completion tx_complete;
2728c2ecf20Sopenharmony_ci	struct ieee802154_hw *hw;
2738c2ecf20Sopenharmony_ci	struct mutex bmux; /* protect SPI messages */
2748c2ecf20Sopenharmony_ci	struct spi_message stat_msg;
2758c2ecf20Sopenharmony_ci	struct spi_transfer stat_xfer;
2768c2ecf20Sopenharmony_ci	struct dentry *debugfs_root;
2778c2ecf20Sopenharmony_ci	struct delayed_work work;
2788c2ecf20Sopenharmony_ci	struct workqueue_struct *wqueue;
2798c2ecf20Sopenharmony_ci	unsigned long flags;
2808c2ecf20Sopenharmony_ci	int tx_stat;
2818c2ecf20Sopenharmony_ci	bool promiscuous;
2828c2ecf20Sopenharmony_ci	s8 rssi;
2838c2ecf20Sopenharmony_ci	u8 max_frame_retries;
2848c2ecf20Sopenharmony_ci	u8 max_cca_retries;
2858c2ecf20Sopenharmony_ci	u8 max_be;
2868c2ecf20Sopenharmony_ci	u8 min_be;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* DMA (thus cache coherency maintenance) requires the
2898c2ecf20Sopenharmony_ci	 * transfer buffers to live in their own cache lines.
2908c2ecf20Sopenharmony_ci	 */
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	u8 buf[3] ____cacheline_aligned;
2938c2ecf20Sopenharmony_ci	u8 buf_reg_tx[3];
2948c2ecf20Sopenharmony_ci	u8 buf_read_tx[4];
2958c2ecf20Sopenharmony_ci	u8 buf_read_rx[4];
2968c2ecf20Sopenharmony_ci	u8 buf_stat_rx;
2978c2ecf20Sopenharmony_ci	u8 buf_stat_tx;
2988c2ecf20Sopenharmony_ci	u8 buf_cmd;
2998c2ecf20Sopenharmony_ci};
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int adf7242_soft_reset(struct adf7242_local *lp, int line);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic int adf7242_status(struct adf7242_local *lp, u8 *stat)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	int status;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
3088c2ecf20Sopenharmony_ci	status = spi_sync(lp->spi, &lp->stat_msg);
3098c2ecf20Sopenharmony_ci	*stat = lp->buf_stat_rx;
3108c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return status;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int adf7242_wait_status(struct adf7242_local *lp, unsigned int status,
3168c2ecf20Sopenharmony_ci			       unsigned int mask, int line)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	int cnt = 0, ret = 0;
3198c2ecf20Sopenharmony_ci	u8 stat;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	do {
3228c2ecf20Sopenharmony_ci		adf7242_status(lp, &stat);
3238c2ecf20Sopenharmony_ci		cnt++;
3248c2ecf20Sopenharmony_ci	} while (((stat & mask) != status) && (cnt < MAX_POLL_LOOPS));
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (cnt >= MAX_POLL_LOOPS) {
3278c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci		if (!(stat & STAT_RC_READY)) {
3308c2ecf20Sopenharmony_ci			adf7242_soft_reset(lp, line);
3318c2ecf20Sopenharmony_ci			adf7242_status(lp, &stat);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci			if ((stat & mask) == status)
3348c2ecf20Sopenharmony_ci				ret = 0;
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		if (ret < 0)
3388c2ecf20Sopenharmony_ci			dev_warn(&lp->spi->dev,
3398c2ecf20Sopenharmony_ci				 "%s:line %d Timeout status 0x%x (%d)\n",
3408c2ecf20Sopenharmony_ci				 __func__, line, stat, cnt);
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : loops=%d line %d\n", __func__, cnt, line);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	return ret;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic int adf7242_wait_rc_ready(struct adf7242_local *lp, int line)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	return adf7242_wait_status(lp, STAT_RC_READY | STAT_SPI_READY,
3518c2ecf20Sopenharmony_ci				   STAT_RC_READY | STAT_SPI_READY, line);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic int adf7242_wait_spi_ready(struct adf7242_local *lp, int line)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	return adf7242_wait_status(lp, STAT_SPI_READY,
3578c2ecf20Sopenharmony_ci				   STAT_SPI_READY, line);
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic int adf7242_write_fbuf(struct adf7242_local *lp, u8 *data, u8 len)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	u8 *buf = lp->buf;
3638c2ecf20Sopenharmony_ci	int status;
3648c2ecf20Sopenharmony_ci	struct spi_message msg;
3658c2ecf20Sopenharmony_ci	struct spi_transfer xfer_head = {
3668c2ecf20Sopenharmony_ci		.len = 2,
3678c2ecf20Sopenharmony_ci		.tx_buf = buf,
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	};
3708c2ecf20Sopenharmony_ci	struct spi_transfer xfer_buf = {
3718c2ecf20Sopenharmony_ci		.len = len,
3728c2ecf20Sopenharmony_ci		.tx_buf = data,
3738c2ecf20Sopenharmony_ci	};
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	spi_message_init(&msg);
3768c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_head, &msg);
3778c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_buf, &msg);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	adf7242_wait_spi_ready(lp, __LINE__);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
3828c2ecf20Sopenharmony_ci	buf[0] = CMD_SPI_PKT_WR;
3838c2ecf20Sopenharmony_ci	buf[1] = len + 2;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	status = spi_sync(lp->spi, &msg);
3868c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return status;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int adf7242_read_fbuf(struct adf7242_local *lp,
3928c2ecf20Sopenharmony_ci			     u8 *data, size_t len, bool packet_read)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	u8 *buf = lp->buf;
3958c2ecf20Sopenharmony_ci	int status;
3968c2ecf20Sopenharmony_ci	struct spi_message msg;
3978c2ecf20Sopenharmony_ci	struct spi_transfer xfer_head = {
3988c2ecf20Sopenharmony_ci		.len = 3,
3998c2ecf20Sopenharmony_ci		.tx_buf = buf,
4008c2ecf20Sopenharmony_ci		.rx_buf = buf,
4018c2ecf20Sopenharmony_ci	};
4028c2ecf20Sopenharmony_ci	struct spi_transfer xfer_buf = {
4038c2ecf20Sopenharmony_ci		.len = len,
4048c2ecf20Sopenharmony_ci		.rx_buf = data,
4058c2ecf20Sopenharmony_ci	};
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	spi_message_init(&msg);
4088c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_head, &msg);
4098c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_buf, &msg);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	adf7242_wait_spi_ready(lp, __LINE__);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
4148c2ecf20Sopenharmony_ci	if (packet_read) {
4158c2ecf20Sopenharmony_ci		buf[0] = CMD_SPI_PKT_RD;
4168c2ecf20Sopenharmony_ci		buf[1] = CMD_SPI_NOP;
4178c2ecf20Sopenharmony_ci		buf[2] = 0;	/* PHR */
4188c2ecf20Sopenharmony_ci	} else {
4198c2ecf20Sopenharmony_ci		buf[0] = CMD_SPI_PRAM_RD;
4208c2ecf20Sopenharmony_ci		buf[1] = 0;
4218c2ecf20Sopenharmony_ci		buf[2] = CMD_SPI_NOP;
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	status = spi_sync(lp->spi, &msg);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	return status;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic int adf7242_read_reg(struct adf7242_local *lp, u16 addr, u8 *data)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	int status;
4348c2ecf20Sopenharmony_ci	struct spi_message msg;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	struct spi_transfer xfer = {
4378c2ecf20Sopenharmony_ci		.len = 4,
4388c2ecf20Sopenharmony_ci		.tx_buf = lp->buf_read_tx,
4398c2ecf20Sopenharmony_ci		.rx_buf = lp->buf_read_rx,
4408c2ecf20Sopenharmony_ci	};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	adf7242_wait_spi_ready(lp, __LINE__);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
4458c2ecf20Sopenharmony_ci	lp->buf_read_tx[0] = CMD_SPI_MEM_RD(addr);
4468c2ecf20Sopenharmony_ci	lp->buf_read_tx[1] = addr;
4478c2ecf20Sopenharmony_ci	lp->buf_read_tx[2] = CMD_SPI_NOP;
4488c2ecf20Sopenharmony_ci	lp->buf_read_tx[3] = CMD_SPI_NOP;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	spi_message_init(&msg);
4518c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer, &msg);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	status = spi_sync(lp->spi, &msg);
4548c2ecf20Sopenharmony_ci	if (msg.status)
4558c2ecf20Sopenharmony_ci		status = msg.status;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (!status)
4588c2ecf20Sopenharmony_ci		*data = lp->buf_read_rx[3];
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : REG 0x%X, VAL 0x%X\n", __func__,
4638c2ecf20Sopenharmony_ci		 addr, *data);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	return status;
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic int adf7242_write_reg(struct adf7242_local *lp, u16 addr, u8 data)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	int status;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	adf7242_wait_spi_ready(lp, __LINE__);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
4758c2ecf20Sopenharmony_ci	lp->buf_reg_tx[0] = CMD_SPI_MEM_WR(addr);
4768c2ecf20Sopenharmony_ci	lp->buf_reg_tx[1] = addr;
4778c2ecf20Sopenharmony_ci	lp->buf_reg_tx[2] = data;
4788c2ecf20Sopenharmony_ci	status = spi_write(lp->spi, lp->buf_reg_tx, 3);
4798c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : REG 0x%X, VAL 0x%X\n",
4828c2ecf20Sopenharmony_ci		 __func__, addr, data);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	return status;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int adf7242_cmd(struct adf7242_local *lp, unsigned int cmd)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	int status;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : CMD=0x%X\n", __func__, cmd);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	if (cmd != CMD_RC_PC_RESET_NO_WAIT)
4948c2ecf20Sopenharmony_ci		adf7242_wait_rc_ready(lp, __LINE__);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	mutex_lock(&lp->bmux);
4978c2ecf20Sopenharmony_ci	lp->buf_cmd = cmd;
4988c2ecf20Sopenharmony_ci	status = spi_write(lp->spi, &lp->buf_cmd, 1);
4998c2ecf20Sopenharmony_ci	mutex_unlock(&lp->bmux);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return status;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic int adf7242_upload_firmware(struct adf7242_local *lp, u8 *data, u16 len)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct spi_message msg;
5078c2ecf20Sopenharmony_ci	struct spi_transfer xfer_buf = { };
5088c2ecf20Sopenharmony_ci	int status, i, page = 0;
5098c2ecf20Sopenharmony_ci	u8 *buf = lp->buf;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	struct spi_transfer xfer_head = {
5128c2ecf20Sopenharmony_ci		.len = 2,
5138c2ecf20Sopenharmony_ci		.tx_buf = buf,
5148c2ecf20Sopenharmony_ci	};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	buf[0] = CMD_SPI_PRAM_WR;
5178c2ecf20Sopenharmony_ci	buf[1] = 0;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	spi_message_init(&msg);
5208c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_head, &msg);
5218c2ecf20Sopenharmony_ci	spi_message_add_tail(&xfer_buf, &msg);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	for (i = len; i >= 0; i -= PRAM_PAGESIZE) {
5248c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_PRAMPG, page);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		xfer_buf.len = (i >= PRAM_PAGESIZE) ? PRAM_PAGESIZE : i;
5278c2ecf20Sopenharmony_ci		xfer_buf.tx_buf = &data[page * PRAM_PAGESIZE];
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		mutex_lock(&lp->bmux);
5308c2ecf20Sopenharmony_ci		status = spi_sync(lp->spi, &msg);
5318c2ecf20Sopenharmony_ci		mutex_unlock(&lp->bmux);
5328c2ecf20Sopenharmony_ci		page++;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return status;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int adf7242_verify_firmware(struct adf7242_local *lp,
5398c2ecf20Sopenharmony_ci				   const u8 *data, size_t len)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci#ifdef DEBUG
5428c2ecf20Sopenharmony_ci	int i, j;
5438c2ecf20Sopenharmony_ci	unsigned int page;
5448c2ecf20Sopenharmony_ci	u8 *buf = kmalloc(PRAM_PAGESIZE, GFP_KERNEL);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	if (!buf)
5478c2ecf20Sopenharmony_ci		return -ENOMEM;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	for (page = 0, i = len; i >= 0; i -= PRAM_PAGESIZE, page++) {
5508c2ecf20Sopenharmony_ci		size_t nb = (i >= PRAM_PAGESIZE) ? PRAM_PAGESIZE : i;
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_PRAMPG, page);
5538c2ecf20Sopenharmony_ci		adf7242_read_fbuf(lp, buf, nb, false);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci		for (j = 0; j < nb; j++) {
5568c2ecf20Sopenharmony_ci			if (buf[j] != data[page * PRAM_PAGESIZE + j]) {
5578c2ecf20Sopenharmony_ci				kfree(buf);
5588c2ecf20Sopenharmony_ci				return -EIO;
5598c2ecf20Sopenharmony_ci			}
5608c2ecf20Sopenharmony_ci		}
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci	kfree(buf);
5638c2ecf20Sopenharmony_ci#endif
5648c2ecf20Sopenharmony_ci	return 0;
5658c2ecf20Sopenharmony_ci}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_cistatic void adf7242_clear_irqstat(struct adf7242_local *lp)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_IRQ1_SRC1, IRQ_CCA_COMPLETE | IRQ_SFD_RX |
5708c2ecf20Sopenharmony_ci			  IRQ_SFD_TX | IRQ_RX_PKT_RCVD | IRQ_TX_PKT_SENT |
5718c2ecf20Sopenharmony_ci			  IRQ_FRAME_VALID | IRQ_ADDRESS_VALID | IRQ_CSMA_CA);
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic int adf7242_cmd_rx(struct adf7242_local *lp)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	/* Wait until the ACK is sent */
5778c2ecf20Sopenharmony_ci	adf7242_wait_status(lp, RC_STATUS_PHY_RDY, RC_STATUS_MASK, __LINE__);
5788c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
5798c2ecf20Sopenharmony_ci	mod_delayed_work(lp->wqueue, &lp->work, msecs_to_jiffies(400));
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return adf7242_cmd(lp, CMD_RC_RX);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic void adf7242_rx_cal_work(struct work_struct *work)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct adf7242_local *lp =
5878c2ecf20Sopenharmony_ci	container_of(work, struct adf7242_local, work.work);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	/* Reissuing RC_RX every 400ms - to adjust for offset
5908c2ecf20Sopenharmony_ci	 * drift in receiver (datasheet page 61, OCL section)
5918c2ecf20Sopenharmony_ci	 */
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	if (!test_bit(FLAG_XMIT, &lp->flags)) {
5948c2ecf20Sopenharmony_ci		adf7242_cmd(lp, CMD_RC_PHY_RDY);
5958c2ecf20Sopenharmony_ci		adf7242_cmd_rx(lp);
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci}
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_cistatic int adf7242_set_txpower(struct ieee802154_hw *hw, int mbm)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
6028c2ecf20Sopenharmony_ci	u8 pwr, bias_ctrl, dbias, tmp;
6038c2ecf20Sopenharmony_ci	int db = mbm / 100;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : Power %d dB\n", __func__, db);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	if (db > 5 || db < -26)
6088c2ecf20Sopenharmony_ci		return -EINVAL;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	db = DIV_ROUND_CLOSEST(db + 29, 2);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (db > 15) {
6138c2ecf20Sopenharmony_ci		dbias = PA_DBIAS_HIGH_POWER;
6148c2ecf20Sopenharmony_ci		bias_ctrl = PA_BIAS_HIGH_POWER;
6158c2ecf20Sopenharmony_ci	} else {
6168c2ecf20Sopenharmony_ci		dbias = PA_DBIAS_LOW_POWER;
6178c2ecf20Sopenharmony_ci		bias_ctrl = PA_BIAS_LOW_POWER;
6188c2ecf20Sopenharmony_ci	}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	pwr = clamp_t(u8, db, 3, 15);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	adf7242_read_reg(lp, REG_PA_CFG, &tmp);
6238c2ecf20Sopenharmony_ci	tmp &= ~PA_BRIDGE_DBIAS(~0);
6248c2ecf20Sopenharmony_ci	tmp |= PA_BRIDGE_DBIAS(dbias);
6258c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_PA_CFG, tmp);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	adf7242_read_reg(lp, REG_PA_BIAS, &tmp);
6288c2ecf20Sopenharmony_ci	tmp &= ~PA_BIAS_CTRL(~0);
6298c2ecf20Sopenharmony_ci	tmp |= PA_BIAS_CTRL(bias_ctrl);
6308c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_PA_BIAS, tmp);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	adf7242_read_reg(lp, REG_EXTPA_MSC, &tmp);
6338c2ecf20Sopenharmony_ci	tmp &= ~PA_PWR(~0);
6348c2ecf20Sopenharmony_ci	tmp |= PA_PWR(pwr);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	return adf7242_write_reg(lp, REG_EXTPA_MSC, tmp);
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic int adf7242_set_csma_params(struct ieee802154_hw *hw, u8 min_be,
6408c2ecf20Sopenharmony_ci				   u8 max_be, u8 retries)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
6438c2ecf20Sopenharmony_ci	int ret;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : min_be=%d max_be=%d retries=%d\n",
6468c2ecf20Sopenharmony_ci		 __func__, min_be, max_be, retries);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (min_be > max_be || max_be > 8 || retries > 5)
6498c2ecf20Sopenharmony_ci		return -EINVAL;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	ret = adf7242_write_reg(lp, REG_AUTO_TX1,
6528c2ecf20Sopenharmony_ci				MAX_FRAME_RETRIES(lp->max_frame_retries) |
6538c2ecf20Sopenharmony_ci				MAX_CCA_RETRIES(retries));
6548c2ecf20Sopenharmony_ci	if (ret)
6558c2ecf20Sopenharmony_ci		return ret;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	lp->max_cca_retries = retries;
6588c2ecf20Sopenharmony_ci	lp->max_be = max_be;
6598c2ecf20Sopenharmony_ci	lp->min_be = min_be;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	return adf7242_write_reg(lp, REG_AUTO_TX2, CSMA_MAX_BE(max_be) |
6628c2ecf20Sopenharmony_ci			CSMA_MIN_BE(min_be));
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic int adf7242_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
6688c2ecf20Sopenharmony_ci	int ret = 0;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s : Retries = %d\n", __func__, retries);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	if (retries < -1 || retries > 15)
6738c2ecf20Sopenharmony_ci		return -EINVAL;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	if (retries >= 0)
6768c2ecf20Sopenharmony_ci		ret = adf7242_write_reg(lp, REG_AUTO_TX1,
6778c2ecf20Sopenharmony_ci					MAX_FRAME_RETRIES(retries) |
6788c2ecf20Sopenharmony_ci					MAX_CCA_RETRIES(lp->max_cca_retries));
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	lp->max_frame_retries = retries;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	return ret;
6838c2ecf20Sopenharmony_ci}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cistatic int adf7242_ed(struct ieee802154_hw *hw, u8 *level)
6868c2ecf20Sopenharmony_ci{
6878c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	*level = lp->rssi;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	dev_vdbg(&lp->spi->dev, "%s :Exit level=%d\n",
6928c2ecf20Sopenharmony_ci		 __func__, *level);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	return 0;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic int adf7242_start(struct ieee802154_hw *hw)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PHY_RDY);
7028c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
7038c2ecf20Sopenharmony_ci	enable_irq(lp->spi->irq);
7048c2ecf20Sopenharmony_ci	set_bit(FLAG_START, &lp->flags);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	return adf7242_cmd_rx(lp);
7078c2ecf20Sopenharmony_ci}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic void adf7242_stop(struct ieee802154_hw *hw)
7108c2ecf20Sopenharmony_ci{
7118c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	disable_irq(lp->spi->irq);
7148c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&lp->work);
7158c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_IDLE);
7168c2ecf20Sopenharmony_ci	clear_bit(FLAG_START, &lp->flags);
7178c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic int adf7242_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
7238c2ecf20Sopenharmony_ci	unsigned long freq;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s :Channel=%d\n", __func__, channel);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	might_sleep();
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	WARN_ON(page != 0);
7308c2ecf20Sopenharmony_ci	WARN_ON(channel < 11);
7318c2ecf20Sopenharmony_ci	WARN_ON(channel > 26);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	freq = (2405 + 5 * (channel - 11)) * 100;
7348c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PHY_RDY);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_CH_FREQ0, freq);
7378c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_CH_FREQ1, freq >> 8);
7388c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_CH_FREQ2, freq >> 16);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	if (test_bit(FLAG_START, &lp->flags))
7418c2ecf20Sopenharmony_ci		return adf7242_cmd_rx(lp);
7428c2ecf20Sopenharmony_ci	else
7438c2ecf20Sopenharmony_ci		return adf7242_cmd(lp, CMD_RC_PHY_RDY);
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic int adf7242_set_hw_addr_filt(struct ieee802154_hw *hw,
7478c2ecf20Sopenharmony_ci				    struct ieee802154_hw_addr_filt *filt,
7488c2ecf20Sopenharmony_ci				    unsigned long changed)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
7518c2ecf20Sopenharmony_ci	u8 reg;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s :Changed=0x%lX\n", __func__, changed);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	might_sleep();
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
7588c2ecf20Sopenharmony_ci		u8 addr[8], i;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci		memcpy(addr, &filt->ieee_addr, 8);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++)
7638c2ecf20Sopenharmony_ci			adf7242_write_reg(lp, REG_IEEE_ADDR_0 + i, addr[i]);
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
7678c2ecf20Sopenharmony_ci		u16 saddr = le16_to_cpu(filt->short_addr);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_SHORT_ADDR_0, saddr);
7708c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_SHORT_ADDR_1, saddr >> 8);
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_PANID_CHANGED) {
7748c2ecf20Sopenharmony_ci		u16 pan_id = le16_to_cpu(filt->pan_id);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_PAN_ID0, pan_id);
7778c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_PAN_ID1, pan_id >> 8);
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_PANC_CHANGED) {
7818c2ecf20Sopenharmony_ci		adf7242_read_reg(lp, REG_AUTO_CFG, &reg);
7828c2ecf20Sopenharmony_ci		if (filt->pan_coord)
7838c2ecf20Sopenharmony_ci			reg |= IS_PANCOORD;
7848c2ecf20Sopenharmony_ci		else
7858c2ecf20Sopenharmony_ci			reg &= ~IS_PANCOORD;
7868c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_AUTO_CFG, reg);
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	return 0;
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic int adf7242_set_promiscuous_mode(struct ieee802154_hw *hw, bool on)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s : mode %d\n", __func__, on);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	lp->promiscuous = on;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	if (on) {
8018c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_AUTO_CFG, 0);
8028c2ecf20Sopenharmony_ci		return adf7242_write_reg(lp, REG_FFILT_CFG,
8038c2ecf20Sopenharmony_ci				  ACCEPT_BEACON_FRAMES |
8048c2ecf20Sopenharmony_ci				  ACCEPT_DATA_FRAMES |
8058c2ecf20Sopenharmony_ci				  ACCEPT_MACCMD_FRAMES |
8068c2ecf20Sopenharmony_ci				  ACCEPT_ALL_ADDRESS |
8078c2ecf20Sopenharmony_ci				  ACCEPT_ACK_FRAMES |
8088c2ecf20Sopenharmony_ci				  ACCEPT_RESERVED_FRAMES);
8098c2ecf20Sopenharmony_ci	} else {
8108c2ecf20Sopenharmony_ci		adf7242_write_reg(lp, REG_FFILT_CFG,
8118c2ecf20Sopenharmony_ci				  ACCEPT_BEACON_FRAMES |
8128c2ecf20Sopenharmony_ci				  ACCEPT_DATA_FRAMES |
8138c2ecf20Sopenharmony_ci				  ACCEPT_MACCMD_FRAMES |
8148c2ecf20Sopenharmony_ci				  ACCEPT_RESERVED_FRAMES);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		return adf7242_write_reg(lp, REG_AUTO_CFG, RX_AUTO_ACK_EN);
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic int adf7242_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
8238c2ecf20Sopenharmony_ci	s8 level = clamp_t(s8, mbm / 100, S8_MIN, S8_MAX);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s : level %d\n", __func__, level);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	return adf7242_write_reg(lp, REG_CCA1, level);
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic int adf7242_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci	struct adf7242_local *lp = hw->priv;
8338c2ecf20Sopenharmony_ci	int ret;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	/* ensure existing instances of the IRQ handler have completed */
8368c2ecf20Sopenharmony_ci	disable_irq(lp->spi->irq);
8378c2ecf20Sopenharmony_ci	set_bit(FLAG_XMIT, &lp->flags);
8388c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&lp->work);
8398c2ecf20Sopenharmony_ci	reinit_completion(&lp->tx_complete);
8408c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PHY_RDY);
8418c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	ret = adf7242_write_fbuf(lp, skb->data, skb->len);
8448c2ecf20Sopenharmony_ci	if (ret)
8458c2ecf20Sopenharmony_ci		goto err;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	ret = adf7242_cmd(lp, CMD_RC_CSMACA);
8488c2ecf20Sopenharmony_ci	if (ret)
8498c2ecf20Sopenharmony_ci		goto err;
8508c2ecf20Sopenharmony_ci	enable_irq(lp->spi->irq);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	ret = wait_for_completion_interruptible_timeout(&lp->tx_complete,
8538c2ecf20Sopenharmony_ci							HZ / 10);
8548c2ecf20Sopenharmony_ci	if (ret < 0)
8558c2ecf20Sopenharmony_ci		goto err;
8568c2ecf20Sopenharmony_ci	if (ret == 0) {
8578c2ecf20Sopenharmony_ci		dev_dbg(&lp->spi->dev, "Timeout waiting for TX interrupt\n");
8588c2ecf20Sopenharmony_ci		ret = -ETIMEDOUT;
8598c2ecf20Sopenharmony_ci		goto err;
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	if (lp->tx_stat != SUCCESS) {
8638c2ecf20Sopenharmony_ci		dev_dbg(&lp->spi->dev,
8648c2ecf20Sopenharmony_ci			"Error xmit: Retry count exceeded Status=0x%x\n",
8658c2ecf20Sopenharmony_ci			lp->tx_stat);
8668c2ecf20Sopenharmony_ci		ret = -ECOMM;
8678c2ecf20Sopenharmony_ci	} else {
8688c2ecf20Sopenharmony_ci		ret = 0;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cierr:
8728c2ecf20Sopenharmony_ci	clear_bit(FLAG_XMIT, &lp->flags);
8738c2ecf20Sopenharmony_ci	adf7242_cmd_rx(lp);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	return ret;
8768c2ecf20Sopenharmony_ci}
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_cistatic int adf7242_rx(struct adf7242_local *lp)
8798c2ecf20Sopenharmony_ci{
8808c2ecf20Sopenharmony_ci	struct sk_buff *skb;
8818c2ecf20Sopenharmony_ci	size_t len;
8828c2ecf20Sopenharmony_ci	int ret;
8838c2ecf20Sopenharmony_ci	u8 lqi, len_u8, *data;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	ret = adf7242_read_reg(lp, 0, &len_u8);
8868c2ecf20Sopenharmony_ci	if (ret)
8878c2ecf20Sopenharmony_ci		return ret;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	len = len_u8;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	if (!ieee802154_is_valid_psdu_len(len)) {
8928c2ecf20Sopenharmony_ci		dev_dbg(&lp->spi->dev,
8938c2ecf20Sopenharmony_ci			"corrupted frame received len %d\n", (int)len);
8948c2ecf20Sopenharmony_ci		len = IEEE802154_MTU;
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(len);
8988c2ecf20Sopenharmony_ci	if (!skb) {
8998c2ecf20Sopenharmony_ci		adf7242_cmd_rx(lp);
9008c2ecf20Sopenharmony_ci		return -ENOMEM;
9018c2ecf20Sopenharmony_ci	}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	data = skb_put(skb, len);
9048c2ecf20Sopenharmony_ci	ret = adf7242_read_fbuf(lp, data, len, true);
9058c2ecf20Sopenharmony_ci	if (ret < 0) {
9068c2ecf20Sopenharmony_ci		kfree_skb(skb);
9078c2ecf20Sopenharmony_ci		adf7242_cmd_rx(lp);
9088c2ecf20Sopenharmony_ci		return ret;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	lqi = data[len - 2];
9128c2ecf20Sopenharmony_ci	lp->rssi = data[len - 1];
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	ret = adf7242_cmd_rx(lp);
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	skb_trim(skb, len - 2);	/* Don't put RSSI/LQI or CRC into the frame */
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	ieee802154_rx_irqsafe(lp->hw, skb, lqi);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s: ret=%d len=%d lqi=%d rssi=%d\n",
9218c2ecf20Sopenharmony_ci		__func__, ret, (int)len, (int)lqi, lp->rssi);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	return ret;
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic const struct ieee802154_ops adf7242_ops = {
9278c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
9288c2ecf20Sopenharmony_ci	.xmit_sync = adf7242_xmit,
9298c2ecf20Sopenharmony_ci	.ed = adf7242_ed,
9308c2ecf20Sopenharmony_ci	.set_channel = adf7242_channel,
9318c2ecf20Sopenharmony_ci	.set_hw_addr_filt = adf7242_set_hw_addr_filt,
9328c2ecf20Sopenharmony_ci	.start = adf7242_start,
9338c2ecf20Sopenharmony_ci	.stop = adf7242_stop,
9348c2ecf20Sopenharmony_ci	.set_csma_params = adf7242_set_csma_params,
9358c2ecf20Sopenharmony_ci	.set_frame_retries = adf7242_set_frame_retries,
9368c2ecf20Sopenharmony_ci	.set_txpower = adf7242_set_txpower,
9378c2ecf20Sopenharmony_ci	.set_promiscuous_mode = adf7242_set_promiscuous_mode,
9388c2ecf20Sopenharmony_ci	.set_cca_ed_level = adf7242_set_cca_ed_level,
9398c2ecf20Sopenharmony_ci};
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_cistatic void adf7242_debug(struct adf7242_local *lp, u8 irq1)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci#ifdef DEBUG
9448c2ecf20Sopenharmony_ci	u8 stat;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	adf7242_status(lp, &stat);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s IRQ1 = %X:\n%s%s%s%s%s%s%s%s\n",
9498c2ecf20Sopenharmony_ci		__func__, irq1,
9508c2ecf20Sopenharmony_ci		irq1 & IRQ_CCA_COMPLETE ? "IRQ_CCA_COMPLETE\n" : "",
9518c2ecf20Sopenharmony_ci		irq1 & IRQ_SFD_RX ? "IRQ_SFD_RX\n" : "",
9528c2ecf20Sopenharmony_ci		irq1 & IRQ_SFD_TX ? "IRQ_SFD_TX\n" : "",
9538c2ecf20Sopenharmony_ci		irq1 & IRQ_RX_PKT_RCVD ? "IRQ_RX_PKT_RCVD\n" : "",
9548c2ecf20Sopenharmony_ci		irq1 & IRQ_TX_PKT_SENT ? "IRQ_TX_PKT_SENT\n" : "",
9558c2ecf20Sopenharmony_ci		irq1 & IRQ_CSMA_CA ? "IRQ_CSMA_CA\n" : "",
9568c2ecf20Sopenharmony_ci		irq1 & IRQ_FRAME_VALID ? "IRQ_FRAME_VALID\n" : "",
9578c2ecf20Sopenharmony_ci		irq1 & IRQ_ADDRESS_VALID ? "IRQ_ADDRESS_VALID\n" : "");
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	dev_dbg(&lp->spi->dev, "%s STATUS = %X:\n%s\n%s\n%s\n%s\n%s%s%s%s%s\n",
9608c2ecf20Sopenharmony_ci		__func__, stat,
9618c2ecf20Sopenharmony_ci		stat & STAT_SPI_READY ? "SPI_READY" : "SPI_BUSY",
9628c2ecf20Sopenharmony_ci		stat & STAT_IRQ_STATUS ? "IRQ_PENDING" : "IRQ_CLEAR",
9638c2ecf20Sopenharmony_ci		stat & STAT_RC_READY ? "RC_READY" : "RC_BUSY",
9648c2ecf20Sopenharmony_ci		stat & STAT_CCA_RESULT ? "CHAN_IDLE" : "CHAN_BUSY",
9658c2ecf20Sopenharmony_ci		(stat & 0xf) == RC_STATUS_IDLE ? "RC_STATUS_IDLE" : "",
9668c2ecf20Sopenharmony_ci		(stat & 0xf) == RC_STATUS_MEAS ? "RC_STATUS_MEAS" : "",
9678c2ecf20Sopenharmony_ci		(stat & 0xf) == RC_STATUS_PHY_RDY ? "RC_STATUS_PHY_RDY" : "",
9688c2ecf20Sopenharmony_ci		(stat & 0xf) == RC_STATUS_RX ? "RC_STATUS_RX" : "",
9698c2ecf20Sopenharmony_ci		(stat & 0xf) == RC_STATUS_TX ? "RC_STATUS_TX" : "");
9708c2ecf20Sopenharmony_ci#endif
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic irqreturn_t adf7242_isr(int irq, void *data)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	struct adf7242_local *lp = data;
9768c2ecf20Sopenharmony_ci	unsigned int xmit;
9778c2ecf20Sopenharmony_ci	u8 irq1;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	mod_delayed_work(lp->wqueue, &lp->work, msecs_to_jiffies(400));
9808c2ecf20Sopenharmony_ci	adf7242_read_reg(lp, REG_IRQ1_SRC1, &irq1);
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	if (!(irq1 & (IRQ_RX_PKT_RCVD | IRQ_CSMA_CA)))
9838c2ecf20Sopenharmony_ci		dev_err(&lp->spi->dev, "%s :ERROR IRQ1 = 0x%X\n",
9848c2ecf20Sopenharmony_ci			__func__, irq1);
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	adf7242_debug(lp, irq1);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	xmit = test_bit(FLAG_XMIT, &lp->flags);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (xmit && (irq1 & IRQ_CSMA_CA)) {
9918c2ecf20Sopenharmony_ci		adf7242_wait_status(lp, RC_STATUS_PHY_RDY,
9928c2ecf20Sopenharmony_ci				    RC_STATUS_MASK, __LINE__);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci		if (ADF7242_REPORT_CSMA_CA_STAT) {
9958c2ecf20Sopenharmony_ci			u8 astat;
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci			adf7242_read_reg(lp, REG_AUTO_STATUS, &astat);
9988c2ecf20Sopenharmony_ci			astat &= AUTO_STATUS_MASK;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci			dev_dbg(&lp->spi->dev, "AUTO_STATUS = %X:\n%s%s%s%s\n",
10018c2ecf20Sopenharmony_ci				astat,
10028c2ecf20Sopenharmony_ci				astat == SUCCESS ? "SUCCESS" : "",
10038c2ecf20Sopenharmony_ci				astat ==
10048c2ecf20Sopenharmony_ci				SUCCESS_DATPEND ? "SUCCESS_DATPEND" : "",
10058c2ecf20Sopenharmony_ci				astat == FAILURE_CSMACA ? "FAILURE_CSMACA" : "",
10068c2ecf20Sopenharmony_ci				astat == FAILURE_NOACK ? "FAILURE_NOACK" : "");
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci			/* save CSMA-CA completion status */
10098c2ecf20Sopenharmony_ci			lp->tx_stat = astat;
10108c2ecf20Sopenharmony_ci		} else {
10118c2ecf20Sopenharmony_ci			lp->tx_stat = SUCCESS;
10128c2ecf20Sopenharmony_ci		}
10138c2ecf20Sopenharmony_ci		complete(&lp->tx_complete);
10148c2ecf20Sopenharmony_ci		adf7242_clear_irqstat(lp);
10158c2ecf20Sopenharmony_ci	} else if (!xmit && (irq1 & IRQ_RX_PKT_RCVD) &&
10168c2ecf20Sopenharmony_ci		   (irq1 & IRQ_FRAME_VALID)) {
10178c2ecf20Sopenharmony_ci		adf7242_rx(lp);
10188c2ecf20Sopenharmony_ci	} else if (!xmit && test_bit(FLAG_START, &lp->flags)) {
10198c2ecf20Sopenharmony_ci		/* Invalid packet received - drop it and restart */
10208c2ecf20Sopenharmony_ci		dev_dbg(&lp->spi->dev, "%s:%d : ERROR IRQ1 = 0x%X\n",
10218c2ecf20Sopenharmony_ci			__func__, __LINE__, irq1);
10228c2ecf20Sopenharmony_ci		adf7242_cmd(lp, CMD_RC_PHY_RDY);
10238c2ecf20Sopenharmony_ci		adf7242_cmd_rx(lp);
10248c2ecf20Sopenharmony_ci	} else {
10258c2ecf20Sopenharmony_ci		/* This can only be xmit without IRQ, likely a RX packet.
10268c2ecf20Sopenharmony_ci		 * we get an TX IRQ shortly - do nothing or let the xmit
10278c2ecf20Sopenharmony_ci		 * timeout handle this
10288c2ecf20Sopenharmony_ci		 */
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci		dev_dbg(&lp->spi->dev, "%s:%d : ERROR IRQ1 = 0x%X, xmit %d\n",
10318c2ecf20Sopenharmony_ci			__func__, __LINE__, irq1, xmit);
10328c2ecf20Sopenharmony_ci		adf7242_wait_status(lp, RC_STATUS_PHY_RDY,
10338c2ecf20Sopenharmony_ci				    RC_STATUS_MASK, __LINE__);
10348c2ecf20Sopenharmony_ci		complete(&lp->tx_complete);
10358c2ecf20Sopenharmony_ci		adf7242_clear_irqstat(lp);
10368c2ecf20Sopenharmony_ci	}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cistatic int adf7242_soft_reset(struct adf7242_local *lp, int line)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	dev_warn(&lp->spi->dev, "%s (line %d)\n", __func__, line);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	if (test_bit(FLAG_START, &lp->flags))
10468c2ecf20Sopenharmony_ci		disable_irq_nosync(lp->spi->irq);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PC_RESET_NO_WAIT);
10498c2ecf20Sopenharmony_ci	usleep_range(200, 250);
10508c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_PKT_CFG, ADDON_EN | BIT(2));
10518c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PHY_RDY);
10528c2ecf20Sopenharmony_ci	adf7242_set_promiscuous_mode(lp->hw, lp->promiscuous);
10538c2ecf20Sopenharmony_ci	adf7242_set_csma_params(lp->hw, lp->min_be, lp->max_be,
10548c2ecf20Sopenharmony_ci				lp->max_cca_retries);
10558c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	if (test_bit(FLAG_START, &lp->flags)) {
10588c2ecf20Sopenharmony_ci		enable_irq(lp->spi->irq);
10598c2ecf20Sopenharmony_ci		return adf7242_cmd(lp, CMD_RC_RX);
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	return 0;
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic int adf7242_hw_init(struct adf7242_local *lp)
10668c2ecf20Sopenharmony_ci{
10678c2ecf20Sopenharmony_ci	int ret;
10688c2ecf20Sopenharmony_ci	const struct firmware *fw;
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_RESET);
10718c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_IDLE);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	/* get ADF7242 addon firmware
10748c2ecf20Sopenharmony_ci	 * build this driver as module
10758c2ecf20Sopenharmony_ci	 * and place under /lib/firmware/adf7242_firmware.bin
10768c2ecf20Sopenharmony_ci	 * or compile firmware into the kernel.
10778c2ecf20Sopenharmony_ci	 */
10788c2ecf20Sopenharmony_ci	ret = request_firmware(&fw, FIRMWARE, &lp->spi->dev);
10798c2ecf20Sopenharmony_ci	if (ret) {
10808c2ecf20Sopenharmony_ci		dev_err(&lp->spi->dev,
10818c2ecf20Sopenharmony_ci			"request_firmware() failed with %d\n", ret);
10828c2ecf20Sopenharmony_ci		return ret;
10838c2ecf20Sopenharmony_ci	}
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	ret = adf7242_upload_firmware(lp, (u8 *)fw->data, fw->size);
10868c2ecf20Sopenharmony_ci	if (ret) {
10878c2ecf20Sopenharmony_ci		dev_err(&lp->spi->dev,
10888c2ecf20Sopenharmony_ci			"upload firmware failed with %d\n", ret);
10898c2ecf20Sopenharmony_ci		release_firmware(fw);
10908c2ecf20Sopenharmony_ci		return ret;
10918c2ecf20Sopenharmony_ci	}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	ret = adf7242_verify_firmware(lp, (u8 *)fw->data, fw->size);
10948c2ecf20Sopenharmony_ci	if (ret) {
10958c2ecf20Sopenharmony_ci		dev_err(&lp->spi->dev,
10968c2ecf20Sopenharmony_ci			"verify firmware failed with %d\n", ret);
10978c2ecf20Sopenharmony_ci		release_firmware(fw);
10988c2ecf20Sopenharmony_ci		return ret;
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_PC_RESET);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	release_firmware(fw);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_FFILT_CFG,
11068c2ecf20Sopenharmony_ci			  ACCEPT_BEACON_FRAMES |
11078c2ecf20Sopenharmony_ci			  ACCEPT_DATA_FRAMES |
11088c2ecf20Sopenharmony_ci			  ACCEPT_MACCMD_FRAMES |
11098c2ecf20Sopenharmony_ci			  ACCEPT_RESERVED_FRAMES);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_AUTO_CFG, RX_AUTO_ACK_EN);
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_PKT_CFG, ADDON_EN | BIT(2));
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_EXTPA_MSC, 0xF1);
11168c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_RXFE_CFG, 0x1D);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_IRQ1_EN0, 0);
11198c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_IRQ1_EN1, IRQ_RX_PKT_RCVD | IRQ_CSMA_CA);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	adf7242_clear_irqstat(lp);
11228c2ecf20Sopenharmony_ci	adf7242_write_reg(lp, REG_IRQ1_SRC0, 0xFF);
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	adf7242_cmd(lp, CMD_RC_IDLE);
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	return 0;
11278c2ecf20Sopenharmony_ci}
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_cistatic int adf7242_stats_show(struct seq_file *file, void *offset)
11308c2ecf20Sopenharmony_ci{
11318c2ecf20Sopenharmony_ci	struct adf7242_local *lp = spi_get_drvdata(file->private);
11328c2ecf20Sopenharmony_ci	u8 stat, irq1;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	adf7242_status(lp, &stat);
11358c2ecf20Sopenharmony_ci	adf7242_read_reg(lp, REG_IRQ1_SRC1, &irq1);
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	seq_printf(file, "IRQ1 = %X:\n%s%s%s%s%s%s%s%s\n", irq1,
11388c2ecf20Sopenharmony_ci		   irq1 & IRQ_CCA_COMPLETE ? "IRQ_CCA_COMPLETE\n" : "",
11398c2ecf20Sopenharmony_ci		   irq1 & IRQ_SFD_RX ? "IRQ_SFD_RX\n" : "",
11408c2ecf20Sopenharmony_ci		   irq1 & IRQ_SFD_TX ? "IRQ_SFD_TX\n" : "",
11418c2ecf20Sopenharmony_ci		   irq1 & IRQ_RX_PKT_RCVD ? "IRQ_RX_PKT_RCVD\n" : "",
11428c2ecf20Sopenharmony_ci		   irq1 & IRQ_TX_PKT_SENT ? "IRQ_TX_PKT_SENT\n" : "",
11438c2ecf20Sopenharmony_ci		   irq1 & IRQ_CSMA_CA ? "IRQ_CSMA_CA\n" : "",
11448c2ecf20Sopenharmony_ci		   irq1 & IRQ_FRAME_VALID ? "IRQ_FRAME_VALID\n" : "",
11458c2ecf20Sopenharmony_ci		   irq1 & IRQ_ADDRESS_VALID ? "IRQ_ADDRESS_VALID\n" : "");
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	seq_printf(file, "STATUS = %X:\n%s\n%s\n%s\n%s\n%s%s%s%s%s\n", stat,
11488c2ecf20Sopenharmony_ci		   stat & STAT_SPI_READY ? "SPI_READY" : "SPI_BUSY",
11498c2ecf20Sopenharmony_ci		   stat & STAT_IRQ_STATUS ? "IRQ_PENDING" : "IRQ_CLEAR",
11508c2ecf20Sopenharmony_ci		   stat & STAT_RC_READY ? "RC_READY" : "RC_BUSY",
11518c2ecf20Sopenharmony_ci		   stat & STAT_CCA_RESULT ? "CHAN_IDLE" : "CHAN_BUSY",
11528c2ecf20Sopenharmony_ci		   (stat & 0xf) == RC_STATUS_IDLE ? "RC_STATUS_IDLE" : "",
11538c2ecf20Sopenharmony_ci		   (stat & 0xf) == RC_STATUS_MEAS ? "RC_STATUS_MEAS" : "",
11548c2ecf20Sopenharmony_ci		   (stat & 0xf) == RC_STATUS_PHY_RDY ? "RC_STATUS_PHY_RDY" : "",
11558c2ecf20Sopenharmony_ci		   (stat & 0xf) == RC_STATUS_RX ? "RC_STATUS_RX" : "",
11568c2ecf20Sopenharmony_ci		   (stat & 0xf) == RC_STATUS_TX ? "RC_STATUS_TX" : "");
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	seq_printf(file, "RSSI = %d\n", lp->rssi);
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	return 0;
11618c2ecf20Sopenharmony_ci}
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_cistatic void adf7242_debugfs_init(struct adf7242_local *lp)
11648c2ecf20Sopenharmony_ci{
11658c2ecf20Sopenharmony_ci	char debugfs_dir_name[DNAME_INLINE_LEN + 1];
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	snprintf(debugfs_dir_name, sizeof(debugfs_dir_name),
11688c2ecf20Sopenharmony_ci		 "adf7242-%s", dev_name(&lp->spi->dev));
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	lp->debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	debugfs_create_devm_seqfile(&lp->spi->dev, "status", lp->debugfs_root,
11738c2ecf20Sopenharmony_ci				    adf7242_stats_show);
11748c2ecf20Sopenharmony_ci}
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_cistatic const s32 adf7242_powers[] = {
11778c2ecf20Sopenharmony_ci	500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
11788c2ecf20Sopenharmony_ci	-800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
11798c2ecf20Sopenharmony_ci	-1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
11808c2ecf20Sopenharmony_ci};
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic const s32 adf7242_ed_levels[] = {
11838c2ecf20Sopenharmony_ci	-9000, -8900, -8800, -8700, -8600, -8500, -8400, -8300, -8200, -8100,
11848c2ecf20Sopenharmony_ci	-8000, -7900, -7800, -7700, -7600, -7500, -7400, -7300, -7200, -7100,
11858c2ecf20Sopenharmony_ci	-7000, -6900, -6800, -6700, -6600, -6500, -6400, -6300, -6200, -6100,
11868c2ecf20Sopenharmony_ci	-6000, -5900, -5800, -5700, -5600, -5500, -5400, -5300, -5200, -5100,
11878c2ecf20Sopenharmony_ci	-5000, -4900, -4800, -4700, -4600, -4500, -4400, -4300, -4200, -4100,
11888c2ecf20Sopenharmony_ci	-4000, -3900, -3800, -3700, -3600, -3500, -3400, -3200, -3100, -3000
11898c2ecf20Sopenharmony_ci};
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_cistatic int adf7242_probe(struct spi_device *spi)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	struct ieee802154_hw *hw;
11948c2ecf20Sopenharmony_ci	struct adf7242_local *lp;
11958c2ecf20Sopenharmony_ci	int ret, irq_type;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	if (!spi->irq) {
11988c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "no IRQ specified\n");
11998c2ecf20Sopenharmony_ci		return -EINVAL;
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	hw = ieee802154_alloc_hw(sizeof(*lp), &adf7242_ops);
12038c2ecf20Sopenharmony_ci	if (!hw)
12048c2ecf20Sopenharmony_ci		return -ENOMEM;
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	lp = hw->priv;
12078c2ecf20Sopenharmony_ci	lp->hw = hw;
12088c2ecf20Sopenharmony_ci	lp->spi = spi;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	hw->priv = lp;
12118c2ecf20Sopenharmony_ci	hw->parent = &spi->dev;
12128c2ecf20Sopenharmony_ci	hw->extra_tx_headroom = 0;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	/* We support only 2.4 Ghz */
12158c2ecf20Sopenharmony_ci	hw->phy->supported.channels[0] = 0x7FFF800;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	hw->flags = IEEE802154_HW_OMIT_CKSUM |
12188c2ecf20Sopenharmony_ci		    IEEE802154_HW_CSMA_PARAMS |
12198c2ecf20Sopenharmony_ci		    IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
12208c2ecf20Sopenharmony_ci		    IEEE802154_HW_PROMISCUOUS;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
12238c2ecf20Sopenharmony_ci			 WPAN_PHY_FLAG_CCA_ED_LEVEL |
12248c2ecf20Sopenharmony_ci			 WPAN_PHY_FLAG_CCA_MODE;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY);
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	hw->phy->supported.cca_ed_levels = adf7242_ed_levels;
12298c2ecf20Sopenharmony_ci	hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(adf7242_ed_levels);
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	hw->phy->cca.mode = NL802154_CCA_ENERGY;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	hw->phy->supported.tx_powers = adf7242_powers;
12348c2ecf20Sopenharmony_ci	hw->phy->supported.tx_powers_size = ARRAY_SIZE(adf7242_powers);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	hw->phy->supported.min_minbe = 0;
12378c2ecf20Sopenharmony_ci	hw->phy->supported.max_minbe = 8;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	hw->phy->supported.min_maxbe = 3;
12408c2ecf20Sopenharmony_ci	hw->phy->supported.max_maxbe = 8;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	hw->phy->supported.min_frame_retries = 0;
12438c2ecf20Sopenharmony_ci	hw->phy->supported.max_frame_retries = 15;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	hw->phy->supported.min_csma_backoffs = 0;
12468c2ecf20Sopenharmony_ci	hw->phy->supported.max_csma_backoffs = 5;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	mutex_init(&lp->bmux);
12518c2ecf20Sopenharmony_ci	init_completion(&lp->tx_complete);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	/* Setup Status Message */
12548c2ecf20Sopenharmony_ci	lp->stat_xfer.len = 1;
12558c2ecf20Sopenharmony_ci	lp->stat_xfer.tx_buf = &lp->buf_stat_tx;
12568c2ecf20Sopenharmony_ci	lp->stat_xfer.rx_buf = &lp->buf_stat_rx;
12578c2ecf20Sopenharmony_ci	lp->buf_stat_tx = CMD_SPI_NOP;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	spi_message_init(&lp->stat_msg);
12608c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->stat_xfer, &lp->stat_msg);
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, lp);
12638c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&lp->work, adf7242_rx_cal_work);
12648c2ecf20Sopenharmony_ci	lp->wqueue = alloc_ordered_workqueue(dev_name(&spi->dev),
12658c2ecf20Sopenharmony_ci					     WQ_MEM_RECLAIM);
12668c2ecf20Sopenharmony_ci	if (unlikely(!lp->wqueue)) {
12678c2ecf20Sopenharmony_ci		ret = -ENOMEM;
12688c2ecf20Sopenharmony_ci		goto err_alloc_wq;
12698c2ecf20Sopenharmony_ci	}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	ret = adf7242_hw_init(lp);
12728c2ecf20Sopenharmony_ci	if (ret)
12738c2ecf20Sopenharmony_ci		goto err_hw_init;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	irq_type = irq_get_trigger_type(spi->irq);
12768c2ecf20Sopenharmony_ci	if (!irq_type)
12778c2ecf20Sopenharmony_ci		irq_type = IRQF_TRIGGER_HIGH;
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, adf7242_isr,
12808c2ecf20Sopenharmony_ci					irq_type | IRQF_ONESHOT,
12818c2ecf20Sopenharmony_ci					dev_name(&spi->dev), lp);
12828c2ecf20Sopenharmony_ci	if (ret)
12838c2ecf20Sopenharmony_ci		goto err_hw_init;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	disable_irq(spi->irq);
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	ret = ieee802154_register_hw(lp->hw);
12888c2ecf20Sopenharmony_ci	if (ret)
12898c2ecf20Sopenharmony_ci		goto err_hw_init;
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	dev_set_drvdata(&spi->dev, lp);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	adf7242_debugfs_init(lp);
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	dev_info(&spi->dev, "mac802154 IRQ-%d registered\n", spi->irq);
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci	return ret;
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_cierr_hw_init:
13008c2ecf20Sopenharmony_ci	destroy_workqueue(lp->wqueue);
13018c2ecf20Sopenharmony_cierr_alloc_wq:
13028c2ecf20Sopenharmony_ci	mutex_destroy(&lp->bmux);
13038c2ecf20Sopenharmony_ci	ieee802154_free_hw(lp->hw);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	return ret;
13068c2ecf20Sopenharmony_ci}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_cistatic int adf7242_remove(struct spi_device *spi)
13098c2ecf20Sopenharmony_ci{
13108c2ecf20Sopenharmony_ci	struct adf7242_local *lp = spi_get_drvdata(spi);
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	debugfs_remove_recursive(lp->debugfs_root);
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	ieee802154_unregister_hw(lp->hw);
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&lp->work);
13178c2ecf20Sopenharmony_ci	destroy_workqueue(lp->wqueue);
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	mutex_destroy(&lp->bmux);
13208c2ecf20Sopenharmony_ci	ieee802154_free_hw(lp->hw);
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	return 0;
13238c2ecf20Sopenharmony_ci}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_cistatic const struct of_device_id adf7242_of_match[] = {
13268c2ecf20Sopenharmony_ci	{ .compatible = "adi,adf7242", },
13278c2ecf20Sopenharmony_ci	{ .compatible = "adi,adf7241", },
13288c2ecf20Sopenharmony_ci	{ },
13298c2ecf20Sopenharmony_ci};
13308c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, adf7242_of_match);
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cistatic const struct spi_device_id adf7242_device_id[] = {
13338c2ecf20Sopenharmony_ci	{ .name = "adf7242", },
13348c2ecf20Sopenharmony_ci	{ .name = "adf7241", },
13358c2ecf20Sopenharmony_ci	{ },
13368c2ecf20Sopenharmony_ci};
13378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, adf7242_device_id);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_cistatic struct spi_driver adf7242_driver = {
13408c2ecf20Sopenharmony_ci	.id_table = adf7242_device_id,
13418c2ecf20Sopenharmony_ci	.driver = {
13428c2ecf20Sopenharmony_ci		   .of_match_table = of_match_ptr(adf7242_of_match),
13438c2ecf20Sopenharmony_ci		   .name = "adf7242",
13448c2ecf20Sopenharmony_ci		   .owner = THIS_MODULE,
13458c2ecf20Sopenharmony_ci		   },
13468c2ecf20Sopenharmony_ci	.probe = adf7242_probe,
13478c2ecf20Sopenharmony_ci	.remove = adf7242_remove,
13488c2ecf20Sopenharmony_ci};
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_cimodule_spi_driver(adf7242_driver);
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
13538c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ADF7242 IEEE802.15.4 Transceiver Driver");
13548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1355