Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Analog Devices ADF7242 Low-Power IEEE 802.15.4 Transceiver
0004  *
0005  * Copyright 2009-2017 Analog Devices Inc.
0006  *
0007  * https://www.analog.com/ADF7242
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/delay.h>
0014 #include <linux/mutex.h>
0015 #include <linux/workqueue.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/firmware.h>
0018 #include <linux/spi/spi.h>
0019 #include <linux/skbuff.h>
0020 #include <linux/of.h>
0021 #include <linux/irq.h>
0022 #include <linux/debugfs.h>
0023 #include <linux/bitops.h>
0024 #include <linux/ieee802154.h>
0025 #include <net/mac802154.h>
0026 #include <net/cfg802154.h>
0027 
0028 #define FIRMWARE "adf7242_firmware.bin"
0029 #define MAX_POLL_LOOPS 200
0030 
0031 /* All Registers */
0032 
0033 #define REG_EXT_CTRL    0x100   /* RW External LNA/PA and internal PA control */
0034 #define REG_TX_FSK_TEST 0x101   /* RW TX FSK test mode configuration */
0035 #define REG_CCA1    0x105   /* RW RSSI threshold for CCA */
0036 #define REG_CCA2    0x106   /* RW CCA mode configuration */
0037 #define REG_BUFFERCFG   0x107   /* RW RX_BUFFER overwrite control */
0038 #define REG_PKT_CFG 0x108   /* RW FCS evaluation configuration */
0039 #define REG_DELAYCFG0   0x109   /* RW RC_RX command to SFD or sync word delay */
0040 #define REG_DELAYCFG1   0x10A   /* RW RC_TX command to TX state */
0041 #define REG_DELAYCFG2   0x10B   /* RW Mac delay extension */
0042 #define REG_SYNC_WORD0  0x10C   /* RW sync word bits [7:0] of [23:0]  */
0043 #define REG_SYNC_WORD1  0x10D   /* RW sync word bits [15:8] of [23:0]  */
0044 #define REG_SYNC_WORD2  0x10E   /* RW sync word bits [23:16] of [23:0]  */
0045 #define REG_SYNC_CONFIG 0x10F   /* RW sync word configuration */
0046 #define REG_RC_CFG  0x13E   /* RW RX / TX packet configuration */
0047 #define REG_RC_VAR44    0x13F   /* RW RESERVED */
0048 #define REG_CH_FREQ0    0x300   /* RW Channel Frequency Settings - Low */
0049 #define REG_CH_FREQ1    0x301   /* RW Channel Frequency Settings - Middle */
0050 #define REG_CH_FREQ2    0x302   /* RW Channel Frequency Settings - High */
0051 #define REG_TX_FD   0x304   /* RW TX Frequency Deviation Register */
0052 #define REG_DM_CFG0 0x305   /* RW RX Discriminator BW Register */
0053 #define REG_TX_M    0x306   /* RW TX Mode Register */
0054 #define REG_RX_M    0x307   /* RW RX Mode Register */
0055 #define REG_RRB     0x30C   /* R RSSI Readback Register */
0056 #define REG_LRB     0x30D   /* R Link Quality Readback Register */
0057 #define REG_DR0     0x30E   /* RW bits [15:8] of [15:0] data rate setting */
0058 #define REG_DR1     0x30F   /* RW bits [7:0] of [15:0] data rate setting */
0059 #define REG_PRAMPG  0x313   /* RW RESERVED */
0060 #define REG_TXPB    0x314   /* RW TX Packet Storage Base Address */
0061 #define REG_RXPB    0x315   /* RW RX Packet Storage Base Address */
0062 #define REG_TMR_CFG0    0x316   /* RW Wake up Timer Conf Register - High */
0063 #define REG_TMR_CFG1    0x317   /* RW Wake up Timer Conf Register - Low */
0064 #define REG_TMR_RLD0    0x318   /* RW Wake up Timer Value Register - High */
0065 #define REG_TMR_RLD1    0x319   /* RW Wake up Timer Value Register - Low  */
0066 #define REG_TMR_CTRL    0x31A   /* RW Wake up Timer Timeout flag */
0067 #define REG_PD_AUX  0x31E   /* RW Battmon enable */
0068 #define REG_GP_CFG  0x32C   /* RW GPIO Configuration */
0069 #define REG_GP_OUT  0x32D   /* RW GPIO Configuration */
0070 #define REG_GP_IN   0x32E   /* R GPIO Configuration */
0071 #define REG_SYNT    0x335   /* RW bandwidth calibration timers */
0072 #define REG_CAL_CFG 0x33D   /* RW Calibration Settings */
0073 #define REG_PA_BIAS 0x36E   /* RW PA BIAS */
0074 #define REG_SYNT_CAL    0x371   /* RW Oscillator and Doubler Configuration */
0075 #define REG_IIRF_CFG    0x389   /* RW BB Filter Decimation Rate */
0076 #define REG_CDR_CFG 0x38A   /* RW CDR kVCO */
0077 #define REG_DM_CFG1 0x38B   /* RW Postdemodulator Filter */
0078 #define REG_AGCSTAT 0x38E   /* R RXBB Ref Osc Calibration Engine Readback */
0079 #define REG_RXCAL0  0x395   /* RW RX BB filter tuning, LSB */
0080 #define REG_RXCAL1  0x396   /* RW RX BB filter tuning, MSB */
0081 #define REG_RXFE_CFG    0x39B   /* RW RXBB Ref Osc & RXFE Calibration */
0082 #define REG_PA_RR   0x3A7   /* RW Set PA ramp rate */
0083 #define REG_PA_CFG  0x3A8   /* RW PA enable */
0084 #define REG_EXTPA_CFG   0x3A9   /* RW External PA BIAS DAC */
0085 #define REG_EXTPA_MSC   0x3AA   /* RW PA Bias Mode */
0086 #define REG_ADC_RBK 0x3AE   /* R Readback temp */
0087 #define REG_AGC_CFG1    0x3B2   /* RW GC Parameters */
0088 #define REG_AGC_MAX 0x3B4   /* RW Slew rate  */
0089 #define REG_AGC_CFG2    0x3B6   /* RW RSSI Parameters */
0090 #define REG_AGC_CFG3    0x3B7   /* RW RSSI Parameters */
0091 #define REG_AGC_CFG4    0x3B8   /* RW RSSI Parameters */
0092 #define REG_AGC_CFG5    0x3B9   /* RW RSSI & NDEC Parameters */
0093 #define REG_AGC_CFG6    0x3BA   /* RW NDEC Parameters */
0094 #define REG_OCL_CFG1    0x3C4   /* RW OCL System Parameters */
0095 #define REG_IRQ1_EN0    0x3C7   /* RW Interrupt Mask set bits for IRQ1 */
0096 #define REG_IRQ1_EN1    0x3C8   /* RW Interrupt Mask set bits for IRQ1 */
0097 #define REG_IRQ2_EN0    0x3C9   /* RW Interrupt Mask set bits for IRQ2 */
0098 #define REG_IRQ2_EN1    0x3CA   /* RW Interrupt Mask set bits for IRQ2 */
0099 #define REG_IRQ1_SRC0   0x3CB   /* RW Interrupt Source bits for IRQ */
0100 #define REG_IRQ1_SRC1   0x3CC   /* RW Interrupt Source bits for IRQ */
0101 #define REG_OCL_BW0 0x3D2   /* RW OCL System Parameters */
0102 #define REG_OCL_BW1 0x3D3   /* RW OCL System Parameters */
0103 #define REG_OCL_BW2 0x3D4   /* RW OCL System Parameters */
0104 #define REG_OCL_BW3 0x3D5   /* RW OCL System Parameters */
0105 #define REG_OCL_BW4 0x3D6   /* RW OCL System Parameters */
0106 #define REG_OCL_BWS 0x3D7   /* RW OCL System Parameters */
0107 #define REG_OCL_CFG13   0x3E0   /* RW OCL System Parameters */
0108 #define REG_GP_DRV  0x3E3   /* RW I/O pads Configuration and bg trim */
0109 #define REG_BM_CFG  0x3E6   /* RW Batt. Monitor Threshold Voltage setting */
0110 #define REG_SFD_15_4    0x3F4   /* RW Option to set non standard SFD */
0111 #define REG_AFC_CFG 0x3F7   /* RW AFC mode and polarity */
0112 #define REG_AFC_KI_KP   0x3F8   /* RW AFC ki and kp */
0113 #define REG_AFC_RANGE   0x3F9   /* RW AFC range */
0114 #define REG_AFC_READ    0x3FA   /* RW Readback frequency error */
0115 
0116 /* REG_EXTPA_MSC */
0117 #define PA_PWR(x)       (((x) & 0xF) << 4)
0118 #define EXTPA_BIAS_SRC      BIT(3)
0119 #define EXTPA_BIAS_MODE(x)  (((x) & 0x7) << 0)
0120 
0121 /* REG_PA_CFG */
0122 #define PA_BRIDGE_DBIAS(x)  (((x) & 0x1F) << 0)
0123 #define PA_DBIAS_HIGH_POWER 21
0124 #define PA_DBIAS_LOW_POWER  13
0125 
0126 /* REG_PA_BIAS */
0127 #define PA_BIAS_CTRL(x)     (((x) & 0x1F) << 1)
0128 #define REG_PA_BIAS_DFL     BIT(0)
0129 #define PA_BIAS_HIGH_POWER  63
0130 #define PA_BIAS_LOW_POWER   55
0131 
0132 #define REG_PAN_ID0     0x112
0133 #define REG_PAN_ID1     0x113
0134 #define REG_SHORT_ADDR_0    0x114
0135 #define REG_SHORT_ADDR_1    0x115
0136 #define REG_IEEE_ADDR_0     0x116
0137 #define REG_IEEE_ADDR_1     0x117
0138 #define REG_IEEE_ADDR_2     0x118
0139 #define REG_IEEE_ADDR_3     0x119
0140 #define REG_IEEE_ADDR_4     0x11A
0141 #define REG_IEEE_ADDR_5     0x11B
0142 #define REG_IEEE_ADDR_6     0x11C
0143 #define REG_IEEE_ADDR_7     0x11D
0144 #define REG_FFILT_CFG       0x11E
0145 #define REG_AUTO_CFG        0x11F
0146 #define REG_AUTO_TX1        0x120
0147 #define REG_AUTO_TX2        0x121
0148 #define REG_AUTO_STATUS     0x122
0149 
0150 /* REG_FFILT_CFG */
0151 #define ACCEPT_BEACON_FRAMES   BIT(0)
0152 #define ACCEPT_DATA_FRAMES     BIT(1)
0153 #define ACCEPT_ACK_FRAMES      BIT(2)
0154 #define ACCEPT_MACCMD_FRAMES   BIT(3)
0155 #define ACCEPT_RESERVED_FRAMES BIT(4)
0156 #define ACCEPT_ALL_ADDRESS     BIT(5)
0157 
0158 /* REG_AUTO_CFG */
0159 #define AUTO_ACK_FRAMEPEND     BIT(0)
0160 #define IS_PANCOORD        BIT(1)
0161 #define RX_AUTO_ACK_EN         BIT(3)
0162 #define CSMA_CA_RX_TURNAROUND  BIT(4)
0163 
0164 /* REG_AUTO_TX1 */
0165 #define MAX_FRAME_RETRIES(x)   ((x) & 0xF)
0166 #define MAX_CCA_RETRIES(x)     (((x) & 0x7) << 4)
0167 
0168 /* REG_AUTO_TX2 */
0169 #define CSMA_MAX_BE(x)         ((x) & 0xF)
0170 #define CSMA_MIN_BE(x)         (((x) & 0xF) << 4)
0171 
0172 #define CMD_SPI_NOP     0xFF /* No operation. Use for dummy writes */
0173 #define CMD_SPI_PKT_WR      0x10 /* Write telegram to the Packet RAM
0174                       * starting from the TX packet base address
0175                       * pointer tx_packet_base
0176                       */
0177 #define CMD_SPI_PKT_RD      0x30 /* Read telegram from the Packet RAM
0178                       * starting from RX packet base address
0179                       * pointer rxpb.rx_packet_base
0180                       */
0181 #define CMD_SPI_MEM_WR(x)   (0x18 + (x >> 8)) /* Write data to MCR or
0182                            * Packet RAM sequentially
0183                            */
0184 #define CMD_SPI_MEM_RD(x)   (0x38 + (x >> 8)) /* Read data from MCR or
0185                            * Packet RAM sequentially
0186                            */
0187 #define CMD_SPI_MEMR_WR(x)  (0x08 + (x >> 8)) /* Write data to MCR or Packet
0188                            * RAM as random block
0189                            */
0190 #define CMD_SPI_MEMR_RD(x)  (0x28 + (x >> 8)) /* Read data from MCR or
0191                            * Packet RAM random block
0192                            */
0193 #define CMD_SPI_PRAM_WR     0x1E /* Write data sequentially to current
0194                       * PRAM page selected
0195                       */
0196 #define CMD_SPI_PRAM_RD     0x3E /* Read data sequentially from current
0197                       * PRAM page selected
0198                       */
0199 #define CMD_RC_SLEEP        0xB1 /* Invoke transition of radio controller
0200                       * into SLEEP state
0201                       */
0202 #define CMD_RC_IDLE     0xB2 /* Invoke transition of radio controller
0203                       * into IDLE state
0204                       */
0205 #define CMD_RC_PHY_RDY      0xB3 /* Invoke transition of radio controller
0206                       * into PHY_RDY state
0207                       */
0208 #define CMD_RC_RX       0xB4 /* Invoke transition of radio controller
0209                       * into RX state
0210                       */
0211 #define CMD_RC_TX       0xB5 /* Invoke transition of radio controller
0212                       * into TX state
0213                       */
0214 #define CMD_RC_MEAS     0xB6 /* Invoke transition of radio controller
0215                       * into MEAS state
0216                       */
0217 #define CMD_RC_CCA      0xB7 /* Invoke Clear channel assessment */
0218 #define CMD_RC_CSMACA       0xC1 /* initiates CSMA-CA channel access
0219                       * sequence and frame transmission
0220                       */
0221 #define CMD_RC_PC_RESET     0xC7 /* Program counter reset */
0222 #define CMD_RC_RESET        0xC8 /* Resets the ADF7242 and puts it in
0223                       * the sleep state
0224                       */
0225 #define CMD_RC_PC_RESET_NO_WAIT (CMD_RC_PC_RESET | BIT(31))
0226 
0227 /* STATUS */
0228 
0229 #define STAT_SPI_READY      BIT(7)
0230 #define STAT_IRQ_STATUS     BIT(6)
0231 #define STAT_RC_READY       BIT(5)
0232 #define STAT_CCA_RESULT     BIT(4)
0233 #define RC_STATUS_IDLE      1
0234 #define RC_STATUS_MEAS      2
0235 #define RC_STATUS_PHY_RDY   3
0236 #define RC_STATUS_RX        4
0237 #define RC_STATUS_TX        5
0238 #define RC_STATUS_MASK      0xF
0239 
0240 /* AUTO_STATUS */
0241 
0242 #define SUCCESS         0
0243 #define SUCCESS_DATPEND     1
0244 #define FAILURE_CSMACA      2
0245 #define FAILURE_NOACK       3
0246 #define AUTO_STATUS_MASK    0x3
0247 
0248 #define PRAM_PAGESIZE       256
0249 
0250 /* IRQ1 */
0251 
0252 #define IRQ_CCA_COMPLETE    BIT(0)
0253 #define IRQ_SFD_RX      BIT(1)
0254 #define IRQ_SFD_TX      BIT(2)
0255 #define IRQ_RX_PKT_RCVD     BIT(3)
0256 #define IRQ_TX_PKT_SENT     BIT(4)
0257 #define IRQ_FRAME_VALID     BIT(5)
0258 #define IRQ_ADDRESS_VALID   BIT(6)
0259 #define IRQ_CSMA_CA     BIT(7)
0260 
0261 #define AUTO_TX_TURNAROUND  BIT(3)
0262 #define ADDON_EN        BIT(4)
0263 
0264 #define FLAG_XMIT       0
0265 #define FLAG_START      1
0266 
0267 #define ADF7242_REPORT_CSMA_CA_STAT 0 /* framework doesn't handle yet */
0268 
0269 struct adf7242_local {
0270     struct spi_device *spi;
0271     struct completion tx_complete;
0272     struct ieee802154_hw *hw;
0273     struct mutex bmux; /* protect SPI messages */
0274     struct spi_message stat_msg;
0275     struct spi_transfer stat_xfer;
0276     struct dentry *debugfs_root;
0277     struct delayed_work work;
0278     struct workqueue_struct *wqueue;
0279     unsigned long flags;
0280     int tx_stat;
0281     bool promiscuous;
0282     s8 rssi;
0283     u8 max_frame_retries;
0284     u8 max_cca_retries;
0285     u8 max_be;
0286     u8 min_be;
0287 
0288     /* DMA (thus cache coherency maintenance) requires the
0289      * transfer buffers to live in their own cache lines.
0290      */
0291 
0292     u8 buf[3] ____cacheline_aligned;
0293     u8 buf_reg_tx[3];
0294     u8 buf_read_tx[4];
0295     u8 buf_read_rx[4];
0296     u8 buf_stat_rx;
0297     u8 buf_stat_tx;
0298     u8 buf_cmd;
0299 };
0300 
0301 static int adf7242_soft_reset(struct adf7242_local *lp, int line);
0302 
0303 static int adf7242_status(struct adf7242_local *lp, u8 *stat)
0304 {
0305     int status;
0306 
0307     mutex_lock(&lp->bmux);
0308     status = spi_sync(lp->spi, &lp->stat_msg);
0309     *stat = lp->buf_stat_rx;
0310     mutex_unlock(&lp->bmux);
0311 
0312     return status;
0313 }
0314 
0315 static int adf7242_wait_status(struct adf7242_local *lp, unsigned int status,
0316                    unsigned int mask, int line)
0317 {
0318     int cnt = 0, ret = 0;
0319     u8 stat;
0320 
0321     do {
0322         adf7242_status(lp, &stat);
0323         cnt++;
0324     } while (((stat & mask) != status) && (cnt < MAX_POLL_LOOPS));
0325 
0326     if (cnt >= MAX_POLL_LOOPS) {
0327         ret = -ETIMEDOUT;
0328 
0329         if (!(stat & STAT_RC_READY)) {
0330             adf7242_soft_reset(lp, line);
0331             adf7242_status(lp, &stat);
0332 
0333             if ((stat & mask) == status)
0334                 ret = 0;
0335         }
0336 
0337         if (ret < 0)
0338             dev_warn(&lp->spi->dev,
0339                  "%s:line %d Timeout status 0x%x (%d)\n",
0340                  __func__, line, stat, cnt);
0341     }
0342 
0343     dev_vdbg(&lp->spi->dev, "%s : loops=%d line %d\n", __func__, cnt, line);
0344 
0345     return ret;
0346 }
0347 
0348 static int adf7242_wait_rc_ready(struct adf7242_local *lp, int line)
0349 {
0350     return adf7242_wait_status(lp, STAT_RC_READY | STAT_SPI_READY,
0351                    STAT_RC_READY | STAT_SPI_READY, line);
0352 }
0353 
0354 static int adf7242_wait_spi_ready(struct adf7242_local *lp, int line)
0355 {
0356     return adf7242_wait_status(lp, STAT_SPI_READY,
0357                    STAT_SPI_READY, line);
0358 }
0359 
0360 static int adf7242_write_fbuf(struct adf7242_local *lp, u8 *data, u8 len)
0361 {
0362     u8 *buf = lp->buf;
0363     int status;
0364     struct spi_message msg;
0365     struct spi_transfer xfer_head = {
0366         .len = 2,
0367         .tx_buf = buf,
0368 
0369     };
0370     struct spi_transfer xfer_buf = {
0371         .len = len,
0372         .tx_buf = data,
0373     };
0374 
0375     spi_message_init(&msg);
0376     spi_message_add_tail(&xfer_head, &msg);
0377     spi_message_add_tail(&xfer_buf, &msg);
0378 
0379     adf7242_wait_spi_ready(lp, __LINE__);
0380 
0381     mutex_lock(&lp->bmux);
0382     buf[0] = CMD_SPI_PKT_WR;
0383     buf[1] = len + 2;
0384 
0385     status = spi_sync(lp->spi, &msg);
0386     mutex_unlock(&lp->bmux);
0387 
0388     return status;
0389 }
0390 
0391 static int adf7242_read_fbuf(struct adf7242_local *lp,
0392                  u8 *data, size_t len, bool packet_read)
0393 {
0394     u8 *buf = lp->buf;
0395     int status;
0396     struct spi_message msg;
0397     struct spi_transfer xfer_head = {
0398         .len = 3,
0399         .tx_buf = buf,
0400         .rx_buf = buf,
0401     };
0402     struct spi_transfer xfer_buf = {
0403         .len = len,
0404         .rx_buf = data,
0405     };
0406 
0407     spi_message_init(&msg);
0408     spi_message_add_tail(&xfer_head, &msg);
0409     spi_message_add_tail(&xfer_buf, &msg);
0410 
0411     adf7242_wait_spi_ready(lp, __LINE__);
0412 
0413     mutex_lock(&lp->bmux);
0414     if (packet_read) {
0415         buf[0] = CMD_SPI_PKT_RD;
0416         buf[1] = CMD_SPI_NOP;
0417         buf[2] = 0; /* PHR */
0418     } else {
0419         buf[0] = CMD_SPI_PRAM_RD;
0420         buf[1] = 0;
0421         buf[2] = CMD_SPI_NOP;
0422     }
0423 
0424     status = spi_sync(lp->spi, &msg);
0425 
0426     mutex_unlock(&lp->bmux);
0427 
0428     return status;
0429 }
0430 
0431 static int adf7242_read_reg(struct adf7242_local *lp, u16 addr, u8 *data)
0432 {
0433     int status;
0434     struct spi_message msg;
0435 
0436     struct spi_transfer xfer = {
0437         .len = 4,
0438         .tx_buf = lp->buf_read_tx,
0439         .rx_buf = lp->buf_read_rx,
0440     };
0441 
0442     adf7242_wait_spi_ready(lp, __LINE__);
0443 
0444     mutex_lock(&lp->bmux);
0445     lp->buf_read_tx[0] = CMD_SPI_MEM_RD(addr);
0446     lp->buf_read_tx[1] = addr;
0447     lp->buf_read_tx[2] = CMD_SPI_NOP;
0448     lp->buf_read_tx[3] = CMD_SPI_NOP;
0449 
0450     spi_message_init(&msg);
0451     spi_message_add_tail(&xfer, &msg);
0452 
0453     status = spi_sync(lp->spi, &msg);
0454     if (msg.status)
0455         status = msg.status;
0456 
0457     if (!status)
0458         *data = lp->buf_read_rx[3];
0459 
0460     mutex_unlock(&lp->bmux);
0461 
0462     dev_vdbg(&lp->spi->dev, "%s : REG 0x%X, VAL 0x%X\n", __func__,
0463          addr, *data);
0464 
0465     return status;
0466 }
0467 
0468 static int adf7242_write_reg(struct adf7242_local *lp, u16 addr, u8 data)
0469 {
0470     int status;
0471 
0472     adf7242_wait_spi_ready(lp, __LINE__);
0473 
0474     mutex_lock(&lp->bmux);
0475     lp->buf_reg_tx[0] = CMD_SPI_MEM_WR(addr);
0476     lp->buf_reg_tx[1] = addr;
0477     lp->buf_reg_tx[2] = data;
0478     status = spi_write(lp->spi, lp->buf_reg_tx, 3);
0479     mutex_unlock(&lp->bmux);
0480 
0481     dev_vdbg(&lp->spi->dev, "%s : REG 0x%X, VAL 0x%X\n",
0482          __func__, addr, data);
0483 
0484     return status;
0485 }
0486 
0487 static int adf7242_cmd(struct adf7242_local *lp, unsigned int cmd)
0488 {
0489     int status;
0490 
0491     dev_vdbg(&lp->spi->dev, "%s : CMD=0x%X\n", __func__, cmd);
0492 
0493     if (cmd != CMD_RC_PC_RESET_NO_WAIT)
0494         adf7242_wait_rc_ready(lp, __LINE__);
0495 
0496     mutex_lock(&lp->bmux);
0497     lp->buf_cmd = cmd;
0498     status = spi_write(lp->spi, &lp->buf_cmd, 1);
0499     mutex_unlock(&lp->bmux);
0500 
0501     return status;
0502 }
0503 
0504 static int adf7242_upload_firmware(struct adf7242_local *lp, u8 *data, u16 len)
0505 {
0506     struct spi_message msg;
0507     struct spi_transfer xfer_buf = { };
0508     int status, i, page = 0;
0509     u8 *buf = lp->buf;
0510 
0511     struct spi_transfer xfer_head = {
0512         .len = 2,
0513         .tx_buf = buf,
0514     };
0515 
0516     buf[0] = CMD_SPI_PRAM_WR;
0517     buf[1] = 0;
0518 
0519     spi_message_init(&msg);
0520     spi_message_add_tail(&xfer_head, &msg);
0521     spi_message_add_tail(&xfer_buf, &msg);
0522 
0523     for (i = len; i >= 0; i -= PRAM_PAGESIZE) {
0524         adf7242_write_reg(lp, REG_PRAMPG, page);
0525 
0526         xfer_buf.len = (i >= PRAM_PAGESIZE) ? PRAM_PAGESIZE : i;
0527         xfer_buf.tx_buf = &data[page * PRAM_PAGESIZE];
0528 
0529         mutex_lock(&lp->bmux);
0530         status = spi_sync(lp->spi, &msg);
0531         mutex_unlock(&lp->bmux);
0532         page++;
0533     }
0534 
0535     return status;
0536 }
0537 
0538 static int adf7242_verify_firmware(struct adf7242_local *lp,
0539                    const u8 *data, size_t len)
0540 {
0541 #ifdef DEBUG
0542     int i, j;
0543     unsigned int page;
0544     u8 *buf = kmalloc(PRAM_PAGESIZE, GFP_KERNEL);
0545 
0546     if (!buf)
0547         return -ENOMEM;
0548 
0549     for (page = 0, i = len; i >= 0; i -= PRAM_PAGESIZE, page++) {
0550         size_t nb = (i >= PRAM_PAGESIZE) ? PRAM_PAGESIZE : i;
0551 
0552         adf7242_write_reg(lp, REG_PRAMPG, page);
0553         adf7242_read_fbuf(lp, buf, nb, false);
0554 
0555         for (j = 0; j < nb; j++) {
0556             if (buf[j] != data[page * PRAM_PAGESIZE + j]) {
0557                 kfree(buf);
0558                 return -EIO;
0559             }
0560         }
0561     }
0562     kfree(buf);
0563 #endif
0564     return 0;
0565 }
0566 
0567 static void adf7242_clear_irqstat(struct adf7242_local *lp)
0568 {
0569     adf7242_write_reg(lp, REG_IRQ1_SRC1, IRQ_CCA_COMPLETE | IRQ_SFD_RX |
0570               IRQ_SFD_TX | IRQ_RX_PKT_RCVD | IRQ_TX_PKT_SENT |
0571               IRQ_FRAME_VALID | IRQ_ADDRESS_VALID | IRQ_CSMA_CA);
0572 }
0573 
0574 static int adf7242_cmd_rx(struct adf7242_local *lp)
0575 {
0576     /* Wait until the ACK is sent */
0577     adf7242_wait_status(lp, RC_STATUS_PHY_RDY, RC_STATUS_MASK, __LINE__);
0578     adf7242_clear_irqstat(lp);
0579     mod_delayed_work(lp->wqueue, &lp->work, msecs_to_jiffies(400));
0580 
0581     return adf7242_cmd(lp, CMD_RC_RX);
0582 }
0583 
0584 static void adf7242_rx_cal_work(struct work_struct *work)
0585 {
0586     struct adf7242_local *lp =
0587     container_of(work, struct adf7242_local, work.work);
0588 
0589     /* Reissuing RC_RX every 400ms - to adjust for offset
0590      * drift in receiver (datasheet page 61, OCL section)
0591      */
0592 
0593     if (!test_bit(FLAG_XMIT, &lp->flags)) {
0594         adf7242_cmd(lp, CMD_RC_PHY_RDY);
0595         adf7242_cmd_rx(lp);
0596     }
0597 }
0598 
0599 static int adf7242_set_txpower(struct ieee802154_hw *hw, int mbm)
0600 {
0601     struct adf7242_local *lp = hw->priv;
0602     u8 pwr, bias_ctrl, dbias, tmp;
0603     int db = mbm / 100;
0604 
0605     dev_vdbg(&lp->spi->dev, "%s : Power %d dB\n", __func__, db);
0606 
0607     if (db > 5 || db < -26)
0608         return -EINVAL;
0609 
0610     db = DIV_ROUND_CLOSEST(db + 29, 2);
0611 
0612     if (db > 15) {
0613         dbias = PA_DBIAS_HIGH_POWER;
0614         bias_ctrl = PA_BIAS_HIGH_POWER;
0615     } else {
0616         dbias = PA_DBIAS_LOW_POWER;
0617         bias_ctrl = PA_BIAS_LOW_POWER;
0618     }
0619 
0620     pwr = clamp_t(u8, db, 3, 15);
0621 
0622     adf7242_read_reg(lp, REG_PA_CFG, &tmp);
0623     tmp &= ~PA_BRIDGE_DBIAS(~0);
0624     tmp |= PA_BRIDGE_DBIAS(dbias);
0625     adf7242_write_reg(lp, REG_PA_CFG, tmp);
0626 
0627     adf7242_read_reg(lp, REG_PA_BIAS, &tmp);
0628     tmp &= ~PA_BIAS_CTRL(~0);
0629     tmp |= PA_BIAS_CTRL(bias_ctrl);
0630     adf7242_write_reg(lp, REG_PA_BIAS, tmp);
0631 
0632     adf7242_read_reg(lp, REG_EXTPA_MSC, &tmp);
0633     tmp &= ~PA_PWR(~0);
0634     tmp |= PA_PWR(pwr);
0635 
0636     return adf7242_write_reg(lp, REG_EXTPA_MSC, tmp);
0637 }
0638 
0639 static int adf7242_set_csma_params(struct ieee802154_hw *hw, u8 min_be,
0640                    u8 max_be, u8 retries)
0641 {
0642     struct adf7242_local *lp = hw->priv;
0643     int ret;
0644 
0645     dev_vdbg(&lp->spi->dev, "%s : min_be=%d max_be=%d retries=%d\n",
0646          __func__, min_be, max_be, retries);
0647 
0648     if (min_be > max_be || max_be > 8 || retries > 5)
0649         return -EINVAL;
0650 
0651     ret = adf7242_write_reg(lp, REG_AUTO_TX1,
0652                 MAX_FRAME_RETRIES(lp->max_frame_retries) |
0653                 MAX_CCA_RETRIES(retries));
0654     if (ret)
0655         return ret;
0656 
0657     lp->max_cca_retries = retries;
0658     lp->max_be = max_be;
0659     lp->min_be = min_be;
0660 
0661     return adf7242_write_reg(lp, REG_AUTO_TX2, CSMA_MAX_BE(max_be) |
0662             CSMA_MIN_BE(min_be));
0663 }
0664 
0665 static int adf7242_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
0666 {
0667     struct adf7242_local *lp = hw->priv;
0668     int ret = 0;
0669 
0670     dev_vdbg(&lp->spi->dev, "%s : Retries = %d\n", __func__, retries);
0671 
0672     if (retries < -1 || retries > 15)
0673         return -EINVAL;
0674 
0675     if (retries >= 0)
0676         ret = adf7242_write_reg(lp, REG_AUTO_TX1,
0677                     MAX_FRAME_RETRIES(retries) |
0678                     MAX_CCA_RETRIES(lp->max_cca_retries));
0679 
0680     lp->max_frame_retries = retries;
0681 
0682     return ret;
0683 }
0684 
0685 static int adf7242_ed(struct ieee802154_hw *hw, u8 *level)
0686 {
0687     struct adf7242_local *lp = hw->priv;
0688 
0689     *level = lp->rssi;
0690 
0691     dev_vdbg(&lp->spi->dev, "%s :Exit level=%d\n",
0692          __func__, *level);
0693 
0694     return 0;
0695 }
0696 
0697 static int adf7242_start(struct ieee802154_hw *hw)
0698 {
0699     struct adf7242_local *lp = hw->priv;
0700 
0701     adf7242_cmd(lp, CMD_RC_PHY_RDY);
0702     adf7242_clear_irqstat(lp);
0703     enable_irq(lp->spi->irq);
0704     set_bit(FLAG_START, &lp->flags);
0705 
0706     return adf7242_cmd_rx(lp);
0707 }
0708 
0709 static void adf7242_stop(struct ieee802154_hw *hw)
0710 {
0711     struct adf7242_local *lp = hw->priv;
0712 
0713     disable_irq(lp->spi->irq);
0714     cancel_delayed_work_sync(&lp->work);
0715     adf7242_cmd(lp, CMD_RC_IDLE);
0716     clear_bit(FLAG_START, &lp->flags);
0717     adf7242_clear_irqstat(lp);
0718 }
0719 
0720 static int adf7242_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
0721 {
0722     struct adf7242_local *lp = hw->priv;
0723     unsigned long freq;
0724 
0725     dev_dbg(&lp->spi->dev, "%s :Channel=%d\n", __func__, channel);
0726 
0727     might_sleep();
0728 
0729     WARN_ON(page != 0);
0730     WARN_ON(channel < 11);
0731     WARN_ON(channel > 26);
0732 
0733     freq = (2405 + 5 * (channel - 11)) * 100;
0734     adf7242_cmd(lp, CMD_RC_PHY_RDY);
0735 
0736     adf7242_write_reg(lp, REG_CH_FREQ0, freq);
0737     adf7242_write_reg(lp, REG_CH_FREQ1, freq >> 8);
0738     adf7242_write_reg(lp, REG_CH_FREQ2, freq >> 16);
0739 
0740     if (test_bit(FLAG_START, &lp->flags))
0741         return adf7242_cmd_rx(lp);
0742     else
0743         return adf7242_cmd(lp, CMD_RC_PHY_RDY);
0744 }
0745 
0746 static int adf7242_set_hw_addr_filt(struct ieee802154_hw *hw,
0747                     struct ieee802154_hw_addr_filt *filt,
0748                     unsigned long changed)
0749 {
0750     struct adf7242_local *lp = hw->priv;
0751     u8 reg;
0752 
0753     dev_dbg(&lp->spi->dev, "%s :Changed=0x%lX\n", __func__, changed);
0754 
0755     might_sleep();
0756 
0757     if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
0758         u8 addr[8], i;
0759 
0760         memcpy(addr, &filt->ieee_addr, 8);
0761 
0762         for (i = 0; i < 8; i++)
0763             adf7242_write_reg(lp, REG_IEEE_ADDR_0 + i, addr[i]);
0764     }
0765 
0766     if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
0767         u16 saddr = le16_to_cpu(filt->short_addr);
0768 
0769         adf7242_write_reg(lp, REG_SHORT_ADDR_0, saddr);
0770         adf7242_write_reg(lp, REG_SHORT_ADDR_1, saddr >> 8);
0771     }
0772 
0773     if (changed & IEEE802154_AFILT_PANID_CHANGED) {
0774         u16 pan_id = le16_to_cpu(filt->pan_id);
0775 
0776         adf7242_write_reg(lp, REG_PAN_ID0, pan_id);
0777         adf7242_write_reg(lp, REG_PAN_ID1, pan_id >> 8);
0778     }
0779 
0780     if (changed & IEEE802154_AFILT_PANC_CHANGED) {
0781         adf7242_read_reg(lp, REG_AUTO_CFG, &reg);
0782         if (filt->pan_coord)
0783             reg |= IS_PANCOORD;
0784         else
0785             reg &= ~IS_PANCOORD;
0786         adf7242_write_reg(lp, REG_AUTO_CFG, reg);
0787     }
0788 
0789     return 0;
0790 }
0791 
0792 static int adf7242_set_promiscuous_mode(struct ieee802154_hw *hw, bool on)
0793 {
0794     struct adf7242_local *lp = hw->priv;
0795 
0796     dev_dbg(&lp->spi->dev, "%s : mode %d\n", __func__, on);
0797 
0798     lp->promiscuous = on;
0799 
0800     if (on) {
0801         adf7242_write_reg(lp, REG_AUTO_CFG, 0);
0802         return adf7242_write_reg(lp, REG_FFILT_CFG,
0803                   ACCEPT_BEACON_FRAMES |
0804                   ACCEPT_DATA_FRAMES |
0805                   ACCEPT_MACCMD_FRAMES |
0806                   ACCEPT_ALL_ADDRESS |
0807                   ACCEPT_ACK_FRAMES |
0808                   ACCEPT_RESERVED_FRAMES);
0809     } else {
0810         adf7242_write_reg(lp, REG_FFILT_CFG,
0811                   ACCEPT_BEACON_FRAMES |
0812                   ACCEPT_DATA_FRAMES |
0813                   ACCEPT_MACCMD_FRAMES |
0814                   ACCEPT_RESERVED_FRAMES);
0815 
0816         return adf7242_write_reg(lp, REG_AUTO_CFG, RX_AUTO_ACK_EN);
0817     }
0818 }
0819 
0820 static int adf7242_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
0821 {
0822     struct adf7242_local *lp = hw->priv;
0823     s8 level = clamp_t(s8, mbm / 100, S8_MIN, S8_MAX);
0824 
0825     dev_dbg(&lp->spi->dev, "%s : level %d\n", __func__, level);
0826 
0827     return adf7242_write_reg(lp, REG_CCA1, level);
0828 }
0829 
0830 static int adf7242_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
0831 {
0832     struct adf7242_local *lp = hw->priv;
0833     int ret;
0834 
0835     /* ensure existing instances of the IRQ handler have completed */
0836     disable_irq(lp->spi->irq);
0837     set_bit(FLAG_XMIT, &lp->flags);
0838     cancel_delayed_work_sync(&lp->work);
0839     reinit_completion(&lp->tx_complete);
0840     adf7242_cmd(lp, CMD_RC_PHY_RDY);
0841     adf7242_clear_irqstat(lp);
0842 
0843     ret = adf7242_write_fbuf(lp, skb->data, skb->len);
0844     if (ret)
0845         goto err;
0846 
0847     ret = adf7242_cmd(lp, CMD_RC_CSMACA);
0848     if (ret)
0849         goto err;
0850     enable_irq(lp->spi->irq);
0851 
0852     ret = wait_for_completion_interruptible_timeout(&lp->tx_complete,
0853                             HZ / 10);
0854     if (ret < 0)
0855         goto err;
0856     if (ret == 0) {
0857         dev_dbg(&lp->spi->dev, "Timeout waiting for TX interrupt\n");
0858         ret = -ETIMEDOUT;
0859         goto err;
0860     }
0861 
0862     if (lp->tx_stat != SUCCESS) {
0863         dev_dbg(&lp->spi->dev,
0864             "Error xmit: Retry count exceeded Status=0x%x\n",
0865             lp->tx_stat);
0866         ret = -ECOMM;
0867     } else {
0868         ret = 0;
0869     }
0870 
0871 err:
0872     clear_bit(FLAG_XMIT, &lp->flags);
0873     adf7242_cmd_rx(lp);
0874 
0875     return ret;
0876 }
0877 
0878 static int adf7242_rx(struct adf7242_local *lp)
0879 {
0880     struct sk_buff *skb;
0881     size_t len;
0882     int ret;
0883     u8 lqi, len_u8, *data;
0884 
0885     ret = adf7242_read_reg(lp, 0, &len_u8);
0886     if (ret)
0887         return ret;
0888 
0889     len = len_u8;
0890 
0891     if (!ieee802154_is_valid_psdu_len(len)) {
0892         dev_dbg(&lp->spi->dev,
0893             "corrupted frame received len %d\n", (int)len);
0894         len = IEEE802154_MTU;
0895     }
0896 
0897     skb = dev_alloc_skb(len);
0898     if (!skb) {
0899         adf7242_cmd_rx(lp);
0900         return -ENOMEM;
0901     }
0902 
0903     data = skb_put(skb, len);
0904     ret = adf7242_read_fbuf(lp, data, len, true);
0905     if (ret < 0) {
0906         kfree_skb(skb);
0907         adf7242_cmd_rx(lp);
0908         return ret;
0909     }
0910 
0911     lqi = data[len - 2];
0912     lp->rssi = data[len - 1];
0913 
0914     ret = adf7242_cmd_rx(lp);
0915 
0916     skb_trim(skb, len - 2); /* Don't put RSSI/LQI or CRC into the frame */
0917 
0918     ieee802154_rx_irqsafe(lp->hw, skb, lqi);
0919 
0920     dev_dbg(&lp->spi->dev, "%s: ret=%d len=%d lqi=%d rssi=%d\n",
0921         __func__, ret, (int)len, (int)lqi, lp->rssi);
0922 
0923     return ret;
0924 }
0925 
0926 static const struct ieee802154_ops adf7242_ops = {
0927     .owner = THIS_MODULE,
0928     .xmit_sync = adf7242_xmit,
0929     .ed = adf7242_ed,
0930     .set_channel = adf7242_channel,
0931     .set_hw_addr_filt = adf7242_set_hw_addr_filt,
0932     .start = adf7242_start,
0933     .stop = adf7242_stop,
0934     .set_csma_params = adf7242_set_csma_params,
0935     .set_frame_retries = adf7242_set_frame_retries,
0936     .set_txpower = adf7242_set_txpower,
0937     .set_promiscuous_mode = adf7242_set_promiscuous_mode,
0938     .set_cca_ed_level = adf7242_set_cca_ed_level,
0939 };
0940 
0941 static void adf7242_debug(struct adf7242_local *lp, u8 irq1)
0942 {
0943 #ifdef DEBUG
0944     u8 stat;
0945 
0946     adf7242_status(lp, &stat);
0947 
0948     dev_dbg(&lp->spi->dev, "%s IRQ1 = %X:\n%s%s%s%s%s%s%s%s\n",
0949         __func__, irq1,
0950         irq1 & IRQ_CCA_COMPLETE ? "IRQ_CCA_COMPLETE\n" : "",
0951         irq1 & IRQ_SFD_RX ? "IRQ_SFD_RX\n" : "",
0952         irq1 & IRQ_SFD_TX ? "IRQ_SFD_TX\n" : "",
0953         irq1 & IRQ_RX_PKT_RCVD ? "IRQ_RX_PKT_RCVD\n" : "",
0954         irq1 & IRQ_TX_PKT_SENT ? "IRQ_TX_PKT_SENT\n" : "",
0955         irq1 & IRQ_CSMA_CA ? "IRQ_CSMA_CA\n" : "",
0956         irq1 & IRQ_FRAME_VALID ? "IRQ_FRAME_VALID\n" : "",
0957         irq1 & IRQ_ADDRESS_VALID ? "IRQ_ADDRESS_VALID\n" : "");
0958 
0959     dev_dbg(&lp->spi->dev, "%s STATUS = %X:\n%s\n%s\n%s\n%s\n%s%s%s%s%s\n",
0960         __func__, stat,
0961         stat & STAT_SPI_READY ? "SPI_READY" : "SPI_BUSY",
0962         stat & STAT_IRQ_STATUS ? "IRQ_PENDING" : "IRQ_CLEAR",
0963         stat & STAT_RC_READY ? "RC_READY" : "RC_BUSY",
0964         stat & STAT_CCA_RESULT ? "CHAN_IDLE" : "CHAN_BUSY",
0965         (stat & 0xf) == RC_STATUS_IDLE ? "RC_STATUS_IDLE" : "",
0966         (stat & 0xf) == RC_STATUS_MEAS ? "RC_STATUS_MEAS" : "",
0967         (stat & 0xf) == RC_STATUS_PHY_RDY ? "RC_STATUS_PHY_RDY" : "",
0968         (stat & 0xf) == RC_STATUS_RX ? "RC_STATUS_RX" : "",
0969         (stat & 0xf) == RC_STATUS_TX ? "RC_STATUS_TX" : "");
0970 #endif
0971 }
0972 
0973 static irqreturn_t adf7242_isr(int irq, void *data)
0974 {
0975     struct adf7242_local *lp = data;
0976     unsigned int xmit;
0977     u8 irq1;
0978 
0979     mod_delayed_work(lp->wqueue, &lp->work, msecs_to_jiffies(400));
0980     adf7242_read_reg(lp, REG_IRQ1_SRC1, &irq1);
0981 
0982     if (!(irq1 & (IRQ_RX_PKT_RCVD | IRQ_CSMA_CA)))
0983         dev_err(&lp->spi->dev, "%s :ERROR IRQ1 = 0x%X\n",
0984             __func__, irq1);
0985 
0986     adf7242_debug(lp, irq1);
0987 
0988     xmit = test_bit(FLAG_XMIT, &lp->flags);
0989 
0990     if (xmit && (irq1 & IRQ_CSMA_CA)) {
0991         adf7242_wait_status(lp, RC_STATUS_PHY_RDY,
0992                     RC_STATUS_MASK, __LINE__);
0993 
0994         if (ADF7242_REPORT_CSMA_CA_STAT) {
0995             u8 astat;
0996 
0997             adf7242_read_reg(lp, REG_AUTO_STATUS, &astat);
0998             astat &= AUTO_STATUS_MASK;
0999 
1000             dev_dbg(&lp->spi->dev, "AUTO_STATUS = %X:\n%s%s%s%s\n",
1001                 astat,
1002                 astat == SUCCESS ? "SUCCESS" : "",
1003                 astat ==
1004                 SUCCESS_DATPEND ? "SUCCESS_DATPEND" : "",
1005                 astat == FAILURE_CSMACA ? "FAILURE_CSMACA" : "",
1006                 astat == FAILURE_NOACK ? "FAILURE_NOACK" : "");
1007 
1008             /* save CSMA-CA completion status */
1009             lp->tx_stat = astat;
1010         } else {
1011             lp->tx_stat = SUCCESS;
1012         }
1013         complete(&lp->tx_complete);
1014         adf7242_clear_irqstat(lp);
1015     } else if (!xmit && (irq1 & IRQ_RX_PKT_RCVD) &&
1016            (irq1 & IRQ_FRAME_VALID)) {
1017         adf7242_rx(lp);
1018     } else if (!xmit && test_bit(FLAG_START, &lp->flags)) {
1019         /* Invalid packet received - drop it and restart */
1020         dev_dbg(&lp->spi->dev, "%s:%d : ERROR IRQ1 = 0x%X\n",
1021             __func__, __LINE__, irq1);
1022         adf7242_cmd(lp, CMD_RC_PHY_RDY);
1023         adf7242_cmd_rx(lp);
1024     } else {
1025         /* This can only be xmit without IRQ, likely a RX packet.
1026          * we get an TX IRQ shortly - do nothing or let the xmit
1027          * timeout handle this
1028          */
1029 
1030         dev_dbg(&lp->spi->dev, "%s:%d : ERROR IRQ1 = 0x%X, xmit %d\n",
1031             __func__, __LINE__, irq1, xmit);
1032         adf7242_wait_status(lp, RC_STATUS_PHY_RDY,
1033                     RC_STATUS_MASK, __LINE__);
1034         complete(&lp->tx_complete);
1035         adf7242_clear_irqstat(lp);
1036     }
1037 
1038     return IRQ_HANDLED;
1039 }
1040 
1041 static int adf7242_soft_reset(struct adf7242_local *lp, int line)
1042 {
1043     dev_warn(&lp->spi->dev, "%s (line %d)\n", __func__, line);
1044 
1045     if (test_bit(FLAG_START, &lp->flags))
1046         disable_irq_nosync(lp->spi->irq);
1047 
1048     adf7242_cmd(lp, CMD_RC_PC_RESET_NO_WAIT);
1049     usleep_range(200, 250);
1050     adf7242_write_reg(lp, REG_PKT_CFG, ADDON_EN | BIT(2));
1051     adf7242_cmd(lp, CMD_RC_PHY_RDY);
1052     adf7242_set_promiscuous_mode(lp->hw, lp->promiscuous);
1053     adf7242_set_csma_params(lp->hw, lp->min_be, lp->max_be,
1054                 lp->max_cca_retries);
1055     adf7242_clear_irqstat(lp);
1056 
1057     if (test_bit(FLAG_START, &lp->flags)) {
1058         enable_irq(lp->spi->irq);
1059         return adf7242_cmd(lp, CMD_RC_RX);
1060     }
1061 
1062     return 0;
1063 }
1064 
1065 static int adf7242_hw_init(struct adf7242_local *lp)
1066 {
1067     int ret;
1068     const struct firmware *fw;
1069 
1070     adf7242_cmd(lp, CMD_RC_RESET);
1071     adf7242_cmd(lp, CMD_RC_IDLE);
1072 
1073     /* get ADF7242 addon firmware
1074      * build this driver as module
1075      * and place under /lib/firmware/adf7242_firmware.bin
1076      * or compile firmware into the kernel.
1077      */
1078     ret = request_firmware(&fw, FIRMWARE, &lp->spi->dev);
1079     if (ret) {
1080         dev_err(&lp->spi->dev,
1081             "request_firmware() failed with %d\n", ret);
1082         return ret;
1083     }
1084 
1085     ret = adf7242_upload_firmware(lp, (u8 *)fw->data, fw->size);
1086     if (ret) {
1087         dev_err(&lp->spi->dev,
1088             "upload firmware failed with %d\n", ret);
1089         release_firmware(fw);
1090         return ret;
1091     }
1092 
1093     ret = adf7242_verify_firmware(lp, (u8 *)fw->data, fw->size);
1094     if (ret) {
1095         dev_err(&lp->spi->dev,
1096             "verify firmware failed with %d\n", ret);
1097         release_firmware(fw);
1098         return ret;
1099     }
1100 
1101     adf7242_cmd(lp, CMD_RC_PC_RESET);
1102 
1103     release_firmware(fw);
1104 
1105     adf7242_write_reg(lp, REG_FFILT_CFG,
1106               ACCEPT_BEACON_FRAMES |
1107               ACCEPT_DATA_FRAMES |
1108               ACCEPT_MACCMD_FRAMES |
1109               ACCEPT_RESERVED_FRAMES);
1110 
1111     adf7242_write_reg(lp, REG_AUTO_CFG, RX_AUTO_ACK_EN);
1112 
1113     adf7242_write_reg(lp, REG_PKT_CFG, ADDON_EN | BIT(2));
1114 
1115     adf7242_write_reg(lp, REG_EXTPA_MSC, 0xF1);
1116     adf7242_write_reg(lp, REG_RXFE_CFG, 0x1D);
1117 
1118     adf7242_write_reg(lp, REG_IRQ1_EN0, 0);
1119     adf7242_write_reg(lp, REG_IRQ1_EN1, IRQ_RX_PKT_RCVD | IRQ_CSMA_CA);
1120 
1121     adf7242_clear_irqstat(lp);
1122     adf7242_write_reg(lp, REG_IRQ1_SRC0, 0xFF);
1123 
1124     adf7242_cmd(lp, CMD_RC_IDLE);
1125 
1126     return 0;
1127 }
1128 
1129 static int adf7242_stats_show(struct seq_file *file, void *offset)
1130 {
1131     struct adf7242_local *lp = spi_get_drvdata(file->private);
1132     u8 stat, irq1;
1133 
1134     adf7242_status(lp, &stat);
1135     adf7242_read_reg(lp, REG_IRQ1_SRC1, &irq1);
1136 
1137     seq_printf(file, "IRQ1 = %X:\n%s%s%s%s%s%s%s%s\n", irq1,
1138            irq1 & IRQ_CCA_COMPLETE ? "IRQ_CCA_COMPLETE\n" : "",
1139            irq1 & IRQ_SFD_RX ? "IRQ_SFD_RX\n" : "",
1140            irq1 & IRQ_SFD_TX ? "IRQ_SFD_TX\n" : "",
1141            irq1 & IRQ_RX_PKT_RCVD ? "IRQ_RX_PKT_RCVD\n" : "",
1142            irq1 & IRQ_TX_PKT_SENT ? "IRQ_TX_PKT_SENT\n" : "",
1143            irq1 & IRQ_CSMA_CA ? "IRQ_CSMA_CA\n" : "",
1144            irq1 & IRQ_FRAME_VALID ? "IRQ_FRAME_VALID\n" : "",
1145            irq1 & IRQ_ADDRESS_VALID ? "IRQ_ADDRESS_VALID\n" : "");
1146 
1147     seq_printf(file, "STATUS = %X:\n%s\n%s\n%s\n%s\n%s%s%s%s%s\n", stat,
1148            stat & STAT_SPI_READY ? "SPI_READY" : "SPI_BUSY",
1149            stat & STAT_IRQ_STATUS ? "IRQ_PENDING" : "IRQ_CLEAR",
1150            stat & STAT_RC_READY ? "RC_READY" : "RC_BUSY",
1151            stat & STAT_CCA_RESULT ? "CHAN_IDLE" : "CHAN_BUSY",
1152            (stat & 0xf) == RC_STATUS_IDLE ? "RC_STATUS_IDLE" : "",
1153            (stat & 0xf) == RC_STATUS_MEAS ? "RC_STATUS_MEAS" : "",
1154            (stat & 0xf) == RC_STATUS_PHY_RDY ? "RC_STATUS_PHY_RDY" : "",
1155            (stat & 0xf) == RC_STATUS_RX ? "RC_STATUS_RX" : "",
1156            (stat & 0xf) == RC_STATUS_TX ? "RC_STATUS_TX" : "");
1157 
1158     seq_printf(file, "RSSI = %d\n", lp->rssi);
1159 
1160     return 0;
1161 }
1162 
1163 static void adf7242_debugfs_init(struct adf7242_local *lp)
1164 {
1165     char debugfs_dir_name[DNAME_INLINE_LEN + 1] = "adf7242-";
1166 
1167     strncat(debugfs_dir_name, dev_name(&lp->spi->dev), DNAME_INLINE_LEN);
1168 
1169     lp->debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
1170 
1171     debugfs_create_devm_seqfile(&lp->spi->dev, "status", lp->debugfs_root,
1172                     adf7242_stats_show);
1173 }
1174 
1175 static const s32 adf7242_powers[] = {
1176     500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1177     -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1178     -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1179 };
1180 
1181 static const s32 adf7242_ed_levels[] = {
1182     -9000, -8900, -8800, -8700, -8600, -8500, -8400, -8300, -8200, -8100,
1183     -8000, -7900, -7800, -7700, -7600, -7500, -7400, -7300, -7200, -7100,
1184     -7000, -6900, -6800, -6700, -6600, -6500, -6400, -6300, -6200, -6100,
1185     -6000, -5900, -5800, -5700, -5600, -5500, -5400, -5300, -5200, -5100,
1186     -5000, -4900, -4800, -4700, -4600, -4500, -4400, -4300, -4200, -4100,
1187     -4000, -3900, -3800, -3700, -3600, -3500, -3400, -3200, -3100, -3000
1188 };
1189 
1190 static int adf7242_probe(struct spi_device *spi)
1191 {
1192     struct ieee802154_hw *hw;
1193     struct adf7242_local *lp;
1194     int ret, irq_type;
1195 
1196     if (!spi->irq) {
1197         dev_err(&spi->dev, "no IRQ specified\n");
1198         return -EINVAL;
1199     }
1200 
1201     hw = ieee802154_alloc_hw(sizeof(*lp), &adf7242_ops);
1202     if (!hw)
1203         return -ENOMEM;
1204 
1205     lp = hw->priv;
1206     lp->hw = hw;
1207     lp->spi = spi;
1208 
1209     hw->priv = lp;
1210     hw->parent = &spi->dev;
1211     hw->extra_tx_headroom = 0;
1212 
1213     /* We support only 2.4 Ghz */
1214     hw->phy->supported.channels[0] = 0x7FFF800;
1215 
1216     hw->flags = IEEE802154_HW_OMIT_CKSUM |
1217             IEEE802154_HW_CSMA_PARAMS |
1218             IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1219             IEEE802154_HW_PROMISCUOUS;
1220 
1221     hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1222              WPAN_PHY_FLAG_CCA_ED_LEVEL |
1223              WPAN_PHY_FLAG_CCA_MODE;
1224 
1225     hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY);
1226 
1227     hw->phy->supported.cca_ed_levels = adf7242_ed_levels;
1228     hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(adf7242_ed_levels);
1229 
1230     hw->phy->cca.mode = NL802154_CCA_ENERGY;
1231 
1232     hw->phy->supported.tx_powers = adf7242_powers;
1233     hw->phy->supported.tx_powers_size = ARRAY_SIZE(adf7242_powers);
1234 
1235     hw->phy->supported.min_minbe = 0;
1236     hw->phy->supported.max_minbe = 8;
1237 
1238     hw->phy->supported.min_maxbe = 3;
1239     hw->phy->supported.max_maxbe = 8;
1240 
1241     hw->phy->supported.min_frame_retries = 0;
1242     hw->phy->supported.max_frame_retries = 15;
1243 
1244     hw->phy->supported.min_csma_backoffs = 0;
1245     hw->phy->supported.max_csma_backoffs = 5;
1246 
1247     ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1248 
1249     mutex_init(&lp->bmux);
1250     init_completion(&lp->tx_complete);
1251 
1252     /* Setup Status Message */
1253     lp->stat_xfer.len = 1;
1254     lp->stat_xfer.tx_buf = &lp->buf_stat_tx;
1255     lp->stat_xfer.rx_buf = &lp->buf_stat_rx;
1256     lp->buf_stat_tx = CMD_SPI_NOP;
1257 
1258     spi_message_init(&lp->stat_msg);
1259     spi_message_add_tail(&lp->stat_xfer, &lp->stat_msg);
1260 
1261     spi_set_drvdata(spi, lp);
1262     INIT_DELAYED_WORK(&lp->work, adf7242_rx_cal_work);
1263     lp->wqueue = alloc_ordered_workqueue(dev_name(&spi->dev),
1264                          WQ_MEM_RECLAIM);
1265     if (unlikely(!lp->wqueue)) {
1266         ret = -ENOMEM;
1267         goto err_alloc_wq;
1268     }
1269 
1270     ret = adf7242_hw_init(lp);
1271     if (ret)
1272         goto err_hw_init;
1273 
1274     irq_type = irq_get_trigger_type(spi->irq);
1275     if (!irq_type)
1276         irq_type = IRQF_TRIGGER_HIGH;
1277 
1278     ret = devm_request_threaded_irq(&spi->dev, spi->irq, NULL, adf7242_isr,
1279                     irq_type | IRQF_ONESHOT,
1280                     dev_name(&spi->dev), lp);
1281     if (ret)
1282         goto err_hw_init;
1283 
1284     disable_irq(spi->irq);
1285 
1286     ret = ieee802154_register_hw(lp->hw);
1287     if (ret)
1288         goto err_hw_init;
1289 
1290     dev_set_drvdata(&spi->dev, lp);
1291 
1292     adf7242_debugfs_init(lp);
1293 
1294     dev_info(&spi->dev, "mac802154 IRQ-%d registered\n", spi->irq);
1295 
1296     return ret;
1297 
1298 err_hw_init:
1299     destroy_workqueue(lp->wqueue);
1300 err_alloc_wq:
1301     mutex_destroy(&lp->bmux);
1302     ieee802154_free_hw(lp->hw);
1303 
1304     return ret;
1305 }
1306 
1307 static void adf7242_remove(struct spi_device *spi)
1308 {
1309     struct adf7242_local *lp = spi_get_drvdata(spi);
1310 
1311     debugfs_remove_recursive(lp->debugfs_root);
1312 
1313     ieee802154_unregister_hw(lp->hw);
1314 
1315     cancel_delayed_work_sync(&lp->work);
1316     destroy_workqueue(lp->wqueue);
1317 
1318     mutex_destroy(&lp->bmux);
1319     ieee802154_free_hw(lp->hw);
1320 }
1321 
1322 static const struct of_device_id adf7242_of_match[] = {
1323     { .compatible = "adi,adf7242", },
1324     { .compatible = "adi,adf7241", },
1325     { },
1326 };
1327 MODULE_DEVICE_TABLE(of, adf7242_of_match);
1328 
1329 static const struct spi_device_id adf7242_device_id[] = {
1330     { .name = "adf7242", },
1331     { .name = "adf7241", },
1332     { },
1333 };
1334 MODULE_DEVICE_TABLE(spi, adf7242_device_id);
1335 
1336 static struct spi_driver adf7242_driver = {
1337     .id_table = adf7242_device_id,
1338     .driver = {
1339            .of_match_table = of_match_ptr(adf7242_of_match),
1340            .name = "adf7242",
1341            .owner = THIS_MODULE,
1342            },
1343     .probe = adf7242_probe,
1344     .remove = adf7242_remove,
1345 };
1346 
1347 module_spi_driver(adf7242_driver);
1348 
1349 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
1350 MODULE_DESCRIPTION("ADF7242 IEEE802.15.4 Transceiver Driver");
1351 MODULE_LICENSE("GPL");