18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for NXP MCR20A 802.15.4 Wireless-PAN Networking controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018 Xue Liu <liuxuenetmail@gmail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
108c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
118c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/irq.h>
148c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
158c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
168c2ecf20Sopenharmony_ci#include <linux/regmap.h>
178c2ecf20Sopenharmony_ci#include <linux/ieee802154.h>
188c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <net/mac802154.h>
218c2ecf20Sopenharmony_ci#include <net/cfg802154.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/device.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "mcr20a.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define	SPI_COMMAND_BUFFER		3
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define REGISTER_READ			BIT(7)
308c2ecf20Sopenharmony_ci#define REGISTER_WRITE			(0 << 7)
318c2ecf20Sopenharmony_ci#define REGISTER_ACCESS			(0 << 6)
328c2ecf20Sopenharmony_ci#define PACKET_BUFF_BURST_ACCESS	BIT(6)
338c2ecf20Sopenharmony_ci#define PACKET_BUFF_BYTE_ACCESS		BIT(5)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define MCR20A_WRITE_REG(x)		(x)
368c2ecf20Sopenharmony_ci#define MCR20A_READ_REG(x)		(REGISTER_READ | (x))
378c2ecf20Sopenharmony_ci#define MCR20A_BURST_READ_PACKET_BUF	(0xC0)
388c2ecf20Sopenharmony_ci#define MCR20A_BURST_WRITE_PACKET_BUF	(0x40)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define MCR20A_CMD_REG		0x80
418c2ecf20Sopenharmony_ci#define MCR20A_CMD_REG_MASK	0x3f
428c2ecf20Sopenharmony_ci#define MCR20A_CMD_WRITE	0x40
438c2ecf20Sopenharmony_ci#define MCR20A_CMD_FB		0x20
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* Number of Interrupt Request Status Register */
468c2ecf20Sopenharmony_ci#define MCR20A_IRQSTS_NUM 2 /* only IRQ_STS1 and IRQ_STS2 */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* MCR20A CCA Type */
498c2ecf20Sopenharmony_cienum {
508c2ecf20Sopenharmony_ci	MCR20A_CCA_ED,	  // energy detect - CCA bit not active,
518c2ecf20Sopenharmony_ci			  // not to be used for T and CCCA sequences
528c2ecf20Sopenharmony_ci	MCR20A_CCA_MODE1, // energy detect - CCA bit ACTIVE
538c2ecf20Sopenharmony_ci	MCR20A_CCA_MODE2, // 802.15.4 compliant signal detect - CCA bit ACTIVE
548c2ecf20Sopenharmony_ci	MCR20A_CCA_MODE3
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cienum {
588c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_IDLE	= 0x00,
598c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_RX	= 0x01,
608c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_TX	= 0x02,
618c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_CCA	= 0x03,
628c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_TR	= 0x04,
638c2ecf20Sopenharmony_ci	MCR20A_XCVSEQ_CCCA	= 0x05,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* IEEE-802.15.4 defined constants (2.4 GHz logical channels) */
678c2ecf20Sopenharmony_ci#define	MCR20A_MIN_CHANNEL	(11)
688c2ecf20Sopenharmony_ci#define	MCR20A_MAX_CHANNEL	(26)
698c2ecf20Sopenharmony_ci#define	MCR20A_CHANNEL_SPACING	(5)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* MCR20A CCA Threshold constans */
728c2ecf20Sopenharmony_ci#define MCR20A_MIN_CCA_THRESHOLD (0x6EU)
738c2ecf20Sopenharmony_ci#define MCR20A_MAX_CCA_THRESHOLD (0x00U)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* version 0C */
768c2ecf20Sopenharmony_ci#define MCR20A_OVERWRITE_VERSION (0x0C)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* MCR20A PLL configurations */
798c2ecf20Sopenharmony_cistatic const u8  PLL_INT[16] = {
808c2ecf20Sopenharmony_ci	/* 2405 */ 0x0B,	/* 2410 */ 0x0B,	/* 2415 */ 0x0B,
818c2ecf20Sopenharmony_ci	/* 2420 */ 0x0B,	/* 2425 */ 0x0B,	/* 2430 */ 0x0B,
828c2ecf20Sopenharmony_ci	/* 2435 */ 0x0C,	/* 2440 */ 0x0C,	/* 2445 */ 0x0C,
838c2ecf20Sopenharmony_ci	/* 2450 */ 0x0C,	/* 2455 */ 0x0C,	/* 2460 */ 0x0C,
848c2ecf20Sopenharmony_ci	/* 2465 */ 0x0D,	/* 2470 */ 0x0D,	/* 2475 */ 0x0D,
858c2ecf20Sopenharmony_ci	/* 2480 */ 0x0D
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const u8 PLL_FRAC[16] = {
898c2ecf20Sopenharmony_ci	/* 2405 */ 0x28,	/* 2410 */ 0x50,	/* 2415 */ 0x78,
908c2ecf20Sopenharmony_ci	/* 2420 */ 0xA0,	/* 2425 */ 0xC8,	/* 2430 */ 0xF0,
918c2ecf20Sopenharmony_ci	/* 2435 */ 0x18,	/* 2440 */ 0x40,	/* 2445 */ 0x68,
928c2ecf20Sopenharmony_ci	/* 2450 */ 0x90,	/* 2455 */ 0xB8,	/* 2460 */ 0xE0,
938c2ecf20Sopenharmony_ci	/* 2465 */ 0x08,	/* 2470 */ 0x30,	/* 2475 */ 0x58,
948c2ecf20Sopenharmony_ci	/* 2480 */ 0x80
958c2ecf20Sopenharmony_ci};
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic const struct reg_sequence mar20a_iar_overwrites[] = {
988c2ecf20Sopenharmony_ci	{ IAR_MISC_PAD_CTRL,	0x02 },
998c2ecf20Sopenharmony_ci	{ IAR_VCO_CTRL1,	0xB3 },
1008c2ecf20Sopenharmony_ci	{ IAR_VCO_CTRL2,	0x07 },
1018c2ecf20Sopenharmony_ci	{ IAR_PA_TUNING,	0x71 },
1028c2ecf20Sopenharmony_ci	{ IAR_CHF_IBUF,		0x2F },
1038c2ecf20Sopenharmony_ci	{ IAR_CHF_QBUF,		0x2F },
1048c2ecf20Sopenharmony_ci	{ IAR_CHF_IRIN,		0x24 },
1058c2ecf20Sopenharmony_ci	{ IAR_CHF_QRIN,		0x24 },
1068c2ecf20Sopenharmony_ci	{ IAR_CHF_IL,		0x24 },
1078c2ecf20Sopenharmony_ci	{ IAR_CHF_QL,		0x24 },
1088c2ecf20Sopenharmony_ci	{ IAR_CHF_CC1,		0x32 },
1098c2ecf20Sopenharmony_ci	{ IAR_CHF_CCL,		0x1D },
1108c2ecf20Sopenharmony_ci	{ IAR_CHF_CC2,		0x2D },
1118c2ecf20Sopenharmony_ci	{ IAR_CHF_IROUT,	0x24 },
1128c2ecf20Sopenharmony_ci	{ IAR_CHF_QROUT,	0x24 },
1138c2ecf20Sopenharmony_ci	{ IAR_PA_CAL,		0x28 },
1148c2ecf20Sopenharmony_ci	{ IAR_AGC_THR1,		0x55 },
1158c2ecf20Sopenharmony_ci	{ IAR_AGC_THR2,		0x2D },
1168c2ecf20Sopenharmony_ci	{ IAR_ATT_RSSI1,	0x5F },
1178c2ecf20Sopenharmony_ci	{ IAR_ATT_RSSI2,	0x8F },
1188c2ecf20Sopenharmony_ci	{ IAR_RSSI_OFFSET,	0x61 },
1198c2ecf20Sopenharmony_ci	{ IAR_CHF_PMA_GAIN,	0x03 },
1208c2ecf20Sopenharmony_ci	{ IAR_CCA1_THRESH,	0x50 },
1218c2ecf20Sopenharmony_ci	{ IAR_CORR_NVAL,	0x13 },
1228c2ecf20Sopenharmony_ci	{ IAR_ACKDELAY,		0x3D },
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define MCR20A_VALID_CHANNELS (0x07FFF800)
1268c2ecf20Sopenharmony_ci#define MCR20A_MAX_BUF		(127)
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define printdev(X) (&X->spi->dev)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/* regmap information for Direct Access Register (DAR) access */
1318c2ecf20Sopenharmony_ci#define MCR20A_DAR_WRITE	0x01
1328c2ecf20Sopenharmony_ci#define MCR20A_DAR_READ		0x00
1338c2ecf20Sopenharmony_ci#define MCR20A_DAR_NUMREGS	0x3F
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* regmap information for Indirect Access Register (IAR) access */
1368c2ecf20Sopenharmony_ci#define MCR20A_IAR_ACCESS	0x80
1378c2ecf20Sopenharmony_ci#define MCR20A_IAR_NUMREGS	0xBEFF
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* Read/Write SPI Commands for DAR and IAR registers. */
1408c2ecf20Sopenharmony_ci#define MCR20A_READSHORT(reg)	((reg) << 1)
1418c2ecf20Sopenharmony_ci#define MCR20A_WRITESHORT(reg)	((reg) << 1 | 1)
1428c2ecf20Sopenharmony_ci#define MCR20A_READLONG(reg)	(1 << 15 | (reg) << 5)
1438c2ecf20Sopenharmony_ci#define MCR20A_WRITELONG(reg)	(1 << 15 | (reg) << 5 | 1 << 4)
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* Type definitions for link configuration of instantiable layers  */
1468c2ecf20Sopenharmony_ci#define MCR20A_PHY_INDIRECT_QUEUE_SIZE (12)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic bool
1498c2ecf20Sopenharmony_cimcr20a_dar_writeable(struct device *dev, unsigned int reg)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	switch (reg) {
1528c2ecf20Sopenharmony_ci	case DAR_IRQ_STS1:
1538c2ecf20Sopenharmony_ci	case DAR_IRQ_STS2:
1548c2ecf20Sopenharmony_ci	case DAR_IRQ_STS3:
1558c2ecf20Sopenharmony_ci	case DAR_PHY_CTRL1:
1568c2ecf20Sopenharmony_ci	case DAR_PHY_CTRL2:
1578c2ecf20Sopenharmony_ci	case DAR_PHY_CTRL3:
1588c2ecf20Sopenharmony_ci	case DAR_PHY_CTRL4:
1598c2ecf20Sopenharmony_ci	case DAR_SRC_CTRL:
1608c2ecf20Sopenharmony_ci	case DAR_SRC_ADDRS_SUM_LSB:
1618c2ecf20Sopenharmony_ci	case DAR_SRC_ADDRS_SUM_MSB:
1628c2ecf20Sopenharmony_ci	case DAR_T3CMP_LSB:
1638c2ecf20Sopenharmony_ci	case DAR_T3CMP_MSB:
1648c2ecf20Sopenharmony_ci	case DAR_T3CMP_USB:
1658c2ecf20Sopenharmony_ci	case DAR_T2PRIMECMP_LSB:
1668c2ecf20Sopenharmony_ci	case DAR_T2PRIMECMP_MSB:
1678c2ecf20Sopenharmony_ci	case DAR_T1CMP_LSB:
1688c2ecf20Sopenharmony_ci	case DAR_T1CMP_MSB:
1698c2ecf20Sopenharmony_ci	case DAR_T1CMP_USB:
1708c2ecf20Sopenharmony_ci	case DAR_T2CMP_LSB:
1718c2ecf20Sopenharmony_ci	case DAR_T2CMP_MSB:
1728c2ecf20Sopenharmony_ci	case DAR_T2CMP_USB:
1738c2ecf20Sopenharmony_ci	case DAR_T4CMP_LSB:
1748c2ecf20Sopenharmony_ci	case DAR_T4CMP_MSB:
1758c2ecf20Sopenharmony_ci	case DAR_T4CMP_USB:
1768c2ecf20Sopenharmony_ci	case DAR_PLL_INT0:
1778c2ecf20Sopenharmony_ci	case DAR_PLL_FRAC0_LSB:
1788c2ecf20Sopenharmony_ci	case DAR_PLL_FRAC0_MSB:
1798c2ecf20Sopenharmony_ci	case DAR_PA_PWR:
1808c2ecf20Sopenharmony_ci	/* no DAR_ACM */
1818c2ecf20Sopenharmony_ci	case DAR_OVERWRITE_VER:
1828c2ecf20Sopenharmony_ci	case DAR_CLK_OUT_CTRL:
1838c2ecf20Sopenharmony_ci	case DAR_PWR_MODES:
1848c2ecf20Sopenharmony_ci		return true;
1858c2ecf20Sopenharmony_ci	default:
1868c2ecf20Sopenharmony_ci		return false;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic bool
1918c2ecf20Sopenharmony_cimcr20a_dar_readable(struct device *dev, unsigned int reg)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	bool rc;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	/* all writeable are also readable */
1968c2ecf20Sopenharmony_ci	rc = mcr20a_dar_writeable(dev, reg);
1978c2ecf20Sopenharmony_ci	if (rc)
1988c2ecf20Sopenharmony_ci		return rc;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* readonly regs */
2018c2ecf20Sopenharmony_ci	switch (reg) {
2028c2ecf20Sopenharmony_ci	case DAR_RX_FRM_LEN:
2038c2ecf20Sopenharmony_ci	case DAR_CCA1_ED_FNL:
2048c2ecf20Sopenharmony_ci	case DAR_EVENT_TMR_LSB:
2058c2ecf20Sopenharmony_ci	case DAR_EVENT_TMR_MSB:
2068c2ecf20Sopenharmony_ci	case DAR_EVENT_TMR_USB:
2078c2ecf20Sopenharmony_ci	case DAR_TIMESTAMP_LSB:
2088c2ecf20Sopenharmony_ci	case DAR_TIMESTAMP_MSB:
2098c2ecf20Sopenharmony_ci	case DAR_TIMESTAMP_USB:
2108c2ecf20Sopenharmony_ci	case DAR_SEQ_STATE:
2118c2ecf20Sopenharmony_ci	case DAR_LQI_VALUE:
2128c2ecf20Sopenharmony_ci	case DAR_RSSI_CCA_CONT:
2138c2ecf20Sopenharmony_ci		return true;
2148c2ecf20Sopenharmony_ci	default:
2158c2ecf20Sopenharmony_ci		return false;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic bool
2208c2ecf20Sopenharmony_cimcr20a_dar_volatile(struct device *dev, unsigned int reg)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	/* can be changed during runtime */
2238c2ecf20Sopenharmony_ci	switch (reg) {
2248c2ecf20Sopenharmony_ci	case DAR_IRQ_STS1:
2258c2ecf20Sopenharmony_ci	case DAR_IRQ_STS2:
2268c2ecf20Sopenharmony_ci	case DAR_IRQ_STS3:
2278c2ecf20Sopenharmony_ci	/* use them in spi_async and regmap so it's volatile */
2288c2ecf20Sopenharmony_ci		return true;
2298c2ecf20Sopenharmony_ci	default:
2308c2ecf20Sopenharmony_ci		return false;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic bool
2358c2ecf20Sopenharmony_cimcr20a_dar_precious(struct device *dev, unsigned int reg)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	/* don't clear irq line on read */
2388c2ecf20Sopenharmony_ci	switch (reg) {
2398c2ecf20Sopenharmony_ci	case DAR_IRQ_STS1:
2408c2ecf20Sopenharmony_ci	case DAR_IRQ_STS2:
2418c2ecf20Sopenharmony_ci	case DAR_IRQ_STS3:
2428c2ecf20Sopenharmony_ci		return true;
2438c2ecf20Sopenharmony_ci	default:
2448c2ecf20Sopenharmony_ci		return false;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic const struct regmap_config mcr20a_dar_regmap = {
2498c2ecf20Sopenharmony_ci	.name			= "mcr20a_dar",
2508c2ecf20Sopenharmony_ci	.reg_bits		= 8,
2518c2ecf20Sopenharmony_ci	.val_bits		= 8,
2528c2ecf20Sopenharmony_ci	.write_flag_mask	= REGISTER_ACCESS | REGISTER_WRITE,
2538c2ecf20Sopenharmony_ci	.read_flag_mask		= REGISTER_ACCESS | REGISTER_READ,
2548c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
2558c2ecf20Sopenharmony_ci	.writeable_reg		= mcr20a_dar_writeable,
2568c2ecf20Sopenharmony_ci	.readable_reg		= mcr20a_dar_readable,
2578c2ecf20Sopenharmony_ci	.volatile_reg		= mcr20a_dar_volatile,
2588c2ecf20Sopenharmony_ci	.precious_reg		= mcr20a_dar_precious,
2598c2ecf20Sopenharmony_ci	.fast_io		= true,
2608c2ecf20Sopenharmony_ci	.can_multi_write	= true,
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic bool
2648c2ecf20Sopenharmony_cimcr20a_iar_writeable(struct device *dev, unsigned int reg)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	switch (reg) {
2678c2ecf20Sopenharmony_ci	case IAR_XTAL_TRIM:
2688c2ecf20Sopenharmony_ci	case IAR_PMC_LP_TRIM:
2698c2ecf20Sopenharmony_ci	case IAR_MACPANID0_LSB:
2708c2ecf20Sopenharmony_ci	case IAR_MACPANID0_MSB:
2718c2ecf20Sopenharmony_ci	case IAR_MACSHORTADDRS0_LSB:
2728c2ecf20Sopenharmony_ci	case IAR_MACSHORTADDRS0_MSB:
2738c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_0:
2748c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_8:
2758c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_16:
2768c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_24:
2778c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_32:
2788c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_40:
2798c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_48:
2808c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS0_56:
2818c2ecf20Sopenharmony_ci	case IAR_RX_FRAME_FILTER:
2828c2ecf20Sopenharmony_ci	case IAR_PLL_INT1:
2838c2ecf20Sopenharmony_ci	case IAR_PLL_FRAC1_LSB:
2848c2ecf20Sopenharmony_ci	case IAR_PLL_FRAC1_MSB:
2858c2ecf20Sopenharmony_ci	case IAR_MACPANID1_LSB:
2868c2ecf20Sopenharmony_ci	case IAR_MACPANID1_MSB:
2878c2ecf20Sopenharmony_ci	case IAR_MACSHORTADDRS1_LSB:
2888c2ecf20Sopenharmony_ci	case IAR_MACSHORTADDRS1_MSB:
2898c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_0:
2908c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_8:
2918c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_16:
2928c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_24:
2938c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_32:
2948c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_40:
2958c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_48:
2968c2ecf20Sopenharmony_ci	case IAR_MACLONGADDRS1_56:
2978c2ecf20Sopenharmony_ci	case IAR_DUAL_PAN_CTRL:
2988c2ecf20Sopenharmony_ci	case IAR_DUAL_PAN_DWELL:
2998c2ecf20Sopenharmony_ci	case IAR_CCA1_THRESH:
3008c2ecf20Sopenharmony_ci	case IAR_CCA1_ED_OFFSET_COMP:
3018c2ecf20Sopenharmony_ci	case IAR_LQI_OFFSET_COMP:
3028c2ecf20Sopenharmony_ci	case IAR_CCA_CTRL:
3038c2ecf20Sopenharmony_ci	case IAR_CCA2_CORR_PEAKS:
3048c2ecf20Sopenharmony_ci	case IAR_CCA2_CORR_THRESH:
3058c2ecf20Sopenharmony_ci	case IAR_TMR_PRESCALE:
3068c2ecf20Sopenharmony_ci	case IAR_ANT_PAD_CTRL:
3078c2ecf20Sopenharmony_ci	case IAR_MISC_PAD_CTRL:
3088c2ecf20Sopenharmony_ci	case IAR_BSM_CTRL:
3098c2ecf20Sopenharmony_ci	case IAR_RNG:
3108c2ecf20Sopenharmony_ci	case IAR_RX_WTR_MARK:
3118c2ecf20Sopenharmony_ci	case IAR_SOFT_RESET:
3128c2ecf20Sopenharmony_ci	case IAR_TXDELAY:
3138c2ecf20Sopenharmony_ci	case IAR_ACKDELAY:
3148c2ecf20Sopenharmony_ci	case IAR_CORR_NVAL:
3158c2ecf20Sopenharmony_ci	case IAR_ANT_AGC_CTRL:
3168c2ecf20Sopenharmony_ci	case IAR_AGC_THR1:
3178c2ecf20Sopenharmony_ci	case IAR_AGC_THR2:
3188c2ecf20Sopenharmony_ci	case IAR_PA_CAL:
3198c2ecf20Sopenharmony_ci	case IAR_ATT_RSSI1:
3208c2ecf20Sopenharmony_ci	case IAR_ATT_RSSI2:
3218c2ecf20Sopenharmony_ci	case IAR_RSSI_OFFSET:
3228c2ecf20Sopenharmony_ci	case IAR_XTAL_CTRL:
3238c2ecf20Sopenharmony_ci	case IAR_CHF_PMA_GAIN:
3248c2ecf20Sopenharmony_ci	case IAR_CHF_IBUF:
3258c2ecf20Sopenharmony_ci	case IAR_CHF_QBUF:
3268c2ecf20Sopenharmony_ci	case IAR_CHF_IRIN:
3278c2ecf20Sopenharmony_ci	case IAR_CHF_QRIN:
3288c2ecf20Sopenharmony_ci	case IAR_CHF_IL:
3298c2ecf20Sopenharmony_ci	case IAR_CHF_QL:
3308c2ecf20Sopenharmony_ci	case IAR_CHF_CC1:
3318c2ecf20Sopenharmony_ci	case IAR_CHF_CCL:
3328c2ecf20Sopenharmony_ci	case IAR_CHF_CC2:
3338c2ecf20Sopenharmony_ci	case IAR_CHF_IROUT:
3348c2ecf20Sopenharmony_ci	case IAR_CHF_QROUT:
3358c2ecf20Sopenharmony_ci	case IAR_PA_TUNING:
3368c2ecf20Sopenharmony_ci	case IAR_VCO_CTRL1:
3378c2ecf20Sopenharmony_ci	case IAR_VCO_CTRL2:
3388c2ecf20Sopenharmony_ci		return true;
3398c2ecf20Sopenharmony_ci	default:
3408c2ecf20Sopenharmony_ci		return false;
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic bool
3458c2ecf20Sopenharmony_cimcr20a_iar_readable(struct device *dev, unsigned int reg)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	bool rc;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	/* all writeable are also readable */
3508c2ecf20Sopenharmony_ci	rc = mcr20a_iar_writeable(dev, reg);
3518c2ecf20Sopenharmony_ci	if (rc)
3528c2ecf20Sopenharmony_ci		return rc;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/* readonly regs */
3558c2ecf20Sopenharmony_ci	switch (reg) {
3568c2ecf20Sopenharmony_ci	case IAR_PART_ID:
3578c2ecf20Sopenharmony_ci	case IAR_DUAL_PAN_STS:
3588c2ecf20Sopenharmony_ci	case IAR_RX_BYTE_COUNT:
3598c2ecf20Sopenharmony_ci	case IAR_FILTERFAIL_CODE1:
3608c2ecf20Sopenharmony_ci	case IAR_FILTERFAIL_CODE2:
3618c2ecf20Sopenharmony_ci	case IAR_RSSI:
3628c2ecf20Sopenharmony_ci		return true;
3638c2ecf20Sopenharmony_ci	default:
3648c2ecf20Sopenharmony_ci		return false;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic bool
3698c2ecf20Sopenharmony_cimcr20a_iar_volatile(struct device *dev, unsigned int reg)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci/* can be changed during runtime */
3728c2ecf20Sopenharmony_ci	switch (reg) {
3738c2ecf20Sopenharmony_ci	case IAR_DUAL_PAN_STS:
3748c2ecf20Sopenharmony_ci	case IAR_RX_BYTE_COUNT:
3758c2ecf20Sopenharmony_ci	case IAR_FILTERFAIL_CODE1:
3768c2ecf20Sopenharmony_ci	case IAR_FILTERFAIL_CODE2:
3778c2ecf20Sopenharmony_ci	case IAR_RSSI:
3788c2ecf20Sopenharmony_ci		return true;
3798c2ecf20Sopenharmony_ci	default:
3808c2ecf20Sopenharmony_ci		return false;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic const struct regmap_config mcr20a_iar_regmap = {
3858c2ecf20Sopenharmony_ci	.name			= "mcr20a_iar",
3868c2ecf20Sopenharmony_ci	.reg_bits		= 16,
3878c2ecf20Sopenharmony_ci	.val_bits		= 8,
3888c2ecf20Sopenharmony_ci	.write_flag_mask	= REGISTER_ACCESS | REGISTER_WRITE | IAR_INDEX,
3898c2ecf20Sopenharmony_ci	.read_flag_mask		= REGISTER_ACCESS | REGISTER_READ  | IAR_INDEX,
3908c2ecf20Sopenharmony_ci	.cache_type		= REGCACHE_RBTREE,
3918c2ecf20Sopenharmony_ci	.writeable_reg		= mcr20a_iar_writeable,
3928c2ecf20Sopenharmony_ci	.readable_reg		= mcr20a_iar_readable,
3938c2ecf20Sopenharmony_ci	.volatile_reg		= mcr20a_iar_volatile,
3948c2ecf20Sopenharmony_ci	.fast_io		= true,
3958c2ecf20Sopenharmony_ci};
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistruct mcr20a_local {
3988c2ecf20Sopenharmony_ci	struct spi_device *spi;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	struct ieee802154_hw *hw;
4018c2ecf20Sopenharmony_ci	struct regmap *regmap_dar;
4028c2ecf20Sopenharmony_ci	struct regmap *regmap_iar;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	u8 *buf;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	bool is_tx;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	/* for writing tx buffer */
4098c2ecf20Sopenharmony_ci	struct spi_message tx_buf_msg;
4108c2ecf20Sopenharmony_ci	u8 tx_header[1];
4118c2ecf20Sopenharmony_ci	/* burst buffer write command */
4128c2ecf20Sopenharmony_ci	struct spi_transfer tx_xfer_header;
4138c2ecf20Sopenharmony_ci	u8 tx_len[1];
4148c2ecf20Sopenharmony_ci	/* len of tx packet */
4158c2ecf20Sopenharmony_ci	struct spi_transfer tx_xfer_len;
4168c2ecf20Sopenharmony_ci	/* data of tx packet */
4178c2ecf20Sopenharmony_ci	struct spi_transfer tx_xfer_buf;
4188c2ecf20Sopenharmony_ci	struct sk_buff *tx_skb;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/* for read length rxfifo */
4218c2ecf20Sopenharmony_ci	struct spi_message reg_msg;
4228c2ecf20Sopenharmony_ci	u8 reg_cmd[1];
4238c2ecf20Sopenharmony_ci	u8 reg_data[MCR20A_IRQSTS_NUM];
4248c2ecf20Sopenharmony_ci	struct spi_transfer reg_xfer_cmd;
4258c2ecf20Sopenharmony_ci	struct spi_transfer reg_xfer_data;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* receive handling */
4288c2ecf20Sopenharmony_ci	struct spi_message rx_buf_msg;
4298c2ecf20Sopenharmony_ci	u8 rx_header[1];
4308c2ecf20Sopenharmony_ci	struct spi_transfer rx_xfer_header;
4318c2ecf20Sopenharmony_ci	u8 rx_lqi[1];
4328c2ecf20Sopenharmony_ci	struct spi_transfer rx_xfer_lqi;
4338c2ecf20Sopenharmony_ci	u8 rx_buf[MCR20A_MAX_BUF];
4348c2ecf20Sopenharmony_ci	struct spi_transfer rx_xfer_buf;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* isr handling for reading intstat */
4378c2ecf20Sopenharmony_ci	struct spi_message irq_msg;
4388c2ecf20Sopenharmony_ci	u8 irq_header[1];
4398c2ecf20Sopenharmony_ci	u8 irq_data[MCR20A_IRQSTS_NUM];
4408c2ecf20Sopenharmony_ci	struct spi_transfer irq_xfer_data;
4418c2ecf20Sopenharmony_ci	struct spi_transfer irq_xfer_header;
4428c2ecf20Sopenharmony_ci};
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic void
4458c2ecf20Sopenharmony_cimcr20a_write_tx_buf_complete(void *context)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = context;
4488c2ecf20Sopenharmony_ci	int ret;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	lp->reg_msg.complete = NULL;
4538c2ecf20Sopenharmony_ci	lp->reg_cmd[0]	= MCR20A_WRITE_REG(DAR_PHY_CTRL1);
4548c2ecf20Sopenharmony_ci	lp->reg_data[0] = MCR20A_XCVSEQ_TX;
4558c2ecf20Sopenharmony_ci	lp->reg_xfer_data.len = 1;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	ret = spi_async(lp->spi, &lp->reg_msg);
4588c2ecf20Sopenharmony_ci	if (ret)
4598c2ecf20Sopenharmony_ci		dev_err(printdev(lp), "failed to set SEQ TX\n");
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic int
4638c2ecf20Sopenharmony_cimcr20a_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	lp->tx_skb = skb;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	print_hex_dump_debug("mcr20a tx: ", DUMP_PREFIX_OFFSET, 16, 1,
4728c2ecf20Sopenharmony_ci			     skb->data, skb->len, 0);
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	lp->is_tx = 1;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	lp->reg_msg.complete	= NULL;
4778c2ecf20Sopenharmony_ci	lp->reg_cmd[0]		= MCR20A_WRITE_REG(DAR_PHY_CTRL1);
4788c2ecf20Sopenharmony_ci	lp->reg_data[0]		= MCR20A_XCVSEQ_IDLE;
4798c2ecf20Sopenharmony_ci	lp->reg_xfer_data.len	= 1;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	return spi_async(lp->spi, &lp->reg_msg);
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic int
4858c2ecf20Sopenharmony_cimcr20a_ed(struct ieee802154_hw *hw, u8 *level)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	WARN_ON(!level);
4888c2ecf20Sopenharmony_ci	*level = 0xbe;
4898c2ecf20Sopenharmony_ci	return 0;
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int
4938c2ecf20Sopenharmony_cimcr20a_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
4968c2ecf20Sopenharmony_ci	int ret;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* freqency = ((PLL_INT+64) + (PLL_FRAC/65536)) * 32 MHz */
5018c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_PLL_INT0, PLL_INT[channel - 11]);
5028c2ecf20Sopenharmony_ci	if (ret)
5038c2ecf20Sopenharmony_ci		return ret;
5048c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_PLL_FRAC0_LSB, 0x00);
5058c2ecf20Sopenharmony_ci	if (ret)
5068c2ecf20Sopenharmony_ci		return ret;
5078c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_PLL_FRAC0_MSB,
5088c2ecf20Sopenharmony_ci			   PLL_FRAC[channel - 11]);
5098c2ecf20Sopenharmony_ci	if (ret)
5108c2ecf20Sopenharmony_ci		return ret;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	return 0;
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic int
5168c2ecf20Sopenharmony_cimcr20a_start(struct ieee802154_hw *hw)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
5198c2ecf20Sopenharmony_ci	int ret;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* No slotted operation */
5248c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "no slotted operation\n");
5258c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL1,
5268c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL1_SLOTTED, 0x0);
5278c2ecf20Sopenharmony_ci	if (ret < 0)
5288c2ecf20Sopenharmony_ci		return ret;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	/* enable irq */
5318c2ecf20Sopenharmony_ci	enable_irq(lp->spi->irq);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Unmask SEQ interrupt */
5348c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL2,
5358c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL2_SEQMSK, 0x0);
5368c2ecf20Sopenharmony_ci	if (ret < 0)
5378c2ecf20Sopenharmony_ci		return ret;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/* Start the RX sequence */
5408c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "start the RX sequence\n");
5418c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL1,
5428c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL1_XCVSEQ_MASK, MCR20A_XCVSEQ_RX);
5438c2ecf20Sopenharmony_ci	if (ret < 0)
5448c2ecf20Sopenharmony_ci		return ret;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	return 0;
5478c2ecf20Sopenharmony_ci}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistatic void
5508c2ecf20Sopenharmony_cimcr20a_stop(struct ieee802154_hw *hw)
5518c2ecf20Sopenharmony_ci{
5528c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	/* stop all running sequence */
5578c2ecf20Sopenharmony_ci	regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL1,
5588c2ecf20Sopenharmony_ci			   DAR_PHY_CTRL1_XCVSEQ_MASK, MCR20A_XCVSEQ_IDLE);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	/* disable irq */
5618c2ecf20Sopenharmony_ci	disable_irq(lp->spi->irq);
5628c2ecf20Sopenharmony_ci}
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistatic int
5658c2ecf20Sopenharmony_cimcr20a_set_hw_addr_filt(struct ieee802154_hw *hw,
5668c2ecf20Sopenharmony_ci			struct ieee802154_hw_addr_filt *filt,
5678c2ecf20Sopenharmony_ci			unsigned long changed)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
5748c2ecf20Sopenharmony_ci		u16 addr = le16_to_cpu(filt->short_addr);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		regmap_write(lp->regmap_iar, IAR_MACSHORTADDRS0_LSB, addr);
5778c2ecf20Sopenharmony_ci		regmap_write(lp->regmap_iar, IAR_MACSHORTADDRS0_MSB, addr >> 8);
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_PANID_CHANGED) {
5818c2ecf20Sopenharmony_ci		u16 pan = le16_to_cpu(filt->pan_id);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		regmap_write(lp->regmap_iar, IAR_MACPANID0_LSB, pan);
5848c2ecf20Sopenharmony_ci		regmap_write(lp->regmap_iar, IAR_MACPANID0_MSB, pan >> 8);
5858c2ecf20Sopenharmony_ci	}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
5888c2ecf20Sopenharmony_ci		u8 addr[8], i;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci		memcpy(addr, &filt->ieee_addr, 8);
5918c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++)
5928c2ecf20Sopenharmony_ci			regmap_write(lp->regmap_iar,
5938c2ecf20Sopenharmony_ci				     IAR_MACLONGADDRS0_0 + i, addr[i]);
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (changed & IEEE802154_AFILT_PANC_CHANGED) {
5978c2ecf20Sopenharmony_ci		if (filt->pan_coord) {
5988c2ecf20Sopenharmony_ci			regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL4,
5998c2ecf20Sopenharmony_ci					   DAR_PHY_CTRL4_PANCORDNTR0, 0x10);
6008c2ecf20Sopenharmony_ci		} else {
6018c2ecf20Sopenharmony_ci			regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL4,
6028c2ecf20Sopenharmony_ci					   DAR_PHY_CTRL4_PANCORDNTR0, 0x00);
6038c2ecf20Sopenharmony_ci		}
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	return 0;
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci/* -30 dBm to 10 dBm */
6108c2ecf20Sopenharmony_ci#define MCR20A_MAX_TX_POWERS 0x14
6118c2ecf20Sopenharmony_cistatic const s32 mcr20a_powers[MCR20A_MAX_TX_POWERS + 1] = {
6128c2ecf20Sopenharmony_ci	-3000, -2800, -2600, -2400, -2200, -2000, -1800, -1600, -1400,
6138c2ecf20Sopenharmony_ci	-1200, -1000, -800, -600, -400, -200, 0, 200, 400, 600, 800, 1000
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic int
6178c2ecf20Sopenharmony_cimcr20a_set_txpower(struct ieee802154_hw *hw, s32 mbm)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
6208c2ecf20Sopenharmony_ci	u32 i;
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s(%d)\n", __func__, mbm);
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
6258c2ecf20Sopenharmony_ci		if (lp->hw->phy->supported.tx_powers[i] == mbm)
6268c2ecf20Sopenharmony_ci			return regmap_write(lp->regmap_dar, DAR_PA_PWR,
6278c2ecf20Sopenharmony_ci					    ((i + 8) & 0x1F));
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return -EINVAL;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci#define MCR20A_MAX_ED_LEVELS MCR20A_MIN_CCA_THRESHOLD
6348c2ecf20Sopenharmony_cistatic s32 mcr20a_ed_levels[MCR20A_MAX_ED_LEVELS + 1];
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic int
6378c2ecf20Sopenharmony_cimcr20a_set_cca_mode(struct ieee802154_hw *hw,
6388c2ecf20Sopenharmony_ci		    const struct wpan_phy_cca *cca)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
6418c2ecf20Sopenharmony_ci	unsigned int cca_mode = 0xff;
6428c2ecf20Sopenharmony_ci	bool cca_mode_and = false;
6438c2ecf20Sopenharmony_ci	int ret;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	/* mapping 802.15.4 to driver spec */
6488c2ecf20Sopenharmony_ci	switch (cca->mode) {
6498c2ecf20Sopenharmony_ci	case NL802154_CCA_ENERGY:
6508c2ecf20Sopenharmony_ci		cca_mode = MCR20A_CCA_MODE1;
6518c2ecf20Sopenharmony_ci		break;
6528c2ecf20Sopenharmony_ci	case NL802154_CCA_CARRIER:
6538c2ecf20Sopenharmony_ci		cca_mode = MCR20A_CCA_MODE2;
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci	case NL802154_CCA_ENERGY_CARRIER:
6568c2ecf20Sopenharmony_ci		switch (cca->opt) {
6578c2ecf20Sopenharmony_ci		case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
6588c2ecf20Sopenharmony_ci			cca_mode = MCR20A_CCA_MODE3;
6598c2ecf20Sopenharmony_ci			cca_mode_and = true;
6608c2ecf20Sopenharmony_ci			break;
6618c2ecf20Sopenharmony_ci		case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
6628c2ecf20Sopenharmony_ci			cca_mode = MCR20A_CCA_MODE3;
6638c2ecf20Sopenharmony_ci			cca_mode_and = false;
6648c2ecf20Sopenharmony_ci			break;
6658c2ecf20Sopenharmony_ci		default:
6668c2ecf20Sopenharmony_ci			return -EINVAL;
6678c2ecf20Sopenharmony_ci		}
6688c2ecf20Sopenharmony_ci		break;
6698c2ecf20Sopenharmony_ci	default:
6708c2ecf20Sopenharmony_ci		return -EINVAL;
6718c2ecf20Sopenharmony_ci	}
6728c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL4,
6738c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL4_CCATYPE_MASK,
6748c2ecf20Sopenharmony_ci				 cca_mode << DAR_PHY_CTRL4_CCATYPE_SHIFT);
6758c2ecf20Sopenharmony_ci	if (ret < 0)
6768c2ecf20Sopenharmony_ci		return ret;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (cca_mode == MCR20A_CCA_MODE3) {
6798c2ecf20Sopenharmony_ci		if (cca_mode_and) {
6808c2ecf20Sopenharmony_ci			ret = regmap_update_bits(lp->regmap_iar, IAR_CCA_CTRL,
6818c2ecf20Sopenharmony_ci						 IAR_CCA_CTRL_CCA3_AND_NOT_OR,
6828c2ecf20Sopenharmony_ci						 0x08);
6838c2ecf20Sopenharmony_ci		} else {
6848c2ecf20Sopenharmony_ci			ret = regmap_update_bits(lp->regmap_iar,
6858c2ecf20Sopenharmony_ci						 IAR_CCA_CTRL,
6868c2ecf20Sopenharmony_ci						 IAR_CCA_CTRL_CCA3_AND_NOT_OR,
6878c2ecf20Sopenharmony_ci						 0x00);
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci		if (ret < 0)
6908c2ecf20Sopenharmony_ci			return ret;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return ret;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic int
6978c2ecf20Sopenharmony_cimcr20a_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
7008c2ecf20Sopenharmony_ci	u32 i;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
7058c2ecf20Sopenharmony_ci		if (hw->phy->supported.cca_ed_levels[i] == mbm)
7068c2ecf20Sopenharmony_ci			return regmap_write(lp->regmap_iar, IAR_CCA1_THRESH, i);
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	return 0;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cistatic int
7138c2ecf20Sopenharmony_cimcr20a_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = hw->priv;
7168c2ecf20Sopenharmony_ci	int ret;
7178c2ecf20Sopenharmony_ci	u8 rx_frame_filter_reg = 0x0;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s(%d)\n", __func__, on);
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	if (on) {
7228c2ecf20Sopenharmony_ci		/* All frame types accepted*/
7238c2ecf20Sopenharmony_ci		rx_frame_filter_reg &= ~(IAR_RX_FRAME_FLT_FRM_VER);
7248c2ecf20Sopenharmony_ci		rx_frame_filter_reg |= (IAR_RX_FRAME_FLT_ACK_FT |
7258c2ecf20Sopenharmony_ci				  IAR_RX_FRAME_FLT_NS_FT);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci		ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL4,
7288c2ecf20Sopenharmony_ci					 DAR_PHY_CTRL4_PROMISCUOUS,
7298c2ecf20Sopenharmony_ci					 DAR_PHY_CTRL4_PROMISCUOUS);
7308c2ecf20Sopenharmony_ci		if (ret < 0)
7318c2ecf20Sopenharmony_ci			return ret;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		ret = regmap_write(lp->regmap_iar, IAR_RX_FRAME_FILTER,
7348c2ecf20Sopenharmony_ci				   rx_frame_filter_reg);
7358c2ecf20Sopenharmony_ci		if (ret < 0)
7368c2ecf20Sopenharmony_ci			return ret;
7378c2ecf20Sopenharmony_ci	} else {
7388c2ecf20Sopenharmony_ci		ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL4,
7398c2ecf20Sopenharmony_ci					 DAR_PHY_CTRL4_PROMISCUOUS, 0x0);
7408c2ecf20Sopenharmony_ci		if (ret < 0)
7418c2ecf20Sopenharmony_ci			return ret;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci		ret = regmap_write(lp->regmap_iar, IAR_RX_FRAME_FILTER,
7448c2ecf20Sopenharmony_ci				   IAR_RX_FRAME_FLT_FRM_VER |
7458c2ecf20Sopenharmony_ci				   IAR_RX_FRAME_FLT_BEACON_FT |
7468c2ecf20Sopenharmony_ci				   IAR_RX_FRAME_FLT_DATA_FT |
7478c2ecf20Sopenharmony_ci				   IAR_RX_FRAME_FLT_CMD_FT);
7488c2ecf20Sopenharmony_ci		if (ret < 0)
7498c2ecf20Sopenharmony_ci			return ret;
7508c2ecf20Sopenharmony_ci	}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	return 0;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cistatic const struct ieee802154_ops mcr20a_hw_ops = {
7568c2ecf20Sopenharmony_ci	.owner			= THIS_MODULE,
7578c2ecf20Sopenharmony_ci	.xmit_async		= mcr20a_xmit,
7588c2ecf20Sopenharmony_ci	.ed			= mcr20a_ed,
7598c2ecf20Sopenharmony_ci	.set_channel		= mcr20a_set_channel,
7608c2ecf20Sopenharmony_ci	.start			= mcr20a_start,
7618c2ecf20Sopenharmony_ci	.stop			= mcr20a_stop,
7628c2ecf20Sopenharmony_ci	.set_hw_addr_filt	= mcr20a_set_hw_addr_filt,
7638c2ecf20Sopenharmony_ci	.set_txpower		= mcr20a_set_txpower,
7648c2ecf20Sopenharmony_ci	.set_cca_mode		= mcr20a_set_cca_mode,
7658c2ecf20Sopenharmony_ci	.set_cca_ed_level	= mcr20a_set_cca_ed_level,
7668c2ecf20Sopenharmony_ci	.set_promiscuous_mode	= mcr20a_set_promiscuous_mode,
7678c2ecf20Sopenharmony_ci};
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic int
7708c2ecf20Sopenharmony_cimcr20a_request_rx(struct mcr20a_local *lp)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	/* Start the RX sequence */
7758c2ecf20Sopenharmony_ci	regmap_update_bits_async(lp->regmap_dar, DAR_PHY_CTRL1,
7768c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL1_XCVSEQ_MASK, MCR20A_XCVSEQ_RX);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	return 0;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic void
7828c2ecf20Sopenharmony_cimcr20a_handle_rx_read_buf_complete(void *context)
7838c2ecf20Sopenharmony_ci{
7848c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = context;
7858c2ecf20Sopenharmony_ci	u8 len = lp->reg_data[0] & DAR_RX_FRAME_LENGTH_MASK;
7868c2ecf20Sopenharmony_ci	struct sk_buff *skb;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "RX is done\n");
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	if (!ieee802154_is_valid_psdu_len(len)) {
7938c2ecf20Sopenharmony_ci		dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
7948c2ecf20Sopenharmony_ci		len = IEEE802154_MTU;
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	len = len - 2;  /* get rid of frame check field */
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(len);
8008c2ecf20Sopenharmony_ci	if (!skb)
8018c2ecf20Sopenharmony_ci		return;
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	__skb_put_data(skb, lp->rx_buf, len);
8048c2ecf20Sopenharmony_ci	ieee802154_rx_irqsafe(lp->hw, skb, lp->rx_lqi[0]);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	print_hex_dump_debug("mcr20a rx: ", DUMP_PREFIX_OFFSET, 16, 1,
8078c2ecf20Sopenharmony_ci			     lp->rx_buf, len, 0);
8088c2ecf20Sopenharmony_ci	pr_debug("mcr20a rx: lqi: %02hhx\n", lp->rx_lqi[0]);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/* start RX sequence */
8118c2ecf20Sopenharmony_ci	mcr20a_request_rx(lp);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_cistatic void
8158c2ecf20Sopenharmony_cimcr20a_handle_rx_read_len_complete(void *context)
8168c2ecf20Sopenharmony_ci{
8178c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = context;
8188c2ecf20Sopenharmony_ci	u8 len;
8198c2ecf20Sopenharmony_ci	int ret;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	/* get the length of received frame */
8248c2ecf20Sopenharmony_ci	len = lp->reg_data[0] & DAR_RX_FRAME_LENGTH_MASK;
8258c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "frame len : %d\n", len);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	/* prepare to read the rx buf */
8288c2ecf20Sopenharmony_ci	lp->rx_buf_msg.complete = mcr20a_handle_rx_read_buf_complete;
8298c2ecf20Sopenharmony_ci	lp->rx_header[0] = MCR20A_BURST_READ_PACKET_BUF;
8308c2ecf20Sopenharmony_ci	lp->rx_xfer_buf.len = len;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	ret = spi_async(lp->spi, &lp->rx_buf_msg);
8338c2ecf20Sopenharmony_ci	if (ret)
8348c2ecf20Sopenharmony_ci		dev_err(printdev(lp), "failed to read rx buffer length\n");
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_cistatic int
8388c2ecf20Sopenharmony_cimcr20a_handle_rx(struct mcr20a_local *lp)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
8418c2ecf20Sopenharmony_ci	lp->reg_msg.complete = mcr20a_handle_rx_read_len_complete;
8428c2ecf20Sopenharmony_ci	lp->reg_cmd[0] = MCR20A_READ_REG(DAR_RX_FRM_LEN);
8438c2ecf20Sopenharmony_ci	lp->reg_xfer_data.len	= 1;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	return spi_async(lp->spi, &lp->reg_msg);
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic int
8498c2ecf20Sopenharmony_cimcr20a_handle_tx_complete(struct mcr20a_local *lp)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	return mcr20a_request_rx(lp);
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic int
8598c2ecf20Sopenharmony_cimcr20a_handle_tx(struct mcr20a_local *lp)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	int ret;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	/* write tx buffer */
8668c2ecf20Sopenharmony_ci	lp->tx_header[0]	= MCR20A_BURST_WRITE_PACKET_BUF;
8678c2ecf20Sopenharmony_ci	/* add 2 bytes of FCS */
8688c2ecf20Sopenharmony_ci	lp->tx_len[0]		= lp->tx_skb->len + 2;
8698c2ecf20Sopenharmony_ci	lp->tx_xfer_buf.tx_buf	= lp->tx_skb->data;
8708c2ecf20Sopenharmony_ci	/* add 1 byte psduLength */
8718c2ecf20Sopenharmony_ci	lp->tx_xfer_buf.len	= lp->tx_skb->len + 1;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	ret = spi_async(lp->spi, &lp->tx_buf_msg);
8748c2ecf20Sopenharmony_ci	if (ret) {
8758c2ecf20Sopenharmony_ci		dev_err(printdev(lp), "SPI write Failed for TX buf\n");
8768c2ecf20Sopenharmony_ci		return ret;
8778c2ecf20Sopenharmony_ci	}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	return 0;
8808c2ecf20Sopenharmony_ci}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_cistatic void
8838c2ecf20Sopenharmony_cimcr20a_irq_clean_complete(void *context)
8848c2ecf20Sopenharmony_ci{
8858c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = context;
8868c2ecf20Sopenharmony_ci	u8 seq_state = lp->irq_data[DAR_IRQ_STS1] & DAR_PHY_CTRL1_XCVSEQ_MASK;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	enable_irq(lp->spi->irq);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "IRQ STA1 (%02x) STA2 (%02x)\n",
8938c2ecf20Sopenharmony_ci		lp->irq_data[DAR_IRQ_STS1], lp->irq_data[DAR_IRQ_STS2]);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	switch (seq_state) {
8968c2ecf20Sopenharmony_ci	/* TX IRQ, RX IRQ and SEQ IRQ */
8978c2ecf20Sopenharmony_ci	case (DAR_IRQSTS1_TXIRQ | DAR_IRQSTS1_SEQIRQ):
8988c2ecf20Sopenharmony_ci		if (lp->is_tx) {
8998c2ecf20Sopenharmony_ci			lp->is_tx = 0;
9008c2ecf20Sopenharmony_ci			dev_dbg(printdev(lp), "TX is done. No ACK\n");
9018c2ecf20Sopenharmony_ci			mcr20a_handle_tx_complete(lp);
9028c2ecf20Sopenharmony_ci		}
9038c2ecf20Sopenharmony_ci		break;
9048c2ecf20Sopenharmony_ci	case (DAR_IRQSTS1_RXIRQ | DAR_IRQSTS1_SEQIRQ):
9058c2ecf20Sopenharmony_ci		/* rx is starting */
9068c2ecf20Sopenharmony_ci		dev_dbg(printdev(lp), "RX is starting\n");
9078c2ecf20Sopenharmony_ci		mcr20a_handle_rx(lp);
9088c2ecf20Sopenharmony_ci		break;
9098c2ecf20Sopenharmony_ci	case (DAR_IRQSTS1_RXIRQ | DAR_IRQSTS1_TXIRQ | DAR_IRQSTS1_SEQIRQ):
9108c2ecf20Sopenharmony_ci		if (lp->is_tx) {
9118c2ecf20Sopenharmony_ci			/* tx is done */
9128c2ecf20Sopenharmony_ci			lp->is_tx = 0;
9138c2ecf20Sopenharmony_ci			dev_dbg(printdev(lp), "TX is done. Get ACK\n");
9148c2ecf20Sopenharmony_ci			mcr20a_handle_tx_complete(lp);
9158c2ecf20Sopenharmony_ci		} else {
9168c2ecf20Sopenharmony_ci			/* rx is starting */
9178c2ecf20Sopenharmony_ci			dev_dbg(printdev(lp), "RX is starting\n");
9188c2ecf20Sopenharmony_ci			mcr20a_handle_rx(lp);
9198c2ecf20Sopenharmony_ci		}
9208c2ecf20Sopenharmony_ci		break;
9218c2ecf20Sopenharmony_ci	case (DAR_IRQSTS1_SEQIRQ):
9228c2ecf20Sopenharmony_ci		if (lp->is_tx) {
9238c2ecf20Sopenharmony_ci			dev_dbg(printdev(lp), "TX is starting\n");
9248c2ecf20Sopenharmony_ci			mcr20a_handle_tx(lp);
9258c2ecf20Sopenharmony_ci		} else {
9268c2ecf20Sopenharmony_ci			dev_dbg(printdev(lp), "MCR20A is stop\n");
9278c2ecf20Sopenharmony_ci		}
9288c2ecf20Sopenharmony_ci		break;
9298c2ecf20Sopenharmony_ci	}
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic void mcr20a_irq_status_complete(void *context)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	int ret;
9358c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = context;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
9388c2ecf20Sopenharmony_ci	regmap_update_bits_async(lp->regmap_dar, DAR_PHY_CTRL1,
9398c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL1_XCVSEQ_MASK, MCR20A_XCVSEQ_IDLE);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	lp->reg_msg.complete = mcr20a_irq_clean_complete;
9428c2ecf20Sopenharmony_ci	lp->reg_cmd[0] = MCR20A_WRITE_REG(DAR_IRQ_STS1);
9438c2ecf20Sopenharmony_ci	memcpy(lp->reg_data, lp->irq_data, MCR20A_IRQSTS_NUM);
9448c2ecf20Sopenharmony_ci	lp->reg_xfer_data.len = MCR20A_IRQSTS_NUM;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	ret = spi_async(lp->spi, &lp->reg_msg);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (ret)
9498c2ecf20Sopenharmony_ci		dev_err(printdev(lp), "failed to clean irq status\n");
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_cistatic irqreturn_t mcr20a_irq_isr(int irq, void *data)
9538c2ecf20Sopenharmony_ci{
9548c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = data;
9558c2ecf20Sopenharmony_ci	int ret;
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci	disable_irq_nosync(irq);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	lp->irq_header[0] = MCR20A_READ_REG(DAR_IRQ_STS1);
9608c2ecf20Sopenharmony_ci	/* read IRQSTSx */
9618c2ecf20Sopenharmony_ci	ret = spi_async(lp->spi, &lp->irq_msg);
9628c2ecf20Sopenharmony_ci	if (ret) {
9638c2ecf20Sopenharmony_ci		enable_irq(irq);
9648c2ecf20Sopenharmony_ci		return IRQ_NONE;
9658c2ecf20Sopenharmony_ci	}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic void mcr20a_hw_setup(struct mcr20a_local *lp)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	u8 i;
9738c2ecf20Sopenharmony_ci	struct ieee802154_hw *hw = lp->hw;
9748c2ecf20Sopenharmony_ci	struct wpan_phy *phy = lp->hw->phy;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	phy->symbol_duration = 16;
9798c2ecf20Sopenharmony_ci	phy->lifs_period = 40 * phy->symbol_duration;
9808c2ecf20Sopenharmony_ci	phy->sifs_period = 12 * phy->symbol_duration;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
9838c2ecf20Sopenharmony_ci			IEEE802154_HW_AFILT |
9848c2ecf20Sopenharmony_ci			IEEE802154_HW_PROMISCUOUS;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
9878c2ecf20Sopenharmony_ci			WPAN_PHY_FLAG_CCA_MODE;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
9908c2ecf20Sopenharmony_ci		BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
9918c2ecf20Sopenharmony_ci	phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
9928c2ecf20Sopenharmony_ci		BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	/* initiating cca_ed_levels */
9958c2ecf20Sopenharmony_ci	for (i = MCR20A_MAX_CCA_THRESHOLD; i < MCR20A_MIN_CCA_THRESHOLD + 1;
9968c2ecf20Sopenharmony_ci	      ++i) {
9978c2ecf20Sopenharmony_ci		mcr20a_ed_levels[i] =  -i * 100;
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	phy->supported.cca_ed_levels = mcr20a_ed_levels;
10018c2ecf20Sopenharmony_ci	phy->supported.cca_ed_levels_size = ARRAY_SIZE(mcr20a_ed_levels);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	phy->cca.mode = NL802154_CCA_ENERGY;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	phy->supported.channels[0] = MCR20A_VALID_CHANNELS;
10068c2ecf20Sopenharmony_ci	phy->current_page = 0;
10078c2ecf20Sopenharmony_ci	/* MCR20A default reset value */
10088c2ecf20Sopenharmony_ci	phy->current_channel = 20;
10098c2ecf20Sopenharmony_ci	phy->symbol_duration = 16;
10108c2ecf20Sopenharmony_ci	phy->supported.tx_powers = mcr20a_powers;
10118c2ecf20Sopenharmony_ci	phy->supported.tx_powers_size = ARRAY_SIZE(mcr20a_powers);
10128c2ecf20Sopenharmony_ci	phy->cca_ed_level = phy->supported.cca_ed_levels[75];
10138c2ecf20Sopenharmony_ci	phy->transmit_power = phy->supported.tx_powers[0x0F];
10148c2ecf20Sopenharmony_ci}
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_cistatic void
10178c2ecf20Sopenharmony_cimcr20a_setup_tx_spi_messages(struct mcr20a_local *lp)
10188c2ecf20Sopenharmony_ci{
10198c2ecf20Sopenharmony_ci	spi_message_init(&lp->tx_buf_msg);
10208c2ecf20Sopenharmony_ci	lp->tx_buf_msg.context = lp;
10218c2ecf20Sopenharmony_ci	lp->tx_buf_msg.complete = mcr20a_write_tx_buf_complete;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	lp->tx_xfer_header.len = 1;
10248c2ecf20Sopenharmony_ci	lp->tx_xfer_header.tx_buf = lp->tx_header;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	lp->tx_xfer_len.len = 1;
10278c2ecf20Sopenharmony_ci	lp->tx_xfer_len.tx_buf = lp->tx_len;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->tx_xfer_header, &lp->tx_buf_msg);
10308c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->tx_xfer_len, &lp->tx_buf_msg);
10318c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->tx_xfer_buf, &lp->tx_buf_msg);
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_cistatic void
10358c2ecf20Sopenharmony_cimcr20a_setup_rx_spi_messages(struct mcr20a_local *lp)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	spi_message_init(&lp->reg_msg);
10388c2ecf20Sopenharmony_ci	lp->reg_msg.context = lp;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	lp->reg_xfer_cmd.len = 1;
10418c2ecf20Sopenharmony_ci	lp->reg_xfer_cmd.tx_buf = lp->reg_cmd;
10428c2ecf20Sopenharmony_ci	lp->reg_xfer_cmd.rx_buf = lp->reg_cmd;
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	lp->reg_xfer_data.rx_buf = lp->reg_data;
10458c2ecf20Sopenharmony_ci	lp->reg_xfer_data.tx_buf = lp->reg_data;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->reg_xfer_cmd, &lp->reg_msg);
10488c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->reg_xfer_data, &lp->reg_msg);
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci	spi_message_init(&lp->rx_buf_msg);
10518c2ecf20Sopenharmony_ci	lp->rx_buf_msg.context = lp;
10528c2ecf20Sopenharmony_ci	lp->rx_buf_msg.complete = mcr20a_handle_rx_read_buf_complete;
10538c2ecf20Sopenharmony_ci	lp->rx_xfer_header.len = 1;
10548c2ecf20Sopenharmony_ci	lp->rx_xfer_header.tx_buf = lp->rx_header;
10558c2ecf20Sopenharmony_ci	lp->rx_xfer_header.rx_buf = lp->rx_header;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	lp->rx_xfer_buf.rx_buf = lp->rx_buf;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	lp->rx_xfer_lqi.len = 1;
10608c2ecf20Sopenharmony_ci	lp->rx_xfer_lqi.rx_buf = lp->rx_lqi;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->rx_xfer_header, &lp->rx_buf_msg);
10638c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->rx_xfer_buf, &lp->rx_buf_msg);
10648c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->rx_xfer_lqi, &lp->rx_buf_msg);
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_cistatic void
10688c2ecf20Sopenharmony_cimcr20a_setup_irq_spi_messages(struct mcr20a_local *lp)
10698c2ecf20Sopenharmony_ci{
10708c2ecf20Sopenharmony_ci	spi_message_init(&lp->irq_msg);
10718c2ecf20Sopenharmony_ci	lp->irq_msg.context		= lp;
10728c2ecf20Sopenharmony_ci	lp->irq_msg.complete	= mcr20a_irq_status_complete;
10738c2ecf20Sopenharmony_ci	lp->irq_xfer_header.len	= 1;
10748c2ecf20Sopenharmony_ci	lp->irq_xfer_header.tx_buf = lp->irq_header;
10758c2ecf20Sopenharmony_ci	lp->irq_xfer_header.rx_buf = lp->irq_header;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	lp->irq_xfer_data.len	= MCR20A_IRQSTS_NUM;
10788c2ecf20Sopenharmony_ci	lp->irq_xfer_data.rx_buf = lp->irq_data;
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->irq_xfer_header, &lp->irq_msg);
10818c2ecf20Sopenharmony_ci	spi_message_add_tail(&lp->irq_xfer_data, &lp->irq_msg);
10828c2ecf20Sopenharmony_ci}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_cistatic int
10858c2ecf20Sopenharmony_cimcr20a_phy_init(struct mcr20a_local *lp)
10868c2ecf20Sopenharmony_ci{
10878c2ecf20Sopenharmony_ci	u8 index;
10888c2ecf20Sopenharmony_ci	unsigned int phy_reg = 0;
10898c2ecf20Sopenharmony_ci	int ret;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "%s\n", __func__);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	/* Disable Tristate on COCO MISO for SPI reads */
10948c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_iar, IAR_MISC_PAD_CTRL, 0x02);
10958c2ecf20Sopenharmony_ci	if (ret)
10968c2ecf20Sopenharmony_ci		goto err_ret;
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	/* Clear all PP IRQ bits in IRQSTS1 to avoid unexpected interrupts
10998c2ecf20Sopenharmony_ci	 * immediately after init
11008c2ecf20Sopenharmony_ci	 */
11018c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_IRQ_STS1, 0xEF);
11028c2ecf20Sopenharmony_ci	if (ret)
11038c2ecf20Sopenharmony_ci		goto err_ret;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	/* Clear all PP IRQ bits in IRQSTS2 */
11068c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_IRQ_STS2,
11078c2ecf20Sopenharmony_ci			   DAR_IRQSTS2_ASM_IRQ | DAR_IRQSTS2_PB_ERR_IRQ |
11088c2ecf20Sopenharmony_ci			   DAR_IRQSTS2_WAKE_IRQ);
11098c2ecf20Sopenharmony_ci	if (ret)
11108c2ecf20Sopenharmony_ci		goto err_ret;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	/* Disable all timer interrupts */
11138c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_IRQ_STS3, 0xFF);
11148c2ecf20Sopenharmony_ci	if (ret)
11158c2ecf20Sopenharmony_ci		goto err_ret;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	/*  PHY_CTRL1 : default HW settings + AUTOACK enabled */
11188c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PHY_CTRL1,
11198c2ecf20Sopenharmony_ci				 DAR_PHY_CTRL1_AUTOACK, DAR_PHY_CTRL1_AUTOACK);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	/*  PHY_CTRL2 : disable all interrupts */
11228c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_PHY_CTRL2, 0xFF);
11238c2ecf20Sopenharmony_ci	if (ret)
11248c2ecf20Sopenharmony_ci		goto err_ret;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	/* PHY_CTRL3 : disable all timers and remaining interrupts */
11278c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_PHY_CTRL3,
11288c2ecf20Sopenharmony_ci			   DAR_PHY_CTRL3_ASM_MSK | DAR_PHY_CTRL3_PB_ERR_MSK |
11298c2ecf20Sopenharmony_ci			   DAR_PHY_CTRL3_WAKE_MSK);
11308c2ecf20Sopenharmony_ci	if (ret)
11318c2ecf20Sopenharmony_ci		goto err_ret;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	/* SRC_CTRL : enable Acknowledge Frame Pending and
11348c2ecf20Sopenharmony_ci	 * Source Address Matching Enable
11358c2ecf20Sopenharmony_ci	 */
11368c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_SRC_CTRL,
11378c2ecf20Sopenharmony_ci			   DAR_SRC_CTRL_ACK_FRM_PND |
11388c2ecf20Sopenharmony_ci			   (DAR_SRC_CTRL_INDEX << DAR_SRC_CTRL_INDEX_SHIFT));
11398c2ecf20Sopenharmony_ci	if (ret)
11408c2ecf20Sopenharmony_ci		goto err_ret;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	/*  RX_FRAME_FILTER */
11438c2ecf20Sopenharmony_ci	/*  FRM_VER[1:0] = b11. Accept FrameVersion 0 and 1 packets */
11448c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_iar, IAR_RX_FRAME_FILTER,
11458c2ecf20Sopenharmony_ci			   IAR_RX_FRAME_FLT_FRM_VER |
11468c2ecf20Sopenharmony_ci			   IAR_RX_FRAME_FLT_BEACON_FT |
11478c2ecf20Sopenharmony_ci			   IAR_RX_FRAME_FLT_DATA_FT |
11488c2ecf20Sopenharmony_ci			   IAR_RX_FRAME_FLT_CMD_FT);
11498c2ecf20Sopenharmony_ci	if (ret)
11508c2ecf20Sopenharmony_ci		goto err_ret;
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	dev_info(printdev(lp), "MCR20A DAR overwrites version: 0x%02x\n",
11538c2ecf20Sopenharmony_ci		 MCR20A_OVERWRITE_VERSION);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	/* Overwrites direct registers  */
11568c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_dar, DAR_OVERWRITE_VER,
11578c2ecf20Sopenharmony_ci			   MCR20A_OVERWRITE_VERSION);
11588c2ecf20Sopenharmony_ci	if (ret)
11598c2ecf20Sopenharmony_ci		goto err_ret;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	/* Overwrites indirect registers  */
11628c2ecf20Sopenharmony_ci	ret = regmap_multi_reg_write(lp->regmap_iar, mar20a_iar_overwrites,
11638c2ecf20Sopenharmony_ci				     ARRAY_SIZE(mar20a_iar_overwrites));
11648c2ecf20Sopenharmony_ci	if (ret)
11658c2ecf20Sopenharmony_ci		goto err_ret;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci	/* Clear HW indirect queue */
11688c2ecf20Sopenharmony_ci	dev_dbg(printdev(lp), "clear HW indirect queue\n");
11698c2ecf20Sopenharmony_ci	for (index = 0; index < MCR20A_PHY_INDIRECT_QUEUE_SIZE; index++) {
11708c2ecf20Sopenharmony_ci		phy_reg = (u8)(((index & DAR_SRC_CTRL_INDEX) <<
11718c2ecf20Sopenharmony_ci			       DAR_SRC_CTRL_INDEX_SHIFT)
11728c2ecf20Sopenharmony_ci			      | (DAR_SRC_CTRL_SRCADDR_EN)
11738c2ecf20Sopenharmony_ci			      | (DAR_SRC_CTRL_INDEX_DISABLE));
11748c2ecf20Sopenharmony_ci		ret = regmap_write(lp->regmap_dar, DAR_SRC_CTRL, phy_reg);
11758c2ecf20Sopenharmony_ci		if (ret)
11768c2ecf20Sopenharmony_ci			goto err_ret;
11778c2ecf20Sopenharmony_ci		phy_reg = 0;
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	/* Assign HW Indirect hash table to PAN0 */
11818c2ecf20Sopenharmony_ci	ret = regmap_read(lp->regmap_iar, IAR_DUAL_PAN_CTRL, &phy_reg);
11828c2ecf20Sopenharmony_ci	if (ret)
11838c2ecf20Sopenharmony_ci		goto err_ret;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	/* Clear current lvl */
11868c2ecf20Sopenharmony_ci	phy_reg &= ~IAR_DUAL_PAN_CTRL_DUAL_PAN_SAM_LVL_MSK;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	/* Set new lvl */
11898c2ecf20Sopenharmony_ci	phy_reg |= MCR20A_PHY_INDIRECT_QUEUE_SIZE <<
11908c2ecf20Sopenharmony_ci		IAR_DUAL_PAN_CTRL_DUAL_PAN_SAM_LVL_SHIFT;
11918c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_iar, IAR_DUAL_PAN_CTRL, phy_reg);
11928c2ecf20Sopenharmony_ci	if (ret)
11938c2ecf20Sopenharmony_ci		goto err_ret;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	/* Set CCA threshold to -75 dBm */
11968c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_iar, IAR_CCA1_THRESH, 0x4B);
11978c2ecf20Sopenharmony_ci	if (ret)
11988c2ecf20Sopenharmony_ci		goto err_ret;
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	/* Set prescaller to obtain 1 symbol (16us) timebase */
12018c2ecf20Sopenharmony_ci	ret = regmap_write(lp->regmap_iar, IAR_TMR_PRESCALE, 0x05);
12028c2ecf20Sopenharmony_ci	if (ret)
12038c2ecf20Sopenharmony_ci		goto err_ret;
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	/* Enable autodoze mode. */
12068c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_PWR_MODES,
12078c2ecf20Sopenharmony_ci				 DAR_PWR_MODES_AUTODOZE,
12088c2ecf20Sopenharmony_ci				 DAR_PWR_MODES_AUTODOZE);
12098c2ecf20Sopenharmony_ci	if (ret)
12108c2ecf20Sopenharmony_ci		goto err_ret;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	/* Disable clk_out */
12138c2ecf20Sopenharmony_ci	ret = regmap_update_bits(lp->regmap_dar, DAR_CLK_OUT_CTRL,
12148c2ecf20Sopenharmony_ci				 DAR_CLK_OUT_CTRL_EN, 0x0);
12158c2ecf20Sopenharmony_ci	if (ret)
12168c2ecf20Sopenharmony_ci		goto err_ret;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	return 0;
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_cierr_ret:
12218c2ecf20Sopenharmony_ci	return ret;
12228c2ecf20Sopenharmony_ci}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_cistatic int
12258c2ecf20Sopenharmony_cimcr20a_probe(struct spi_device *spi)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	struct ieee802154_hw *hw;
12288c2ecf20Sopenharmony_ci	struct mcr20a_local *lp;
12298c2ecf20Sopenharmony_ci	struct gpio_desc *rst_b;
12308c2ecf20Sopenharmony_ci	int irq_type;
12318c2ecf20Sopenharmony_ci	int ret = -ENOMEM;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "%s\n", __func__);
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	if (!spi->irq) {
12368c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "no IRQ specified\n");
12378c2ecf20Sopenharmony_ci		return -EINVAL;
12388c2ecf20Sopenharmony_ci	}
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	rst_b = devm_gpiod_get(&spi->dev, "rst_b", GPIOD_OUT_HIGH);
12418c2ecf20Sopenharmony_ci	if (IS_ERR(rst_b)) {
12428c2ecf20Sopenharmony_ci		ret = PTR_ERR(rst_b);
12438c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
12448c2ecf20Sopenharmony_ci			dev_err(&spi->dev, "Failed to get 'rst_b' gpio: %d", ret);
12458c2ecf20Sopenharmony_ci		return ret;
12468c2ecf20Sopenharmony_ci	}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	/* reset mcr20a */
12498c2ecf20Sopenharmony_ci	usleep_range(10, 20);
12508c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(rst_b, 1);
12518c2ecf20Sopenharmony_ci	usleep_range(10, 20);
12528c2ecf20Sopenharmony_ci	gpiod_set_value_cansleep(rst_b, 0);
12538c2ecf20Sopenharmony_ci	usleep_range(120, 240);
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	/* allocate ieee802154_hw and private data */
12568c2ecf20Sopenharmony_ci	hw = ieee802154_alloc_hw(sizeof(*lp), &mcr20a_hw_ops);
12578c2ecf20Sopenharmony_ci	if (!hw) {
12588c2ecf20Sopenharmony_ci		dev_crit(&spi->dev, "ieee802154_alloc_hw failed\n");
12598c2ecf20Sopenharmony_ci		return ret;
12608c2ecf20Sopenharmony_ci	}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	/* init mcr20a local data */
12638c2ecf20Sopenharmony_ci	lp = hw->priv;
12648c2ecf20Sopenharmony_ci	lp->hw = hw;
12658c2ecf20Sopenharmony_ci	lp->spi = spi;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	/* init ieee802154_hw */
12688c2ecf20Sopenharmony_ci	hw->parent = &spi->dev;
12698c2ecf20Sopenharmony_ci	ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	/* init buf */
12728c2ecf20Sopenharmony_ci	lp->buf = devm_kzalloc(&spi->dev, SPI_COMMAND_BUFFER, GFP_KERNEL);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	if (!lp->buf) {
12758c2ecf20Sopenharmony_ci		ret = -ENOMEM;
12768c2ecf20Sopenharmony_ci		goto free_dev;
12778c2ecf20Sopenharmony_ci	}
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	mcr20a_setup_tx_spi_messages(lp);
12808c2ecf20Sopenharmony_ci	mcr20a_setup_rx_spi_messages(lp);
12818c2ecf20Sopenharmony_ci	mcr20a_setup_irq_spi_messages(lp);
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	/* setup regmap */
12848c2ecf20Sopenharmony_ci	lp->regmap_dar = devm_regmap_init_spi(spi, &mcr20a_dar_regmap);
12858c2ecf20Sopenharmony_ci	if (IS_ERR(lp->regmap_dar)) {
12868c2ecf20Sopenharmony_ci		ret = PTR_ERR(lp->regmap_dar);
12878c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "Failed to allocate dar map: %d\n",
12888c2ecf20Sopenharmony_ci			ret);
12898c2ecf20Sopenharmony_ci		goto free_dev;
12908c2ecf20Sopenharmony_ci	}
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	lp->regmap_iar = devm_regmap_init_spi(spi, &mcr20a_iar_regmap);
12938c2ecf20Sopenharmony_ci	if (IS_ERR(lp->regmap_iar)) {
12948c2ecf20Sopenharmony_ci		ret = PTR_ERR(lp->regmap_iar);
12958c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "Failed to allocate iar map: %d\n", ret);
12968c2ecf20Sopenharmony_ci		goto free_dev;
12978c2ecf20Sopenharmony_ci	}
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	mcr20a_hw_setup(lp);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	spi_set_drvdata(spi, lp);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	ret = mcr20a_phy_init(lp);
13048c2ecf20Sopenharmony_ci	if (ret < 0) {
13058c2ecf20Sopenharmony_ci		dev_crit(&spi->dev, "mcr20a_phy_init failed\n");
13068c2ecf20Sopenharmony_ci		goto free_dev;
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	irq_type = irq_get_trigger_type(spi->irq);
13108c2ecf20Sopenharmony_ci	if (!irq_type)
13118c2ecf20Sopenharmony_ci		irq_type = IRQF_TRIGGER_FALLING;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	ret = devm_request_irq(&spi->dev, spi->irq, mcr20a_irq_isr,
13148c2ecf20Sopenharmony_ci			       irq_type, dev_name(&spi->dev), lp);
13158c2ecf20Sopenharmony_ci	if (ret) {
13168c2ecf20Sopenharmony_ci		dev_err(&spi->dev, "could not request_irq for mcr20a\n");
13178c2ecf20Sopenharmony_ci		ret = -ENODEV;
13188c2ecf20Sopenharmony_ci		goto free_dev;
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	/* disable_irq by default and wait for starting hardware */
13228c2ecf20Sopenharmony_ci	disable_irq(spi->irq);
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	ret = ieee802154_register_hw(hw);
13258c2ecf20Sopenharmony_ci	if (ret) {
13268c2ecf20Sopenharmony_ci		dev_crit(&spi->dev, "ieee802154_register_hw failed\n");
13278c2ecf20Sopenharmony_ci		goto free_dev;
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	return ret;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cifree_dev:
13338c2ecf20Sopenharmony_ci	ieee802154_free_hw(lp->hw);
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	return ret;
13368c2ecf20Sopenharmony_ci}
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_cistatic int mcr20a_remove(struct spi_device *spi)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	struct mcr20a_local *lp = spi_get_drvdata(spi);
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	dev_dbg(&spi->dev, "%s\n", __func__);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	ieee802154_unregister_hw(lp->hw);
13458c2ecf20Sopenharmony_ci	ieee802154_free_hw(lp->hw);
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	return 0;
13488c2ecf20Sopenharmony_ci}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_cistatic const struct of_device_id mcr20a_of_match[] = {
13518c2ecf20Sopenharmony_ci	{ .compatible = "nxp,mcr20a", },
13528c2ecf20Sopenharmony_ci	{ },
13538c2ecf20Sopenharmony_ci};
13548c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mcr20a_of_match);
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_cistatic const struct spi_device_id mcr20a_device_id[] = {
13578c2ecf20Sopenharmony_ci	{ .name = "mcr20a", },
13588c2ecf20Sopenharmony_ci	{ },
13598c2ecf20Sopenharmony_ci};
13608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, mcr20a_device_id);
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_cistatic struct spi_driver mcr20a_driver = {
13638c2ecf20Sopenharmony_ci	.id_table = mcr20a_device_id,
13648c2ecf20Sopenharmony_ci	.driver = {
13658c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(mcr20a_of_match),
13668c2ecf20Sopenharmony_ci		.name	= "mcr20a",
13678c2ecf20Sopenharmony_ci	},
13688c2ecf20Sopenharmony_ci	.probe      = mcr20a_probe,
13698c2ecf20Sopenharmony_ci	.remove     = mcr20a_remove,
13708c2ecf20Sopenharmony_ci};
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_cimodule_spi_driver(mcr20a_driver);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MCR20A Transceiver Driver");
13758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
13768c2ecf20Sopenharmony_ciMODULE_AUTHOR("Xue Liu <liuxuenetmail@gmail>");
1377