alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AT86RF230/RF231 driver |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Siemens AG |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 15 | * Written by: |
| 16 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 17 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
Alexander Aring | 01ebd60 | 2014-07-03 00:20:55 +0200 | [diff] [blame] | 18 | * Alexander Aring <aar@pengutronix.de> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 22 | #include <linux/hrtimer.h> |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 25 | #include <linux/irq.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 26 | #include <linux/gpio.h> |
| 27 | #include <linux/delay.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 28 | #include <linux/spi/spi.h> |
| 29 | #include <linux/spi/at86rf230.h> |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 30 | #include <linux/regmap.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 31 | #include <linux/skbuff.h> |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 32 | #include <linux/of_gpio.h> |
Alexander Aring | 4ca24ac | 2014-10-25 09:41:04 +0200 | [diff] [blame] | 33 | #include <linux/ieee802154.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 34 | |
| 35 | #include <net/mac802154.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 36 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 37 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 38 | struct at86rf230_local; |
| 39 | /* at86rf2xx chip depend data. |
| 40 | * All timings are in us. |
| 41 | */ |
| 42 | struct at86rf2xx_chip_data { |
Alexander Aring | 7a4ef91 | 2014-07-03 00:20:54 +0200 | [diff] [blame] | 43 | u16 t_sleep_cycle; |
Alexander Aring | 984e0c6 | 2014-07-03 00:20:53 +0200 | [diff] [blame] | 44 | u16 t_channel_switch; |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 45 | u16 t_reset_to_off; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 46 | u16 t_off_to_aack; |
| 47 | u16 t_off_to_tx_on; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 48 | u16 t_frame; |
| 49 | u16 t_p_ack; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 50 | int rssi_base_val; |
| 51 | |
Alexander Aring | e37d2ec | 2014-10-28 18:21:19 +0100 | [diff] [blame] | 52 | int (*set_channel)(struct at86rf230_local *, u8, u8); |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 53 | int (*get_desense_steps)(struct at86rf230_local *, s32); |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 54 | int (*set_txpower)(struct at86rf230_local *, s32); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 57 | #define AT86RF2XX_MAX_BUF (127 + 3) |
| 58 | /* tx retries to access the TX_ON state |
| 59 | * if it's above then force change will be started. |
| 60 | * |
| 61 | * We assume the max_frame_retries (7) value of 802.15.4 here. |
| 62 | */ |
| 63 | #define AT86RF2XX_MAX_TX_RETRIES 7 |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 64 | /* We use the recommended 5 minutes timeout to recalibrate */ |
| 65 | #define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 66 | |
| 67 | struct at86rf230_state_change { |
| 68 | struct at86rf230_local *lp; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 69 | int irq; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 70 | |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 71 | struct hrtimer timer; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 72 | struct spi_message msg; |
| 73 | struct spi_transfer trx; |
| 74 | u8 buf[AT86RF2XX_MAX_BUF]; |
| 75 | |
| 76 | void (*complete)(void *context); |
| 77 | u8 from_state; |
| 78 | u8 to_state; |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 79 | |
| 80 | bool irq_enable; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 83 | struct at86rf230_local { |
| 84 | struct spi_device *spi; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 85 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 86 | struct ieee802154_hw *hw; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 87 | struct at86rf2xx_chip_data *data; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 88 | struct regmap *regmap; |
Alexander Aring | d2c8bf5 | 2015-04-30 17:45:03 +0200 | [diff] [blame] | 89 | int slp_tr; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 90 | |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 91 | struct completion state_complete; |
| 92 | struct at86rf230_state_change state; |
| 93 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 94 | struct at86rf230_state_change irq; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 95 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 96 | bool tx_aret; |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 97 | unsigned long cal_timeout; |
Alexander Aring | 850f43a | 2014-10-07 10:38:27 +0200 | [diff] [blame] | 98 | s8 max_frame_retries; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 99 | bool is_tx; |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 100 | bool is_tx_from_off; |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 101 | u8 tx_retry; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 102 | struct sk_buff *tx_skb; |
| 103 | struct at86rf230_state_change tx; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Alexander Aring | 5e8e01e | 2015-04-30 17:44:58 +0200 | [diff] [blame] | 106 | #define RG_TRX_STATUS (0x01) |
| 107 | #define SR_TRX_STATUS 0x01, 0x1f, 0 |
| 108 | #define SR_RESERVED_01_3 0x01, 0x20, 5 |
| 109 | #define SR_CCA_STATUS 0x01, 0x40, 6 |
| 110 | #define SR_CCA_DONE 0x01, 0x80, 7 |
| 111 | #define RG_TRX_STATE (0x02) |
| 112 | #define SR_TRX_CMD 0x02, 0x1f, 0 |
| 113 | #define SR_TRAC_STATUS 0x02, 0xe0, 5 |
| 114 | #define RG_TRX_CTRL_0 (0x03) |
| 115 | #define SR_CLKM_CTRL 0x03, 0x07, 0 |
| 116 | #define SR_CLKM_SHA_SEL 0x03, 0x08, 3 |
| 117 | #define SR_PAD_IO_CLKM 0x03, 0x30, 4 |
| 118 | #define SR_PAD_IO 0x03, 0xc0, 6 |
| 119 | #define RG_TRX_CTRL_1 (0x04) |
| 120 | #define SR_IRQ_POLARITY 0x04, 0x01, 0 |
| 121 | #define SR_IRQ_MASK_MODE 0x04, 0x02, 1 |
| 122 | #define SR_SPI_CMD_MODE 0x04, 0x0c, 2 |
| 123 | #define SR_RX_BL_CTRL 0x04, 0x10, 4 |
| 124 | #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5 |
| 125 | #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6 |
| 126 | #define SR_PA_EXT_EN 0x04, 0x80, 7 |
| 127 | #define RG_PHY_TX_PWR (0x05) |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 128 | #define SR_TX_PWR_23X 0x05, 0x0f, 0 |
| 129 | #define SR_PA_LT_230 0x05, 0x30, 4 |
| 130 | #define SR_PA_BUF_LT_230 0x05, 0xc0, 6 |
| 131 | #define SR_TX_PWR_212 0x05, 0x1f, 0 |
| 132 | #define SR_GC_PA_212 0x05, 0x60, 5 |
| 133 | #define SR_PA_BOOST_LT_212 0x05, 0x80, 7 |
Alexander Aring | 5e8e01e | 2015-04-30 17:44:58 +0200 | [diff] [blame] | 134 | #define RG_PHY_RSSI (0x06) |
| 135 | #define SR_RSSI 0x06, 0x1f, 0 |
| 136 | #define SR_RND_VALUE 0x06, 0x60, 5 |
| 137 | #define SR_RX_CRC_VALID 0x06, 0x80, 7 |
| 138 | #define RG_PHY_ED_LEVEL (0x07) |
| 139 | #define SR_ED_LEVEL 0x07, 0xff, 0 |
| 140 | #define RG_PHY_CC_CCA (0x08) |
| 141 | #define SR_CHANNEL 0x08, 0x1f, 0 |
| 142 | #define SR_CCA_MODE 0x08, 0x60, 5 |
| 143 | #define SR_CCA_REQUEST 0x08, 0x80, 7 |
| 144 | #define RG_CCA_THRES (0x09) |
| 145 | #define SR_CCA_ED_THRES 0x09, 0x0f, 0 |
| 146 | #define SR_RESERVED_09_1 0x09, 0xf0, 4 |
| 147 | #define RG_RX_CTRL (0x0a) |
| 148 | #define SR_PDT_THRES 0x0a, 0x0f, 0 |
| 149 | #define SR_RESERVED_0a_1 0x0a, 0xf0, 4 |
| 150 | #define RG_SFD_VALUE (0x0b) |
| 151 | #define SR_SFD_VALUE 0x0b, 0xff, 0 |
| 152 | #define RG_TRX_CTRL_2 (0x0c) |
| 153 | #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0 |
| 154 | #define SR_SUB_MODE 0x0c, 0x04, 2 |
| 155 | #define SR_BPSK_QPSK 0x0c, 0x08, 3 |
| 156 | #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4 |
| 157 | #define SR_RESERVED_0c_5 0x0c, 0x60, 5 |
| 158 | #define SR_RX_SAFE_MODE 0x0c, 0x80, 7 |
| 159 | #define RG_ANT_DIV (0x0d) |
| 160 | #define SR_ANT_CTRL 0x0d, 0x03, 0 |
| 161 | #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2 |
| 162 | #define SR_ANT_DIV_EN 0x0d, 0x08, 3 |
| 163 | #define SR_RESERVED_0d_2 0x0d, 0x70, 4 |
| 164 | #define SR_ANT_SEL 0x0d, 0x80, 7 |
| 165 | #define RG_IRQ_MASK (0x0e) |
| 166 | #define SR_IRQ_MASK 0x0e, 0xff, 0 |
| 167 | #define RG_IRQ_STATUS (0x0f) |
| 168 | #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0 |
| 169 | #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1 |
| 170 | #define SR_IRQ_2_RX_START 0x0f, 0x04, 2 |
| 171 | #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3 |
| 172 | #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4 |
| 173 | #define SR_IRQ_5_AMI 0x0f, 0x20, 5 |
| 174 | #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6 |
| 175 | #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7 |
| 176 | #define RG_VREG_CTRL (0x10) |
| 177 | #define SR_RESERVED_10_6 0x10, 0x03, 0 |
| 178 | #define SR_DVDD_OK 0x10, 0x04, 2 |
| 179 | #define SR_DVREG_EXT 0x10, 0x08, 3 |
| 180 | #define SR_RESERVED_10_3 0x10, 0x30, 4 |
| 181 | #define SR_AVDD_OK 0x10, 0x40, 6 |
| 182 | #define SR_AVREG_EXT 0x10, 0x80, 7 |
| 183 | #define RG_BATMON (0x11) |
| 184 | #define SR_BATMON_VTH 0x11, 0x0f, 0 |
| 185 | #define SR_BATMON_HR 0x11, 0x10, 4 |
| 186 | #define SR_BATMON_OK 0x11, 0x20, 5 |
| 187 | #define SR_RESERVED_11_1 0x11, 0xc0, 6 |
| 188 | #define RG_XOSC_CTRL (0x12) |
| 189 | #define SR_XTAL_TRIM 0x12, 0x0f, 0 |
| 190 | #define SR_XTAL_MODE 0x12, 0xf0, 4 |
| 191 | #define RG_RX_SYN (0x15) |
| 192 | #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0 |
| 193 | #define SR_RESERVED_15_2 0x15, 0x70, 4 |
| 194 | #define SR_RX_PDT_DIS 0x15, 0x80, 7 |
| 195 | #define RG_XAH_CTRL_1 (0x17) |
| 196 | #define SR_RESERVED_17_8 0x17, 0x01, 0 |
| 197 | #define SR_AACK_PROM_MODE 0x17, 0x02, 1 |
| 198 | #define SR_AACK_ACK_TIME 0x17, 0x04, 2 |
| 199 | #define SR_RESERVED_17_5 0x17, 0x08, 3 |
| 200 | #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4 |
| 201 | #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5 |
| 202 | #define SR_CSMA_LBT_MODE 0x17, 0x40, 6 |
| 203 | #define SR_RESERVED_17_1 0x17, 0x80, 7 |
| 204 | #define RG_FTN_CTRL (0x18) |
| 205 | #define SR_RESERVED_18_2 0x18, 0x7f, 0 |
| 206 | #define SR_FTN_START 0x18, 0x80, 7 |
| 207 | #define RG_PLL_CF (0x1a) |
| 208 | #define SR_RESERVED_1a_2 0x1a, 0x7f, 0 |
| 209 | #define SR_PLL_CF_START 0x1a, 0x80, 7 |
| 210 | #define RG_PLL_DCU (0x1b) |
| 211 | #define SR_RESERVED_1b_3 0x1b, 0x3f, 0 |
| 212 | #define SR_RESERVED_1b_2 0x1b, 0x40, 6 |
| 213 | #define SR_PLL_DCU_START 0x1b, 0x80, 7 |
| 214 | #define RG_PART_NUM (0x1c) |
| 215 | #define SR_PART_NUM 0x1c, 0xff, 0 |
| 216 | #define RG_VERSION_NUM (0x1d) |
| 217 | #define SR_VERSION_NUM 0x1d, 0xff, 0 |
| 218 | #define RG_MAN_ID_0 (0x1e) |
| 219 | #define SR_MAN_ID_0 0x1e, 0xff, 0 |
| 220 | #define RG_MAN_ID_1 (0x1f) |
| 221 | #define SR_MAN_ID_1 0x1f, 0xff, 0 |
| 222 | #define RG_SHORT_ADDR_0 (0x20) |
| 223 | #define SR_SHORT_ADDR_0 0x20, 0xff, 0 |
| 224 | #define RG_SHORT_ADDR_1 (0x21) |
| 225 | #define SR_SHORT_ADDR_1 0x21, 0xff, 0 |
| 226 | #define RG_PAN_ID_0 (0x22) |
| 227 | #define SR_PAN_ID_0 0x22, 0xff, 0 |
| 228 | #define RG_PAN_ID_1 (0x23) |
| 229 | #define SR_PAN_ID_1 0x23, 0xff, 0 |
| 230 | #define RG_IEEE_ADDR_0 (0x24) |
| 231 | #define SR_IEEE_ADDR_0 0x24, 0xff, 0 |
| 232 | #define RG_IEEE_ADDR_1 (0x25) |
| 233 | #define SR_IEEE_ADDR_1 0x25, 0xff, 0 |
| 234 | #define RG_IEEE_ADDR_2 (0x26) |
| 235 | #define SR_IEEE_ADDR_2 0x26, 0xff, 0 |
| 236 | #define RG_IEEE_ADDR_3 (0x27) |
| 237 | #define SR_IEEE_ADDR_3 0x27, 0xff, 0 |
| 238 | #define RG_IEEE_ADDR_4 (0x28) |
| 239 | #define SR_IEEE_ADDR_4 0x28, 0xff, 0 |
| 240 | #define RG_IEEE_ADDR_5 (0x29) |
| 241 | #define SR_IEEE_ADDR_5 0x29, 0xff, 0 |
| 242 | #define RG_IEEE_ADDR_6 (0x2a) |
| 243 | #define SR_IEEE_ADDR_6 0x2a, 0xff, 0 |
| 244 | #define RG_IEEE_ADDR_7 (0x2b) |
| 245 | #define SR_IEEE_ADDR_7 0x2b, 0xff, 0 |
| 246 | #define RG_XAH_CTRL_0 (0x2c) |
| 247 | #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0 |
| 248 | #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1 |
| 249 | #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4 |
| 250 | #define RG_CSMA_SEED_0 (0x2d) |
| 251 | #define SR_CSMA_SEED_0 0x2d, 0xff, 0 |
| 252 | #define RG_CSMA_SEED_1 (0x2e) |
| 253 | #define SR_CSMA_SEED_1 0x2e, 0x07, 0 |
| 254 | #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3 |
| 255 | #define SR_AACK_DIS_ACK 0x2e, 0x10, 4 |
| 256 | #define SR_AACK_SET_PD 0x2e, 0x20, 5 |
| 257 | #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6 |
| 258 | #define RG_CSMA_BE (0x2f) |
| 259 | #define SR_MIN_BE 0x2f, 0x0f, 0 |
| 260 | #define SR_MAX_BE 0x2f, 0xf0, 4 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 261 | |
| 262 | #define CMD_REG 0x80 |
| 263 | #define CMD_REG_MASK 0x3f |
| 264 | #define CMD_WRITE 0x40 |
| 265 | #define CMD_FB 0x20 |
| 266 | |
| 267 | #define IRQ_BAT_LOW (1 << 7) |
| 268 | #define IRQ_TRX_UR (1 << 6) |
| 269 | #define IRQ_AMI (1 << 5) |
| 270 | #define IRQ_CCA_ED (1 << 4) |
| 271 | #define IRQ_TRX_END (1 << 3) |
| 272 | #define IRQ_RX_START (1 << 2) |
| 273 | #define IRQ_PLL_UNL (1 << 1) |
| 274 | #define IRQ_PLL_LOCK (1 << 0) |
| 275 | |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 276 | #define IRQ_ACTIVE_HIGH 0 |
| 277 | #define IRQ_ACTIVE_LOW 1 |
| 278 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 279 | #define STATE_P_ON 0x00 /* BUSY */ |
| 280 | #define STATE_BUSY_RX 0x01 |
| 281 | #define STATE_BUSY_TX 0x02 |
| 282 | #define STATE_FORCE_TRX_OFF 0x03 |
| 283 | #define STATE_FORCE_TX_ON 0x04 /* IDLE */ |
| 284 | /* 0x05 */ /* INVALID_PARAMETER */ |
| 285 | #define STATE_RX_ON 0x06 |
| 286 | /* 0x07 */ /* SUCCESS */ |
| 287 | #define STATE_TRX_OFF 0x08 |
| 288 | #define STATE_TX_ON 0x09 |
| 289 | /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */ |
| 290 | #define STATE_SLEEP 0x0F |
Thomas Stilwell | 48d5dba | 2014-03-10 19:29:25 -0500 | [diff] [blame] | 291 | #define STATE_PREP_DEEP_SLEEP 0x10 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 292 | #define STATE_BUSY_RX_AACK 0x11 |
| 293 | #define STATE_BUSY_TX_ARET 0x12 |
stefan@datenfreihafen.org | 028889b | 2013-03-26 12:41:31 +0000 | [diff] [blame] | 294 | #define STATE_RX_AACK_ON 0x16 |
| 295 | #define STATE_TX_ARET_ON 0x19 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 296 | #define STATE_RX_ON_NOCLK 0x1C |
| 297 | #define STATE_RX_AACK_ON_NOCLK 0x1D |
| 298 | #define STATE_BUSY_RX_AACK_NOCLK 0x1E |
| 299 | #define STATE_TRANSITION_IN_PROGRESS 0x1F |
| 300 | |
Christoffer Holmstedt | 4748e86 | 2015-04-30 17:44:56 +0200 | [diff] [blame] | 301 | #define TRX_STATE_MASK (0x1F) |
| 302 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 303 | #define AT86RF2XX_NUMREGS 0x3F |
| 304 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 305 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 306 | at86rf230_async_state_change(struct at86rf230_local *lp, |
| 307 | struct at86rf230_state_change *ctx, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 308 | const u8 state, void (*complete)(void *context), |
| 309 | const bool irq_enable); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 310 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 311 | static inline int |
| 312 | __at86rf230_write(struct at86rf230_local *lp, |
| 313 | unsigned int addr, unsigned int data) |
| 314 | { |
| 315 | return regmap_write(lp->regmap, addr, data); |
| 316 | } |
| 317 | |
| 318 | static inline int |
| 319 | __at86rf230_read(struct at86rf230_local *lp, |
| 320 | unsigned int addr, unsigned int *data) |
| 321 | { |
| 322 | return regmap_read(lp->regmap, addr, data); |
| 323 | } |
| 324 | |
| 325 | static inline int |
| 326 | at86rf230_read_subreg(struct at86rf230_local *lp, |
| 327 | unsigned int addr, unsigned int mask, |
| 328 | unsigned int shift, unsigned int *data) |
| 329 | { |
| 330 | int rc; |
| 331 | |
| 332 | rc = __at86rf230_read(lp, addr, data); |
Alexander Aring | d907c4f | 2015-03-17 10:32:39 +0100 | [diff] [blame] | 333 | if (!rc) |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 334 | *data = (*data & mask) >> shift; |
| 335 | |
| 336 | return rc; |
| 337 | } |
| 338 | |
| 339 | static inline int |
| 340 | at86rf230_write_subreg(struct at86rf230_local *lp, |
| 341 | unsigned int addr, unsigned int mask, |
| 342 | unsigned int shift, unsigned int data) |
| 343 | { |
| 344 | return regmap_update_bits(lp->regmap, addr, mask, data << shift); |
| 345 | } |
| 346 | |
Alexander Aring | d2c8bf5 | 2015-04-30 17:45:03 +0200 | [diff] [blame] | 347 | static inline void |
| 348 | at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp) |
| 349 | { |
| 350 | gpio_set_value(lp->slp_tr, 1); |
| 351 | udelay(1); |
| 352 | gpio_set_value(lp->slp_tr, 0); |
| 353 | } |
| 354 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 355 | static bool |
| 356 | at86rf230_reg_writeable(struct device *dev, unsigned int reg) |
| 357 | { |
| 358 | switch (reg) { |
| 359 | case RG_TRX_STATE: |
| 360 | case RG_TRX_CTRL_0: |
| 361 | case RG_TRX_CTRL_1: |
| 362 | case RG_PHY_TX_PWR: |
| 363 | case RG_PHY_ED_LEVEL: |
| 364 | case RG_PHY_CC_CCA: |
| 365 | case RG_CCA_THRES: |
| 366 | case RG_RX_CTRL: |
| 367 | case RG_SFD_VALUE: |
| 368 | case RG_TRX_CTRL_2: |
| 369 | case RG_ANT_DIV: |
| 370 | case RG_IRQ_MASK: |
| 371 | case RG_VREG_CTRL: |
| 372 | case RG_BATMON: |
| 373 | case RG_XOSC_CTRL: |
| 374 | case RG_RX_SYN: |
| 375 | case RG_XAH_CTRL_1: |
| 376 | case RG_FTN_CTRL: |
| 377 | case RG_PLL_CF: |
| 378 | case RG_PLL_DCU: |
| 379 | case RG_SHORT_ADDR_0: |
| 380 | case RG_SHORT_ADDR_1: |
| 381 | case RG_PAN_ID_0: |
| 382 | case RG_PAN_ID_1: |
| 383 | case RG_IEEE_ADDR_0: |
| 384 | case RG_IEEE_ADDR_1: |
| 385 | case RG_IEEE_ADDR_2: |
| 386 | case RG_IEEE_ADDR_3: |
| 387 | case RG_IEEE_ADDR_4: |
| 388 | case RG_IEEE_ADDR_5: |
| 389 | case RG_IEEE_ADDR_6: |
| 390 | case RG_IEEE_ADDR_7: |
| 391 | case RG_XAH_CTRL_0: |
| 392 | case RG_CSMA_SEED_0: |
| 393 | case RG_CSMA_SEED_1: |
| 394 | case RG_CSMA_BE: |
| 395 | return true; |
| 396 | default: |
| 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static bool |
| 402 | at86rf230_reg_readable(struct device *dev, unsigned int reg) |
| 403 | { |
| 404 | bool rc; |
| 405 | |
| 406 | /* all writeable are also readable */ |
| 407 | rc = at86rf230_reg_writeable(dev, reg); |
| 408 | if (rc) |
| 409 | return rc; |
| 410 | |
| 411 | /* readonly regs */ |
| 412 | switch (reg) { |
| 413 | case RG_TRX_STATUS: |
| 414 | case RG_PHY_RSSI: |
| 415 | case RG_IRQ_STATUS: |
| 416 | case RG_PART_NUM: |
| 417 | case RG_VERSION_NUM: |
| 418 | case RG_MAN_ID_1: |
| 419 | case RG_MAN_ID_0: |
| 420 | return true; |
| 421 | default: |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static bool |
| 427 | at86rf230_reg_volatile(struct device *dev, unsigned int reg) |
| 428 | { |
| 429 | /* can be changed during runtime */ |
| 430 | switch (reg) { |
| 431 | case RG_TRX_STATUS: |
| 432 | case RG_TRX_STATE: |
| 433 | case RG_PHY_RSSI: |
| 434 | case RG_PHY_ED_LEVEL: |
| 435 | case RG_IRQ_STATUS: |
| 436 | case RG_VREG_CTRL: |
Alexander Aring | 51b3b2c | 2015-03-09 13:56:12 +0100 | [diff] [blame] | 437 | case RG_PLL_CF: |
| 438 | case RG_PLL_DCU: |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 439 | return true; |
| 440 | default: |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static bool |
| 446 | at86rf230_reg_precious(struct device *dev, unsigned int reg) |
| 447 | { |
| 448 | /* don't clear irq line on read */ |
| 449 | switch (reg) { |
| 450 | case RG_IRQ_STATUS: |
| 451 | return true; |
| 452 | default: |
| 453 | return false; |
| 454 | } |
| 455 | } |
| 456 | |
Krzysztof Kozlowski | 889ee2c | 2015-01-05 10:02:31 +0100 | [diff] [blame] | 457 | static const struct regmap_config at86rf230_regmap_spi_config = { |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 458 | .reg_bits = 8, |
| 459 | .val_bits = 8, |
| 460 | .write_flag_mask = CMD_REG | CMD_WRITE, |
| 461 | .read_flag_mask = CMD_REG, |
| 462 | .cache_type = REGCACHE_RBTREE, |
| 463 | .max_register = AT86RF2XX_NUMREGS, |
| 464 | .writeable_reg = at86rf230_reg_writeable, |
| 465 | .readable_reg = at86rf230_reg_readable, |
| 466 | .volatile_reg = at86rf230_reg_volatile, |
| 467 | .precious_reg = at86rf230_reg_precious, |
| 468 | }; |
| 469 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 470 | static void |
| 471 | at86rf230_async_error_recover(void *context) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 472 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 473 | struct at86rf230_state_change *ctx = context; |
| 474 | struct at86rf230_local *lp = ctx->lp; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 475 | |
Alexander Aring | a7a484b | 2015-03-26 12:46:30 +0100 | [diff] [blame] | 476 | lp->is_tx = 0; |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 477 | at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false); |
Alexander Aring | 955aee8 | 2014-10-26 09:37:15 +0100 | [diff] [blame] | 478 | ieee802154_wake_queue(lp->hw); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 479 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 480 | |
Alexander Aring | fc50c6e | 2014-12-15 10:25:54 +0100 | [diff] [blame] | 481 | static inline void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 482 | at86rf230_async_error(struct at86rf230_local *lp, |
| 483 | struct at86rf230_state_change *ctx, int rc) |
| 484 | { |
| 485 | dev_err(&lp->spi->dev, "spi_async error %d\n", rc); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 486 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 487 | at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 488 | at86rf230_async_error_recover, false); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 489 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 490 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 491 | /* Generic function to get some register value in async mode */ |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 492 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 493 | at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg, |
| 494 | struct at86rf230_state_change *ctx, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 495 | void (*complete)(void *context), |
| 496 | const bool irq_enable) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 497 | { |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 498 | int rc; |
| 499 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 500 | u8 *tx_buf = ctx->buf; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 501 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 502 | tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 503 | ctx->msg.complete = complete; |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 504 | ctx->irq_enable = irq_enable; |
| 505 | rc = spi_async(lp->spi, &ctx->msg); |
| 506 | if (rc) { |
| 507 | if (irq_enable) |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 508 | enable_irq(ctx->irq); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 509 | |
| 510 | at86rf230_async_error(lp, ctx, rc); |
| 511 | } |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 512 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 513 | |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 514 | static inline u8 at86rf230_state_to_force(u8 state) |
| 515 | { |
| 516 | if (state == STATE_TX_ON) |
| 517 | return STATE_FORCE_TX_ON; |
| 518 | else |
| 519 | return STATE_FORCE_TRX_OFF; |
| 520 | } |
| 521 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 522 | static void |
| 523 | at86rf230_async_state_assert(void *context) |
| 524 | { |
| 525 | struct at86rf230_state_change *ctx = context; |
| 526 | struct at86rf230_local *lp = ctx->lp; |
| 527 | const u8 *buf = ctx->buf; |
Christoffer Holmstedt | 4748e86 | 2015-04-30 17:44:56 +0200 | [diff] [blame] | 528 | const u8 trx_state = buf[1] & TRX_STATE_MASK; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 529 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 530 | /* Assert state change */ |
| 531 | if (trx_state != ctx->to_state) { |
| 532 | /* Special handling if transceiver state is in |
| 533 | * STATE_BUSY_RX_AACK and a SHR was detected. |
| 534 | */ |
| 535 | if (trx_state == STATE_BUSY_RX_AACK) { |
| 536 | /* Undocumented race condition. If we send a state |
| 537 | * change to STATE_RX_AACK_ON the transceiver could |
| 538 | * change his state automatically to STATE_BUSY_RX_AACK |
| 539 | * if a SHR was detected. This is not an error, but we |
| 540 | * can't assert this. |
| 541 | */ |
| 542 | if (ctx->to_state == STATE_RX_AACK_ON) |
| 543 | goto done; |
| 544 | |
| 545 | /* If we change to STATE_TX_ON without forcing and |
| 546 | * transceiver state is STATE_BUSY_RX_AACK, we wait |
| 547 | * 'tFrame + tPAck' receiving time. In this time the |
| 548 | * PDU should be received. If the transceiver is still |
| 549 | * in STATE_BUSY_RX_AACK, we run a force state change |
| 550 | * to STATE_TX_ON. This is a timeout handling, if the |
| 551 | * transceiver stucks in STATE_BUSY_RX_AACK. |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 552 | * |
| 553 | * Additional we do several retries to try to get into |
| 554 | * TX_ON state without forcing. If the retries are |
| 555 | * higher or equal than AT86RF2XX_MAX_TX_RETRIES we |
| 556 | * will do a force change. |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 557 | */ |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 558 | if (ctx->to_state == STATE_TX_ON || |
| 559 | ctx->to_state == STATE_TRX_OFF) { |
| 560 | u8 state = ctx->to_state; |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 561 | |
| 562 | if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES) |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 563 | state = at86rf230_state_to_force(state); |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 564 | lp->tx_retry++; |
| 565 | |
| 566 | at86rf230_async_state_change(lp, ctx, state, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 567 | ctx->complete, |
| 568 | ctx->irq_enable); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 569 | return; |
| 570 | } |
| 571 | } |
| 572 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 573 | dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n", |
| 574 | ctx->from_state, ctx->to_state, trx_state); |
| 575 | } |
| 576 | |
| 577 | done: |
| 578 | if (ctx->complete) |
| 579 | ctx->complete(context); |
| 580 | } |
| 581 | |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 582 | static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer) |
| 583 | { |
| 584 | struct at86rf230_state_change *ctx = |
| 585 | container_of(timer, struct at86rf230_state_change, timer); |
| 586 | struct at86rf230_local *lp = ctx->lp; |
| 587 | |
| 588 | at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 589 | at86rf230_async_state_assert, |
| 590 | ctx->irq_enable); |
| 591 | |
| 592 | return HRTIMER_NORESTART; |
| 593 | } |
| 594 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 595 | /* Do state change timing delay. */ |
| 596 | static void |
| 597 | at86rf230_async_state_delay(void *context) |
| 598 | { |
| 599 | struct at86rf230_state_change *ctx = context; |
| 600 | struct at86rf230_local *lp = ctx->lp; |
| 601 | struct at86rf2xx_chip_data *c = lp->data; |
| 602 | bool force = false; |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 603 | ktime_t tim; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 604 | |
| 605 | /* The force state changes are will show as normal states in the |
| 606 | * state status subregister. We change the to_state to the |
| 607 | * corresponding one and remember if it was a force change, this |
| 608 | * differs if we do a state change from STATE_BUSY_RX_AACK. |
| 609 | */ |
| 610 | switch (ctx->to_state) { |
| 611 | case STATE_FORCE_TX_ON: |
| 612 | ctx->to_state = STATE_TX_ON; |
| 613 | force = true; |
| 614 | break; |
| 615 | case STATE_FORCE_TRX_OFF: |
| 616 | ctx->to_state = STATE_TRX_OFF; |
| 617 | force = true; |
| 618 | break; |
| 619 | default: |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | switch (ctx->from_state) { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 624 | case STATE_TRX_OFF: |
| 625 | switch (ctx->to_state) { |
| 626 | case STATE_RX_AACK_ON: |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 627 | tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC); |
Alexander Aring | 2ad3324 | 2015-04-30 17:44:59 +0200 | [diff] [blame] | 628 | /* state change from TRX_OFF to RX_AACK_ON to do a |
| 629 | * calibration, we need to reset the timeout for the |
| 630 | * next one. |
| 631 | */ |
| 632 | lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 633 | goto change; |
Alexander Aring | 3b951ca | 2015-04-30 17:45:00 +0200 | [diff] [blame] | 634 | case STATE_TX_ARET_ON: |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 635 | case STATE_TX_ON: |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 636 | tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC); |
Alexander Aring | 3b951ca | 2015-04-30 17:45:00 +0200 | [diff] [blame] | 637 | /* state change from TRX_OFF to TX_ON or ARET_ON to do |
| 638 | * a calibration, we need to reset the timeout for the |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 639 | * next one. |
| 640 | */ |
| 641 | lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 642 | goto change; |
| 643 | default: |
| 644 | break; |
| 645 | } |
| 646 | break; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 647 | case STATE_BUSY_RX_AACK: |
| 648 | switch (ctx->to_state) { |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 649 | case STATE_TRX_OFF: |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 650 | case STATE_TX_ON: |
| 651 | /* Wait for worst case receiving time if we |
| 652 | * didn't make a force change from BUSY_RX_AACK |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 653 | * to TX_ON or TRX_OFF. |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 654 | */ |
| 655 | if (!force) { |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 656 | tim = ktime_set(0, (c->t_frame + c->t_p_ack) * |
| 657 | NSEC_PER_USEC); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 658 | goto change; |
| 659 | } |
| 660 | break; |
| 661 | default: |
| 662 | break; |
| 663 | } |
| 664 | break; |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 665 | /* Default value, means RESET state */ |
| 666 | case STATE_P_ON: |
| 667 | switch (ctx->to_state) { |
| 668 | case STATE_TRX_OFF: |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 669 | tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC); |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 670 | goto change; |
| 671 | default: |
| 672 | break; |
| 673 | } |
| 674 | break; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 675 | default: |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | /* Default delay is 1us in the most cases */ |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 680 | tim = ktime_set(0, NSEC_PER_USEC); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 681 | |
| 682 | change: |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 683 | hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static void |
| 687 | at86rf230_async_state_change_start(void *context) |
| 688 | { |
| 689 | struct at86rf230_state_change *ctx = context; |
| 690 | struct at86rf230_local *lp = ctx->lp; |
| 691 | u8 *buf = ctx->buf; |
Christoffer Holmstedt | 4748e86 | 2015-04-30 17:44:56 +0200 | [diff] [blame] | 692 | const u8 trx_state = buf[1] & TRX_STATE_MASK; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 693 | int rc; |
| 694 | |
| 695 | /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */ |
| 696 | if (trx_state == STATE_TRANSITION_IN_PROGRESS) { |
| 697 | udelay(1); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 698 | at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 699 | at86rf230_async_state_change_start, |
| 700 | ctx->irq_enable); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 701 | return; |
| 702 | } |
| 703 | |
| 704 | /* Check if we already are in the state which we change in */ |
| 705 | if (trx_state == ctx->to_state) { |
| 706 | if (ctx->complete) |
| 707 | ctx->complete(context); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | /* Set current state to the context of state change */ |
| 712 | ctx->from_state = trx_state; |
| 713 | |
| 714 | /* Going into the next step for a state change which do a timing |
| 715 | * relevant delay. |
| 716 | */ |
| 717 | buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE; |
| 718 | buf[1] = ctx->to_state; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 719 | ctx->msg.complete = at86rf230_async_state_delay; |
| 720 | rc = spi_async(lp->spi, &ctx->msg); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 721 | if (rc) { |
| 722 | if (ctx->irq_enable) |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 723 | enable_irq(ctx->irq); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 724 | |
Alexander Aring | 4fef7d3 | 2014-12-15 10:25:55 +0100 | [diff] [blame] | 725 | at86rf230_async_error(lp, ctx, rc); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 726 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 729 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 730 | at86rf230_async_state_change(struct at86rf230_local *lp, |
| 731 | struct at86rf230_state_change *ctx, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 732 | const u8 state, void (*complete)(void *context), |
| 733 | const bool irq_enable) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 734 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 735 | /* Initialization for the state change context */ |
| 736 | ctx->to_state = state; |
| 737 | ctx->complete = complete; |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 738 | ctx->irq_enable = irq_enable; |
| 739 | at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 740 | at86rf230_async_state_change_start, |
| 741 | irq_enable); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 742 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 743 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 744 | static void |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 745 | at86rf230_sync_state_change_complete(void *context) |
| 746 | { |
| 747 | struct at86rf230_state_change *ctx = context; |
| 748 | struct at86rf230_local *lp = ctx->lp; |
| 749 | |
| 750 | complete(&lp->state_complete); |
| 751 | } |
| 752 | |
| 753 | /* This function do a sync framework above the async state change. |
| 754 | * Some callbacks of the IEEE 802.15.4 driver interface need to be |
| 755 | * handled synchronously. |
| 756 | */ |
| 757 | static int |
| 758 | at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state) |
| 759 | { |
Nicholas Mc Guire | 3e544ef | 2015-02-14 23:57:48 +0100 | [diff] [blame] | 760 | unsigned long rc; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 761 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 762 | at86rf230_async_state_change(lp, &lp->state, state, |
| 763 | at86rf230_sync_state_change_complete, |
| 764 | false); |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 765 | |
| 766 | rc = wait_for_completion_timeout(&lp->state_complete, |
| 767 | msecs_to_jiffies(100)); |
Alexander Aring | d06c219 | 2014-10-07 10:38:26 +0200 | [diff] [blame] | 768 | if (!rc) { |
| 769 | at86rf230_async_error(lp, &lp->state, -ETIMEDOUT); |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 770 | return -ETIMEDOUT; |
Alexander Aring | d06c219 | 2014-10-07 10:38:26 +0200 | [diff] [blame] | 771 | } |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 777 | at86rf230_tx_complete(void *context) |
| 778 | { |
| 779 | struct at86rf230_state_change *ctx = context; |
| 780 | struct at86rf230_local *lp = ctx->lp; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 781 | |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 782 | enable_irq(ctx->irq); |
Alexander Aring | 955aee8 | 2014-10-26 09:37:15 +0100 | [diff] [blame] | 783 | |
Alexander Aring | ef5428a | 2015-03-01 21:55:29 +0100 | [diff] [blame] | 784 | ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 785 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 786 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 787 | static void |
| 788 | at86rf230_tx_on(void *context) |
| 789 | { |
| 790 | struct at86rf230_state_change *ctx = context; |
| 791 | struct at86rf230_local *lp = ctx->lp; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 792 | |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 793 | at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 794 | at86rf230_tx_complete, true); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 795 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 796 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 797 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 798 | at86rf230_tx_trac_check(void *context) |
| 799 | { |
| 800 | struct at86rf230_state_change *ctx = context; |
| 801 | struct at86rf230_local *lp = ctx->lp; |
| 802 | const u8 *buf = ctx->buf; |
| 803 | const u8 trac = (buf[1] & 0xe0) >> 5; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 804 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 805 | /* If trac status is different than zero we need to do a state change |
Alexander Aring | 2f8cdd9 | 2015-04-30 17:45:01 +0200 | [diff] [blame] | 806 | * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the |
| 807 | * transceiver. |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 808 | */ |
Alexander Aring | c8c7e3d | 2014-12-19 10:36:50 +0100 | [diff] [blame] | 809 | if (trac) |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 810 | at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF, |
Alexander Aring | 2f8cdd9 | 2015-04-30 17:45:01 +0200 | [diff] [blame] | 811 | at86rf230_tx_on, true); |
Alexander Aring | c8c7e3d | 2014-12-19 10:36:50 +0100 | [diff] [blame] | 812 | else |
| 813 | at86rf230_tx_on(context); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 816 | static void |
| 817 | at86rf230_tx_trac_status(void *context) |
| 818 | { |
| 819 | struct at86rf230_state_change *ctx = context; |
| 820 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 821 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 822 | at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx, |
| 823 | at86rf230_tx_trac_check, true); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static void |
Alexander Aring | 74de4c8 | 2015-03-01 21:55:30 +0100 | [diff] [blame] | 827 | at86rf230_rx_read_frame_complete(void *context) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 828 | { |
Alexander Aring | 74de4c8 | 2015-03-01 21:55:30 +0100 | [diff] [blame] | 829 | struct at86rf230_state_change *ctx = context; |
| 830 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 831 | u8 rx_local_buf[AT86RF2XX_MAX_BUF]; |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 832 | const u8 *buf = ctx->buf; |
Alexander Aring | 74de4c8 | 2015-03-01 21:55:30 +0100 | [diff] [blame] | 833 | struct sk_buff *skb; |
| 834 | u8 len, lqi; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 835 | |
Alexander Aring | 74de4c8 | 2015-03-01 21:55:30 +0100 | [diff] [blame] | 836 | len = buf[1]; |
| 837 | if (!ieee802154_is_valid_psdu_len(len)) { |
| 838 | dev_vdbg(&lp->spi->dev, "corrupted frame received\n"); |
| 839 | len = IEEE802154_MTU; |
| 840 | } |
| 841 | lqi = buf[2 + len]; |
| 842 | |
| 843 | memcpy(rx_local_buf, buf + 2, len); |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 844 | ctx->trx.len = 2; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 845 | enable_irq(ctx->irq); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 846 | |
Alexander Aring | 61a2281 | 2014-10-27 17:13:29 +0100 | [diff] [blame] | 847 | skb = dev_alloc_skb(IEEE802154_MTU); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 848 | if (!skb) { |
| 849 | dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n"); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | memcpy(skb_put(skb, len), rx_local_buf, len); |
Alexander Aring | b89c334 | 2014-10-27 17:13:42 +0100 | [diff] [blame] | 854 | ieee802154_rx_irqsafe(lp->hw, skb, lqi); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static void |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 858 | at86rf230_rx_read_frame(void *context) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 859 | { |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 860 | struct at86rf230_state_change *ctx = context; |
| 861 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 862 | u8 *buf = ctx->buf; |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 863 | int rc; |
| 864 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 865 | buf[0] = CMD_FB; |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 866 | ctx->trx.len = AT86RF2XX_MAX_BUF; |
| 867 | ctx->msg.complete = at86rf230_rx_read_frame_complete; |
| 868 | rc = spi_async(lp->spi, &ctx->msg); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 869 | if (rc) { |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 870 | ctx->trx.len = 2; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 871 | enable_irq(ctx->irq); |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 872 | at86rf230_async_error(lp, ctx, rc); |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 873 | } |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static void |
| 877 | at86rf230_rx_trac_check(void *context) |
| 878 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 879 | /* Possible check on trac status here. This could be useful to make |
| 880 | * some stats why receive is failed. Not used at the moment, but it's |
| 881 | * maybe timing relevant. Datasheet doesn't say anything about this. |
| 882 | * The programming guide say do it so. |
| 883 | */ |
| 884 | |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 885 | at86rf230_rx_read_frame(context); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 888 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 889 | at86rf230_irq_trx_end(struct at86rf230_local *lp) |
| 890 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 891 | if (lp->is_tx) { |
| 892 | lp->is_tx = 0; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 893 | |
| 894 | if (lp->tx_aret) |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 895 | at86rf230_async_state_change(lp, &lp->irq, |
| 896 | STATE_FORCE_TX_ON, |
| 897 | at86rf230_tx_trac_status, |
| 898 | true); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 899 | else |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 900 | at86rf230_async_state_change(lp, &lp->irq, |
| 901 | STATE_RX_AACK_ON, |
| 902 | at86rf230_tx_complete, |
| 903 | true); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 904 | } else { |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 905 | at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq, |
| 906 | at86rf230_rx_trac_check, true); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | static void |
| 911 | at86rf230_irq_status(void *context) |
| 912 | { |
| 913 | struct at86rf230_state_change *ctx = context; |
| 914 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 915 | const u8 *buf = ctx->buf; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 916 | const u8 irq = buf[1]; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 917 | |
| 918 | if (irq & IRQ_TRX_END) { |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 919 | at86rf230_irq_trx_end(lp); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 920 | } else { |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 921 | enable_irq(ctx->irq); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 922 | dev_err(&lp->spi->dev, "not supported irq %02x received\n", |
| 923 | irq); |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | static irqreturn_t at86rf230_isr(int irq, void *data) |
| 928 | { |
| 929 | struct at86rf230_local *lp = data; |
| 930 | struct at86rf230_state_change *ctx = &lp->irq; |
| 931 | u8 *buf = ctx->buf; |
| 932 | int rc; |
| 933 | |
Alexander Aring | 9056636 | 2014-10-07 10:38:29 +0200 | [diff] [blame] | 934 | disable_irq_nosync(irq); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 935 | |
| 936 | buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 937 | ctx->msg.complete = at86rf230_irq_status; |
| 938 | rc = spi_async(lp->spi, &ctx->msg); |
| 939 | if (rc) { |
Alexander Aring | e931021 | 2014-10-07 10:38:30 +0200 | [diff] [blame] | 940 | enable_irq(irq); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 941 | at86rf230_async_error(lp, ctx, rc); |
| 942 | return IRQ_NONE; |
| 943 | } |
| 944 | |
| 945 | return IRQ_HANDLED; |
| 946 | } |
| 947 | |
| 948 | static void |
| 949 | at86rf230_write_frame_complete(void *context) |
| 950 | { |
| 951 | struct at86rf230_state_change *ctx = context; |
| 952 | struct at86rf230_local *lp = ctx->lp; |
| 953 | u8 *buf = ctx->buf; |
| 954 | int rc; |
| 955 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 956 | ctx->trx.len = 2; |
Alexander Aring | d2c8bf5 | 2015-04-30 17:45:03 +0200 | [diff] [blame] | 957 | |
| 958 | if (gpio_is_valid(lp->slp_tr)) { |
| 959 | at86rf230_slp_tr_rising_edge(lp); |
| 960 | } else { |
| 961 | buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE; |
| 962 | buf[1] = STATE_BUSY_TX; |
| 963 | ctx->msg.complete = NULL; |
| 964 | rc = spi_async(lp->spi, &ctx->msg); |
| 965 | if (rc) |
| 966 | at86rf230_async_error(lp, ctx, rc); |
| 967 | } |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | static void |
| 971 | at86rf230_write_frame(void *context) |
| 972 | { |
| 973 | struct at86rf230_state_change *ctx = context; |
| 974 | struct at86rf230_local *lp = ctx->lp; |
| 975 | struct sk_buff *skb = lp->tx_skb; |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 976 | u8 *buf = ctx->buf; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 977 | int rc; |
| 978 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 979 | lp->is_tx = 1; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 980 | |
| 981 | buf[0] = CMD_FB | CMD_WRITE; |
| 982 | buf[1] = skb->len + 2; |
| 983 | memcpy(buf + 2, skb->data, skb->len); |
Alexander Aring | 31fa743 | 2015-03-01 21:55:32 +0100 | [diff] [blame] | 984 | ctx->trx.len = skb->len + 2; |
| 985 | ctx->msg.complete = at86rf230_write_frame_complete; |
| 986 | rc = spi_async(lp->spi, &ctx->msg); |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 987 | if (rc) { |
| 988 | ctx->trx.len = 2; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 989 | at86rf230_async_error(lp, ctx, rc); |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 990 | } |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | static void |
| 994 | at86rf230_xmit_tx_on(void *context) |
| 995 | { |
| 996 | struct at86rf230_state_change *ctx = context; |
| 997 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 998 | |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 999 | at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON, |
| 1000 | at86rf230_write_frame, false); |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1003 | static void |
| 1004 | at86rf230_xmit_start(void *context) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1005 | { |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1006 | struct at86rf230_state_change *ctx = context; |
| 1007 | struct at86rf230_local *lp = ctx->lp; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* In ARET mode we need to go into STATE_TX_ARET_ON after we |
| 1010 | * are in STATE_TX_ON. The pfad differs here, so we change |
| 1011 | * the complete handler. |
| 1012 | */ |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 1013 | if (lp->tx_aret) { |
| 1014 | if (lp->is_tx_from_off) { |
| 1015 | lp->is_tx_from_off = false; |
| 1016 | at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON, |
| 1017 | at86rf230_xmit_tx_on, |
| 1018 | false); |
| 1019 | } else { |
| 1020 | at86rf230_async_state_change(lp, ctx, STATE_TX_ON, |
| 1021 | at86rf230_xmit_tx_on, |
| 1022 | false); |
| 1023 | } |
| 1024 | } else { |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1025 | at86rf230_async_state_change(lp, ctx, STATE_TX_ON, |
| 1026 | at86rf230_write_frame, false); |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 1027 | } |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1028 | } |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1029 | |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1030 | static int |
| 1031 | at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb) |
| 1032 | { |
| 1033 | struct at86rf230_local *lp = hw->priv; |
| 1034 | struct at86rf230_state_change *ctx = &lp->tx; |
| 1035 | |
| 1036 | lp->tx_skb = skb; |
Alexander Aring | ba6d223 | 2015-03-01 21:55:28 +0100 | [diff] [blame] | 1037 | lp->tx_retry = 0; |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* After 5 minutes in PLL and the same frequency we run again the |
| 1040 | * calibration loops which is recommended by at86rf2xx datasheets. |
| 1041 | * |
| 1042 | * The calibration is initiate by a state change from TRX_OFF |
| 1043 | * to TX_ON, the lp->cal_timeout should be reinit by state_delay |
| 1044 | * function then to start in the next 5 minutes. |
| 1045 | */ |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 1046 | if (time_is_before_jiffies(lp->cal_timeout)) { |
| 1047 | lp->is_tx_from_off = true; |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1048 | at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF, |
| 1049 | at86rf230_xmit_start, false); |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 1050 | } else { |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1051 | at86rf230_xmit_start(ctx); |
Alexander Aring | 8500920 | 2015-04-30 17:45:02 +0200 | [diff] [blame] | 1052 | } |
Alexander Aring | 97fed79 | 2014-10-07 10:38:32 +0200 | [diff] [blame] | 1053 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1054 | return 0; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1058 | at86rf230_ed(struct ieee802154_hw *hw, u8 *level) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1059 | { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1060 | BUG_ON(!level); |
| 1061 | *level = 0xbe; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1066 | at86rf230_start(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1067 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1068 | return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | static void |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1072 | at86rf230_stop(struct ieee802154_hw *hw) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1073 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1074 | at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static int |
Alexander Aring | e37d2ec | 2014-10-28 18:21:19 +0100 | [diff] [blame] | 1078 | at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1079 | { |
| 1080 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 1081 | } |
| 1082 | |
| 1083 | static int |
Alexander Aring | e37d2ec | 2014-10-28 18:21:19 +0100 | [diff] [blame] | 1084 | at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1085 | { |
| 1086 | int rc; |
| 1087 | |
| 1088 | if (channel == 0) |
| 1089 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0); |
| 1090 | else |
| 1091 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1); |
| 1092 | if (rc < 0) |
| 1093 | return rc; |
| 1094 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1095 | if (page == 0) { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1096 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1097 | lp->data->rssi_base_val = -100; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1098 | } else { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1099 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1100 | lp->data->rssi_base_val = -98; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1101 | } |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1102 | if (rc < 0) |
| 1103 | return rc; |
| 1104 | |
Alexander Aring | 24ccb9f | 2014-11-12 19:51:57 +0100 | [diff] [blame] | 1105 | /* This sets the symbol_duration according frequency on the 212. |
| 1106 | * TODO move this handling while set channel and page in cfg802154. |
| 1107 | * We can do that, this timings are according 802.15.4 standard. |
| 1108 | * If we do that in cfg802154, this is a more generic calculation. |
| 1109 | * |
| 1110 | * This should also protected from ifs_timer. Means cancel timer and |
| 1111 | * init with a new value. For now, this is okay. |
| 1112 | */ |
| 1113 | if (channel == 0) { |
| 1114 | if (page == 0) { |
| 1115 | /* SUB:0 and BPSK:0 -> BPSK-20 */ |
| 1116 | lp->hw->phy->symbol_duration = 50; |
| 1117 | } else { |
| 1118 | /* SUB:1 and BPSK:0 -> BPSK-40 */ |
| 1119 | lp->hw->phy->symbol_duration = 25; |
| 1120 | } |
| 1121 | } else { |
| 1122 | if (page == 0) |
Alexander Aring | 2d6dde2 | 2014-11-17 08:20:44 +0100 | [diff] [blame] | 1123 | /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */ |
Alexander Aring | 24ccb9f | 2014-11-12 19:51:57 +0100 | [diff] [blame] | 1124 | lp->hw->phy->symbol_duration = 40; |
| 1125 | else |
Alexander Aring | 2d6dde2 | 2014-11-17 08:20:44 +0100 | [diff] [blame] | 1126 | /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */ |
Alexander Aring | 24ccb9f | 2014-11-12 19:51:57 +0100 | [diff] [blame] | 1127 | lp->hw->phy->symbol_duration = 16; |
| 1128 | } |
| 1129 | |
| 1130 | lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD * |
| 1131 | lp->hw->phy->symbol_duration; |
| 1132 | lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD * |
| 1133 | lp->hw->phy->symbol_duration; |
| 1134 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1135 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 1136 | } |
| 1137 | |
| 1138 | static int |
Alexander Aring | e37d2ec | 2014-10-28 18:21:19 +0100 | [diff] [blame] | 1139 | at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1140 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1141 | struct at86rf230_local *lp = hw->priv; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1142 | int rc; |
| 1143 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1144 | rc = lp->data->set_channel(lp, page, channel); |
Alexander Aring | 984e0c6 | 2014-07-03 00:20:53 +0200 | [diff] [blame] | 1145 | /* Wait for PLL */ |
| 1146 | usleep_range(lp->data->t_channel_switch, |
| 1147 | lp->data->t_channel_switch + 10); |
Alexander Aring | dce481e | 2015-03-09 13:56:11 +0100 | [diff] [blame] | 1148 | |
| 1149 | lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT; |
Alexander Aring | 820bd66 | 2014-11-12 03:36:56 +0100 | [diff] [blame] | 1150 | return rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1154 | at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw, |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1155 | struct ieee802154_hw_addr_filt *filt, |
| 1156 | unsigned long changed) |
| 1157 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1158 | struct at86rf230_local *lp = hw->priv; |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1159 | |
Alexander Aring | 57205c1 | 2014-10-25 05:25:09 +0200 | [diff] [blame] | 1160 | if (changed & IEEE802154_AFILT_SADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1161 | u16 addr = le16_to_cpu(filt->short_addr); |
| 1162 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1163 | dev_vdbg(&lp->spi->dev, |
Stefan Schmidt | e80fb5e | 2014-12-12 12:45:29 +0100 | [diff] [blame] | 1164 | "at86rf230_set_hw_addr_filt called for saddr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1165 | __at86rf230_write(lp, RG_SHORT_ADDR_0, addr); |
| 1166 | __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Alexander Aring | 57205c1 | 2014-10-25 05:25:09 +0200 | [diff] [blame] | 1169 | if (changed & IEEE802154_AFILT_PANID_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1170 | u16 pan = le16_to_cpu(filt->pan_id); |
| 1171 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1172 | dev_vdbg(&lp->spi->dev, |
Stefan Schmidt | e80fb5e | 2014-12-12 12:45:29 +0100 | [diff] [blame] | 1173 | "at86rf230_set_hw_addr_filt called for pan id\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1174 | __at86rf230_write(lp, RG_PAN_ID_0, pan); |
| 1175 | __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Alexander Aring | 57205c1 | 2014-10-25 05:25:09 +0200 | [diff] [blame] | 1178 | if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1179 | u8 i, addr[8]; |
| 1180 | |
| 1181 | memcpy(addr, &filt->ieee_addr, 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1182 | dev_vdbg(&lp->spi->dev, |
Stefan Schmidt | e80fb5e | 2014-12-12 12:45:29 +0100 | [diff] [blame] | 1183 | "at86rf230_set_hw_addr_filt called for IEEE addr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1184 | for (i = 0; i < 8; i++) |
| 1185 | __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Alexander Aring | 57205c1 | 2014-10-25 05:25:09 +0200 | [diff] [blame] | 1188 | if (changed & IEEE802154_AFILT_PANC_CHANGED) { |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1189 | dev_vdbg(&lp->spi->dev, |
Stefan Schmidt | e80fb5e | 2014-12-12 12:45:29 +0100 | [diff] [blame] | 1190 | "at86rf230_set_hw_addr_filt called for panc change\n"); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1191 | if (filt->pan_coord) |
| 1192 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1); |
| 1193 | else |
| 1194 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0); |
| 1195 | } |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1200 | #define AT86RF23X_MAX_TX_POWERS 0xF |
| 1201 | static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = { |
| 1202 | 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600, |
| 1203 | -800, -1200, -1700, |
| 1204 | }; |
| 1205 | |
| 1206 | static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = { |
| 1207 | 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700, |
| 1208 | -900, -1200, -1700, |
| 1209 | }; |
| 1210 | |
| 1211 | #define AT86RF212_MAX_TX_POWERS 0x1F |
| 1212 | static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = { |
| 1213 | 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700, |
| 1214 | -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700, |
| 1215 | -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600, |
| 1216 | }; |
| 1217 | |
| 1218 | static int |
| 1219 | at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm) |
| 1220 | { |
| 1221 | u32 i; |
| 1222 | |
| 1223 | for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) { |
| 1224 | if (lp->hw->phy->supported.tx_powers[i] == mbm) |
| 1225 | return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i); |
| 1226 | } |
| 1227 | |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | |
| 1231 | static int |
| 1232 | at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm) |
| 1233 | { |
| 1234 | u32 i; |
| 1235 | |
| 1236 | for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) { |
| 1237 | if (lp->hw->phy->supported.tx_powers[i] == mbm) |
| 1238 | return at86rf230_write_subreg(lp, SR_TX_PWR_212, i); |
| 1239 | } |
| 1240 | |
| 1241 | return -EINVAL; |
| 1242 | } |
| 1243 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1244 | static int |
Alexander Aring | e2eb173 | 2015-05-17 21:44:40 +0200 | [diff] [blame] | 1245 | at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm) |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1246 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1247 | struct at86rf230_local *lp = hw->priv; |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1248 | |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1249 | return lp->data->set_txpower(lp, mbm); |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 1252 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1253 | at86rf230_set_lbt(struct ieee802154_hw *hw, bool on) |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 1254 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1255 | struct at86rf230_local *lp = hw->priv; |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 1256 | |
| 1257 | return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on); |
| 1258 | } |
| 1259 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1260 | static int |
Alexander Aring | 7fe9a38 | 2014-12-10 15:33:12 +0100 | [diff] [blame] | 1261 | at86rf230_set_cca_mode(struct ieee802154_hw *hw, |
| 1262 | const struct wpan_phy_cca *cca) |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1263 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1264 | struct at86rf230_local *lp = hw->priv; |
Alexander Aring | 7fe9a38 | 2014-12-10 15:33:12 +0100 | [diff] [blame] | 1265 | u8 val; |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1266 | |
Alexander Aring | 7fe9a38 | 2014-12-10 15:33:12 +0100 | [diff] [blame] | 1267 | /* mapping 802.15.4 to driver spec */ |
| 1268 | switch (cca->mode) { |
| 1269 | case NL802154_CCA_ENERGY: |
| 1270 | val = 1; |
| 1271 | break; |
| 1272 | case NL802154_CCA_CARRIER: |
| 1273 | val = 2; |
| 1274 | break; |
| 1275 | case NL802154_CCA_ENERGY_CARRIER: |
| 1276 | switch (cca->opt) { |
| 1277 | case NL802154_CCA_OPT_ENERGY_CARRIER_AND: |
| 1278 | val = 3; |
| 1279 | break; |
| 1280 | case NL802154_CCA_OPT_ENERGY_CARRIER_OR: |
| 1281 | val = 0; |
| 1282 | break; |
| 1283 | default: |
| 1284 | return -EINVAL; |
| 1285 | } |
| 1286 | break; |
| 1287 | default: |
| 1288 | return -EINVAL; |
| 1289 | } |
| 1290 | |
| 1291 | return at86rf230_write_subreg(lp, SR_CCA_MODE, val); |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1294 | static int |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1295 | at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level) |
| 1296 | { |
| 1297 | return (level - lp->data->rssi_base_val) * 100 / 207; |
| 1298 | } |
| 1299 | |
| 1300 | static int |
| 1301 | at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level) |
| 1302 | { |
| 1303 | return (level - lp->data->rssi_base_val) / 2; |
| 1304 | } |
| 1305 | |
| 1306 | static int |
Alexander Aring | 32b2355 | 2015-05-17 21:44:41 +0200 | [diff] [blame] | 1307 | at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm) |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1308 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1309 | struct at86rf230_local *lp = hw->priv; |
Alexander Aring | 32b2355 | 2015-05-17 21:44:41 +0200 | [diff] [blame] | 1310 | s32 level = mbm / 100; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1311 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1312 | if (level < lp->data->rssi_base_val || level > 30) |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1313 | return -EINVAL; |
| 1314 | |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1315 | return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, |
| 1316 | lp->data->get_desense_steps(lp, level)); |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1317 | } |
| 1318 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1319 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1320 | at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1321 | u8 retries) |
| 1322 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1323 | struct at86rf230_local *lp = hw->priv; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1324 | int rc; |
| 1325 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1326 | rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be); |
| 1327 | if (rc) |
| 1328 | return rc; |
| 1329 | |
| 1330 | rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be); |
| 1331 | if (rc) |
| 1332 | return rc; |
| 1333 | |
Alexander Aring | 39d7f32 | 2014-04-05 13:49:26 +0200 | [diff] [blame] | 1334 | return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries); |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static int |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1338 | at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries) |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1339 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1340 | struct at86rf230_local *lp = hw->priv; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1341 | int rc = 0; |
| 1342 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1343 | lp->tx_aret = retries >= 0; |
Alexander Aring | 850f43a | 2014-10-07 10:38:27 +0200 | [diff] [blame] | 1344 | lp->max_frame_retries = retries; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1345 | |
| 1346 | if (retries >= 0) |
| 1347 | rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries); |
| 1348 | |
| 1349 | return rc; |
| 1350 | } |
| 1351 | |
Alexander Aring | 92f45f5 | 2014-10-29 21:34:33 +0100 | [diff] [blame] | 1352 | static int |
| 1353 | at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on) |
| 1354 | { |
| 1355 | struct at86rf230_local *lp = hw->priv; |
| 1356 | int rc; |
| 1357 | |
| 1358 | if (on) { |
| 1359 | rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1); |
| 1360 | if (rc < 0) |
| 1361 | return rc; |
| 1362 | |
| 1363 | rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1); |
| 1364 | if (rc < 0) |
| 1365 | return rc; |
| 1366 | } else { |
| 1367 | rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0); |
| 1368 | if (rc < 0) |
| 1369 | return rc; |
| 1370 | |
| 1371 | rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0); |
| 1372 | if (rc < 0) |
| 1373 | return rc; |
| 1374 | } |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Alexander Aring | 1630186 | 2014-10-28 18:21:18 +0100 | [diff] [blame] | 1379 | static const struct ieee802154_ops at86rf230_ops = { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1380 | .owner = THIS_MODULE, |
Alexander Aring | 955aee8 | 2014-10-26 09:37:15 +0100 | [diff] [blame] | 1381 | .xmit_async = at86rf230_xmit, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1382 | .ed = at86rf230_ed, |
| 1383 | .set_channel = at86rf230_channel, |
| 1384 | .start = at86rf230_start, |
| 1385 | .stop = at86rf230_stop, |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1386 | .set_hw_addr_filt = at86rf230_set_hw_addr_filt, |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1387 | .set_txpower = at86rf230_set_txpower, |
| 1388 | .set_lbt = at86rf230_set_lbt, |
| 1389 | .set_cca_mode = at86rf230_set_cca_mode, |
| 1390 | .set_cca_ed_level = at86rf230_set_cca_ed_level, |
| 1391 | .set_csma_params = at86rf230_set_csma_params, |
| 1392 | .set_frame_retries = at86rf230_set_frame_retries, |
Alexander Aring | 92f45f5 | 2014-10-29 21:34:33 +0100 | [diff] [blame] | 1393 | .set_promiscuous_mode = at86rf230_set_promiscuous_mode, |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1394 | }; |
| 1395 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1396 | static struct at86rf2xx_chip_data at86rf233_data = { |
Alexander Aring | 7a4ef91 | 2014-07-03 00:20:54 +0200 | [diff] [blame] | 1397 | .t_sleep_cycle = 330, |
Alexander Aring | 984e0c6 | 2014-07-03 00:20:53 +0200 | [diff] [blame] | 1398 | .t_channel_switch = 11, |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 1399 | .t_reset_to_off = 26, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1400 | .t_off_to_aack = 80, |
| 1401 | .t_off_to_tx_on = 80, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1402 | .t_frame = 4096, |
| 1403 | .t_p_ack = 545, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1404 | .rssi_base_val = -91, |
| 1405 | .set_channel = at86rf23x_set_channel, |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1406 | .get_desense_steps = at86rf23x_get_desens_steps, |
| 1407 | .set_txpower = at86rf23x_set_txpower, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1408 | }; |
| 1409 | |
| 1410 | static struct at86rf2xx_chip_data at86rf231_data = { |
Alexander Aring | 7a4ef91 | 2014-07-03 00:20:54 +0200 | [diff] [blame] | 1411 | .t_sleep_cycle = 330, |
Alexander Aring | 984e0c6 | 2014-07-03 00:20:53 +0200 | [diff] [blame] | 1412 | .t_channel_switch = 24, |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 1413 | .t_reset_to_off = 37, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1414 | .t_off_to_aack = 110, |
| 1415 | .t_off_to_tx_on = 110, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1416 | .t_frame = 4096, |
| 1417 | .t_p_ack = 545, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1418 | .rssi_base_val = -91, |
| 1419 | .set_channel = at86rf23x_set_channel, |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1420 | .get_desense_steps = at86rf23x_get_desens_steps, |
| 1421 | .set_txpower = at86rf23x_set_txpower, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1422 | }; |
| 1423 | |
| 1424 | static struct at86rf2xx_chip_data at86rf212_data = { |
Alexander Aring | 7a4ef91 | 2014-07-03 00:20:54 +0200 | [diff] [blame] | 1425 | .t_sleep_cycle = 330, |
Alexander Aring | 984e0c6 | 2014-07-03 00:20:53 +0200 | [diff] [blame] | 1426 | .t_channel_switch = 11, |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 1427 | .t_reset_to_off = 26, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1428 | .t_off_to_aack = 200, |
| 1429 | .t_off_to_tx_on = 200, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1430 | .t_frame = 4096, |
| 1431 | .t_p_ack = 545, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1432 | .rssi_base_val = -100, |
| 1433 | .set_channel = at86rf212_set_channel, |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1434 | .get_desense_steps = at86rf212_get_desens_steps, |
| 1435 | .set_txpower = at86rf212_set_txpower, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1436 | }; |
| 1437 | |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1438 | static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1439 | { |
Alexander Aring | 1db0558 | 2014-07-03 00:20:50 +0200 | [diff] [blame] | 1440 | int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1441 | unsigned int dvdd; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1442 | u8 csma_seed[2]; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1443 | |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame] | 1444 | rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF); |
Phoebe Buckheister | 7dcbd22 | 2014-02-17 11:34:13 +0100 | [diff] [blame] | 1445 | if (rc) |
| 1446 | return rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1447 | |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1448 | irq_type = irq_get_trigger_type(lp->spi->irq); |
Alexander Aring | c91799c | 2015-02-27 09:58:30 +0100 | [diff] [blame] | 1449 | if (irq_type == IRQ_TYPE_EDGE_RISING || |
| 1450 | irq_type == IRQ_TYPE_EDGE_FALLING) |
| 1451 | dev_warn(&lp->spi->dev, |
| 1452 | "Using edge triggered irq's are not recommended!\n"); |
Alexander Aring | 702d211 | 2015-02-27 09:58:29 +0100 | [diff] [blame] | 1453 | if (irq_type == IRQ_TYPE_EDGE_FALLING || |
| 1454 | irq_type == IRQ_TYPE_LEVEL_LOW) |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1455 | irq_pol = IRQ_ACTIVE_LOW; |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1456 | |
Alexander Aring | 18c6504 | 2014-04-24 19:09:18 +0200 | [diff] [blame] | 1457 | rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1458 | if (rc) |
| 1459 | return rc; |
| 1460 | |
Alexander Aring | 6bd2b13 | 2014-07-03 00:20:49 +0200 | [diff] [blame] | 1461 | rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1); |
| 1462 | if (rc) |
| 1463 | return rc; |
| 1464 | |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1465 | rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1466 | if (rc) |
| 1467 | return rc; |
| 1468 | |
Alexander Aring | be64f07 | 2015-02-27 09:58:28 +0100 | [diff] [blame] | 1469 | /* reset values differs in at86rf231 and at86rf233 */ |
| 1470 | rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0); |
| 1471 | if (rc) |
| 1472 | return rc; |
| 1473 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1474 | get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed)); |
| 1475 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]); |
| 1476 | if (rc) |
| 1477 | return rc; |
| 1478 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]); |
| 1479 | if (rc) |
| 1480 | return rc; |
| 1481 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1482 | /* CLKM changes are applied immediately */ |
| 1483 | rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00); |
| 1484 | if (rc) |
| 1485 | return rc; |
| 1486 | |
| 1487 | /* Turn CLKM Off */ |
| 1488 | rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00); |
| 1489 | if (rc) |
| 1490 | return rc; |
| 1491 | /* Wait the next SLEEP cycle */ |
Alexander Aring | 7a4ef91 | 2014-07-03 00:20:54 +0200 | [diff] [blame] | 1492 | usleep_range(lp->data->t_sleep_cycle, |
| 1493 | lp->data->t_sleep_cycle + 100); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1494 | |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1495 | /* xtal_trim value is calculated by: |
| 1496 | * CL = 0.5 * (CX + CTRIM + CPAR) |
| 1497 | * |
| 1498 | * whereas: |
| 1499 | * CL = capacitor of used crystal |
| 1500 | * CX = connected capacitors at xtal pins |
| 1501 | * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF, |
| 1502 | * but this is different on each board setup. You need to fine |
| 1503 | * tuning this value via CTRIM. |
| 1504 | * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is |
| 1505 | * 0 pF upto 4.5 pF. |
| 1506 | * |
| 1507 | * Examples: |
| 1508 | * atben transceiver: |
| 1509 | * |
| 1510 | * CL = 8 pF |
| 1511 | * CX = 12 pF |
| 1512 | * CPAR = 3 pF (We assume the magic constant from datasheet) |
| 1513 | * CTRIM = 0.9 pF |
| 1514 | * |
| 1515 | * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF |
| 1516 | * |
| 1517 | * xtal_trim = 0x3 |
| 1518 | * |
| 1519 | * openlabs transceiver: |
| 1520 | * |
| 1521 | * CL = 16 pF |
| 1522 | * CX = 22 pF |
| 1523 | * CPAR = 3 pF (We assume the magic constant from datasheet) |
| 1524 | * CTRIM = 4.5 pF |
| 1525 | * |
| 1526 | * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF |
| 1527 | * |
| 1528 | * xtal_trim = 0xf |
| 1529 | */ |
| 1530 | rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim); |
| 1531 | if (rc) |
| 1532 | return rc; |
| 1533 | |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1534 | rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1535 | if (rc) |
| 1536 | return rc; |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1537 | if (!dvdd) { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1538 | dev_err(&lp->spi->dev, "DVDD error\n"); |
| 1539 | return -EINVAL; |
| 1540 | } |
| 1541 | |
Alexander Aring | 05e3f2f | 2014-11-05 20:51:27 +0100 | [diff] [blame] | 1542 | /* Force setting slotted operation bit to 0. Sometimes the atben |
| 1543 | * sets this bit and I don't know why. We set this always force |
| 1544 | * to zero while probing. |
| 1545 | */ |
Fengguang Wu | 6cc6399 | 2014-11-06 15:31:57 +0800 | [diff] [blame] | 1546 | return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1549 | static int |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1550 | at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr, |
| 1551 | u8 *xtal_trim) |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1552 | { |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1553 | struct at86rf230_platform_data *pdata = spi->dev.platform_data; |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1554 | int ret; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1555 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1556 | if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) { |
| 1557 | if (!pdata) |
| 1558 | return -ENOENT; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1559 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1560 | *rstn = pdata->rstn; |
| 1561 | *slp_tr = pdata->slp_tr; |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1562 | *xtal_trim = pdata->xtal_trim; |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1565 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1566 | *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0); |
| 1567 | *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0); |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1568 | ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim); |
| 1569 | if (ret < 0 && ret != -EINVAL) |
| 1570 | return ret; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1571 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1572 | return 0; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1575 | static int |
| 1576 | at86rf230_detect_device(struct at86rf230_local *lp) |
| 1577 | { |
| 1578 | unsigned int part, version, val; |
| 1579 | u16 man_id = 0; |
| 1580 | const char *chip; |
| 1581 | int rc; |
| 1582 | |
| 1583 | rc = __at86rf230_read(lp, RG_MAN_ID_0, &val); |
| 1584 | if (rc) |
| 1585 | return rc; |
| 1586 | man_id |= val; |
| 1587 | |
| 1588 | rc = __at86rf230_read(lp, RG_MAN_ID_1, &val); |
| 1589 | if (rc) |
| 1590 | return rc; |
| 1591 | man_id |= (val << 8); |
| 1592 | |
| 1593 | rc = __at86rf230_read(lp, RG_PART_NUM, &part); |
| 1594 | if (rc) |
| 1595 | return rc; |
| 1596 | |
Andrey Yurovsky | 7598968 | 2014-12-17 13:14:42 -0800 | [diff] [blame] | 1597 | rc = __at86rf230_read(lp, RG_VERSION_NUM, &version); |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1598 | if (rc) |
| 1599 | return rc; |
| 1600 | |
| 1601 | if (man_id != 0x001f) { |
| 1602 | dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n", |
| 1603 | man_id >> 8, man_id & 0xFF); |
| 1604 | return -EINVAL; |
| 1605 | } |
| 1606 | |
Alexander Aring | 2ac0f3a | 2014-10-29 21:34:43 +0100 | [diff] [blame] | 1607 | lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK | |
Alexander Aring | edea8f7 | 2015-05-17 21:44:46 +0200 | [diff] [blame] | 1608 | IEEE802154_HW_CSMA_PARAMS | |
| 1609 | IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT | |
| 1610 | IEEE802154_HW_PROMISCUOUS; |
| 1611 | |
| 1612 | lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | |
| 1613 | WPAN_PHY_FLAG_CCA_ED_LEVEL | |
| 1614 | WPAN_PHY_FLAG_CCA_MODE; |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1615 | |
Alexander Aring | 8377d22 | 2015-05-17 21:44:48 +0200 | [diff] [blame] | 1616 | lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) | |
| 1617 | BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER); |
| 1618 | lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) | |
| 1619 | BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR); |
| 1620 | |
Alexander Aring | b48a7c1 | 2014-12-10 15:33:14 +0100 | [diff] [blame] | 1621 | lp->hw->phy->cca.mode = NL802154_CCA_ENERGY; |
| 1622 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1623 | switch (part) { |
| 1624 | case 2: |
| 1625 | chip = "at86rf230"; |
| 1626 | rc = -ENOTSUPP; |
| 1627 | break; |
| 1628 | case 3: |
| 1629 | chip = "at86rf231"; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1630 | lp->data = &at86rf231_data; |
Alexander Aring | 72f655e | 2015-05-17 21:44:42 +0200 | [diff] [blame] | 1631 | lp->hw->phy->supported.channels[0] = 0x7FFF800; |
Alexander Aring | fe58d01 | 2014-11-02 04:18:34 +0100 | [diff] [blame] | 1632 | lp->hw->phy->current_channel = 11; |
Alexander Aring | 24ccb9f | 2014-11-12 19:51:57 +0100 | [diff] [blame] | 1633 | lp->hw->phy->symbol_duration = 16; |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1634 | lp->hw->phy->supported.tx_powers = at86rf231_powers; |
| 1635 | lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers); |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1636 | break; |
| 1637 | case 7: |
| 1638 | chip = "at86rf212"; |
Andrey Yurovsky | 4ecc8a5 | 2014-12-18 15:36:18 -0800 | [diff] [blame] | 1639 | lp->data = &at86rf212_data; |
| 1640 | lp->hw->flags |= IEEE802154_HW_LBT; |
Alexander Aring | 72f655e | 2015-05-17 21:44:42 +0200 | [diff] [blame] | 1641 | lp->hw->phy->supported.channels[0] = 0x00007FF; |
| 1642 | lp->hw->phy->supported.channels[2] = 0x00007FF; |
Andrey Yurovsky | 4ecc8a5 | 2014-12-18 15:36:18 -0800 | [diff] [blame] | 1643 | lp->hw->phy->current_channel = 5; |
| 1644 | lp->hw->phy->symbol_duration = 25; |
Alexander Aring | 8377d22 | 2015-05-17 21:44:48 +0200 | [diff] [blame] | 1645 | lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH; |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1646 | lp->hw->phy->supported.tx_powers = at86rf212_powers; |
| 1647 | lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers); |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1648 | break; |
| 1649 | case 11: |
| 1650 | chip = "at86rf233"; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1651 | lp->data = &at86rf233_data; |
Alexander Aring | 72f655e | 2015-05-17 21:44:42 +0200 | [diff] [blame] | 1652 | lp->hw->phy->supported.channels[0] = 0x7FFF800; |
Alexander Aring | fe58d01 | 2014-11-02 04:18:34 +0100 | [diff] [blame] | 1653 | lp->hw->phy->current_channel = 13; |
Alexander Aring | 24ccb9f | 2014-11-12 19:51:57 +0100 | [diff] [blame] | 1654 | lp->hw->phy->symbol_duration = 16; |
Alexander Aring | 6f4da3f | 2015-05-17 21:44:49 +0200 | [diff] [blame^] | 1655 | lp->hw->phy->supported.tx_powers = at86rf233_powers; |
| 1656 | lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers); |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1657 | break; |
| 1658 | default: |
Stefan Schmidt | 2b8b7e2 | 2014-12-12 12:45:30 +0100 | [diff] [blame] | 1659 | chip = "unknown"; |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1660 | rc = -ENOTSUPP; |
| 1661 | break; |
| 1662 | } |
| 1663 | |
| 1664 | dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version); |
| 1665 | |
| 1666 | return rc; |
| 1667 | } |
| 1668 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1669 | static void |
| 1670 | at86rf230_setup_spi_messages(struct at86rf230_local *lp) |
| 1671 | { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1672 | lp->state.lp = lp; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 1673 | lp->state.irq = lp->spi->irq; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1674 | spi_message_init(&lp->state.msg); |
| 1675 | lp->state.msg.context = &lp->state; |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 1676 | lp->state.trx.len = 2; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1677 | lp->state.trx.tx_buf = lp->state.buf; |
| 1678 | lp->state.trx.rx_buf = lp->state.buf; |
| 1679 | spi_message_add_tail(&lp->state.trx, &lp->state.msg); |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 1680 | hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1681 | lp->state.timer.function = at86rf230_async_state_timer; |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1682 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1683 | lp->irq.lp = lp; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 1684 | lp->irq.irq = lp->spi->irq; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1685 | spi_message_init(&lp->irq.msg); |
| 1686 | lp->irq.msg.context = &lp->irq; |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 1687 | lp->irq.trx.len = 2; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1688 | lp->irq.trx.tx_buf = lp->irq.buf; |
| 1689 | lp->irq.trx.rx_buf = lp->irq.buf; |
| 1690 | spi_message_add_tail(&lp->irq.trx, &lp->irq.msg); |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 1691 | hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1692 | lp->irq.timer.function = at86rf230_async_state_timer; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1693 | |
| 1694 | lp->tx.lp = lp; |
Alexander Aring | cca990c | 2015-03-01 21:55:31 +0100 | [diff] [blame] | 1695 | lp->tx.irq = lp->spi->irq; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1696 | spi_message_init(&lp->tx.msg); |
| 1697 | lp->tx.msg.context = &lp->tx; |
Alexander Aring | 263be33 | 2015-03-01 21:55:33 +0100 | [diff] [blame] | 1698 | lp->tx.trx.len = 2; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1699 | lp->tx.trx.tx_buf = lp->tx.buf; |
| 1700 | lp->tx.trx.rx_buf = lp->tx.buf; |
| 1701 | spi_message_add_tail(&lp->tx.trx, &lp->tx.msg); |
Alexander Aring | eb3b435 | 2015-03-09 13:56:10 +0100 | [diff] [blame] | 1702 | hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1703 | lp->tx.timer.function = at86rf230_async_state_timer; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1706 | static int at86rf230_probe(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1707 | { |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1708 | struct ieee802154_hw *hw; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1709 | struct at86rf230_local *lp; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1710 | unsigned int status; |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1711 | int rc, irq_type, rstn, slp_tr; |
Alexander Aring | e372174 | 2015-03-07 22:07:07 +0100 | [diff] [blame] | 1712 | u8 xtal_trim = 0; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1713 | |
| 1714 | if (!spi->irq) { |
| 1715 | dev_err(&spi->dev, "no IRQ specified\n"); |
| 1716 | return -EINVAL; |
| 1717 | } |
| 1718 | |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1719 | rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim); |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1720 | if (rc < 0) { |
| 1721 | dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc); |
| 1722 | return rc; |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1725 | if (gpio_is_valid(rstn)) { |
| 1726 | rc = devm_gpio_request_one(&spi->dev, rstn, |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1727 | GPIOF_OUT_INIT_HIGH, "rstn"); |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1728 | if (rc) |
| 1729 | return rc; |
| 1730 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1731 | |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1732 | if (gpio_is_valid(slp_tr)) { |
| 1733 | rc = devm_gpio_request_one(&spi->dev, slp_tr, |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1734 | GPIOF_OUT_INIT_LOW, "slp_tr"); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1735 | if (rc) |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1736 | return rc; |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* Reset */ |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1740 | if (gpio_is_valid(rstn)) { |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1741 | udelay(1); |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1742 | gpio_set_value(rstn, 0); |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1743 | udelay(1); |
Alexander Aring | aaa1c4d | 2015-02-27 09:58:25 +0100 | [diff] [blame] | 1744 | gpio_set_value(rstn, 1); |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1745 | usleep_range(120, 240); |
| 1746 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1747 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1748 | hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops); |
| 1749 | if (!hw) |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1750 | return -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1751 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1752 | lp = hw->priv; |
| 1753 | lp->hw = hw; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1754 | lp->spi = spi; |
Alexander Aring | d2c8bf5 | 2015-04-30 17:45:03 +0200 | [diff] [blame] | 1755 | lp->slp_tr = slp_tr; |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1756 | hw->parent = &spi->dev; |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 1757 | hw->vif_data_size = sizeof(*lp); |
Alexander Aring | f6f4e86 | 2014-11-05 20:51:26 +0100 | [diff] [blame] | 1758 | ieee802154_random_extended_addr(&hw->phy->perm_extended_addr); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1759 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1760 | lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config); |
| 1761 | if (IS_ERR(lp->regmap)) { |
| 1762 | rc = PTR_ERR(lp->regmap); |
| 1763 | dev_err(&spi->dev, "Failed to allocate register map: %d\n", |
| 1764 | rc); |
| 1765 | goto free_dev; |
| 1766 | } |
| 1767 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1768 | at86rf230_setup_spi_messages(lp); |
| 1769 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1770 | rc = at86rf230_detect_device(lp); |
| 1771 | if (rc < 0) |
| 1772 | goto free_dev; |
| 1773 | |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1774 | init_completion(&lp->state_complete); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1775 | |
| 1776 | spi_set_drvdata(spi, lp); |
| 1777 | |
Alexander Aring | ccdaeb2 | 2015-02-27 09:58:26 +0100 | [diff] [blame] | 1778 | rc = at86rf230_hw_init(lp, xtal_trim); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1779 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1780 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1781 | |
Alexander Aring | 1962694 | 2014-04-24 19:09:15 +0200 | [diff] [blame] | 1782 | /* Read irq status register to reset irq line */ |
| 1783 | rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1784 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1785 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1786 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1787 | irq_type = irq_get_trigger_type(spi->irq); |
| 1788 | if (!irq_type) |
| 1789 | irq_type = IRQF_TRIGGER_RISING; |
| 1790 | |
| 1791 | rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr, |
| 1792 | IRQF_SHARED | irq_type, dev_name(&spi->dev), lp); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1793 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1794 | goto free_dev; |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1795 | |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1796 | rc = ieee802154_register_hw(lp->hw); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1797 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1798 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1799 | |
| 1800 | return rc; |
| 1801 | |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1802 | free_dev: |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1803 | ieee802154_free_hw(lp->hw); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1804 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1805 | return rc; |
| 1806 | } |
| 1807 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1808 | static int at86rf230_remove(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1809 | { |
| 1810 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
| 1811 | |
Alexander Aring | 17e84a9 | 2014-03-31 03:26:51 +0200 | [diff] [blame] | 1812 | /* mask all at86rf230 irq's */ |
| 1813 | at86rf230_write_subreg(lp, SR_IRQ_MASK, 0); |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 1814 | ieee802154_unregister_hw(lp->hw); |
| 1815 | ieee802154_free_hw(lp->hw); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1816 | dev_dbg(&spi->dev, "unregistered at86rf230\n"); |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1817 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
Alexander Aring | 1086b4f | 2014-04-24 19:09:11 +0200 | [diff] [blame] | 1821 | static const struct of_device_id at86rf230_of_match[] = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1822 | { .compatible = "atmel,at86rf230", }, |
| 1823 | { .compatible = "atmel,at86rf231", }, |
| 1824 | { .compatible = "atmel,at86rf233", }, |
| 1825 | { .compatible = "atmel,at86rf212", }, |
| 1826 | { }, |
| 1827 | }; |
Alexander Aring | 835cb7d | 2014-04-24 19:09:10 +0200 | [diff] [blame] | 1828 | MODULE_DEVICE_TABLE(of, at86rf230_of_match); |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1829 | |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1830 | static const struct spi_device_id at86rf230_device_id[] = { |
| 1831 | { .name = "at86rf230", }, |
| 1832 | { .name = "at86rf231", }, |
| 1833 | { .name = "at86rf233", }, |
| 1834 | { .name = "at86rf212", }, |
| 1835 | { }, |
| 1836 | }; |
| 1837 | MODULE_DEVICE_TABLE(spi, at86rf230_device_id); |
| 1838 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1839 | static struct spi_driver at86rf230_driver = { |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1840 | .id_table = at86rf230_device_id, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1841 | .driver = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1842 | .of_match_table = of_match_ptr(at86rf230_of_match), |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1843 | .name = "at86rf230", |
| 1844 | .owner = THIS_MODULE, |
| 1845 | }, |
| 1846 | .probe = at86rf230_probe, |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1847 | .remove = at86rf230_remove, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1848 | }; |
| 1849 | |
alex.bluesman.smirnov@gmail.com | 395a573 | 2012-08-26 05:10:10 +0000 | [diff] [blame] | 1850 | module_spi_driver(at86rf230_driver); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1851 | |
| 1852 | MODULE_DESCRIPTION("AT86RF230 Transceiver Driver"); |
| 1853 | MODULE_LICENSE("GPL v2"); |