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 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Written by: |
| 20 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 21 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 22 | */ |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/interrupt.h> |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 26 | #include <linux/irq.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 27 | #include <linux/gpio.h> |
| 28 | #include <linux/delay.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/spi/spi.h> |
| 31 | #include <linux/spi/at86rf230.h> |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 32 | #include <linux/regmap.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 33 | #include <linux/skbuff.h> |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 34 | #include <linux/of_gpio.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 35 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 36 | #include <net/ieee802154.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 37 | #include <net/mac802154.h> |
| 38 | #include <net/wpan-phy.h> |
| 39 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 40 | struct at86rf230_local; |
| 41 | /* at86rf2xx chip depend data. |
| 42 | * All timings are in us. |
| 43 | */ |
| 44 | struct at86rf2xx_chip_data { |
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; |
| 50 | /* short interframe spacing time */ |
| 51 | u16 t_sifs; |
| 52 | /* long interframe spacing time */ |
| 53 | u16 t_lifs; |
| 54 | /* completion timeout for tx in msecs */ |
| 55 | u16 t_tx_timeout; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 56 | int rssi_base_val; |
| 57 | |
| 58 | int (*set_channel)(struct at86rf230_local *, int, int); |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 59 | int (*get_desense_steps)(struct at86rf230_local *, s32); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 62 | #define AT86RF2XX_MAX_BUF (127 + 3) |
| 63 | |
| 64 | struct at86rf230_state_change { |
| 65 | struct at86rf230_local *lp; |
| 66 | |
| 67 | struct spi_message msg; |
| 68 | struct spi_transfer trx; |
| 69 | u8 buf[AT86RF2XX_MAX_BUF]; |
| 70 | |
| 71 | void (*complete)(void *context); |
| 72 | u8 from_state; |
| 73 | u8 to_state; |
| 74 | }; |
| 75 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 76 | struct at86rf230_local { |
| 77 | struct spi_device *spi; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 78 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 79 | struct ieee802154_dev *dev; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 80 | struct at86rf2xx_chip_data *data; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 81 | struct regmap *regmap; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 82 | |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 83 | struct completion state_complete; |
| 84 | struct at86rf230_state_change state; |
| 85 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 86 | struct at86rf230_state_change irq; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 87 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 88 | bool tx_aret; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 89 | bool is_tx; |
| 90 | /* spinlock for is_tx protection */ |
| 91 | spinlock_t lock; |
| 92 | struct completion tx_complete; |
| 93 | struct sk_buff *tx_skb; |
| 94 | struct at86rf230_state_change tx; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | #define RG_TRX_STATUS (0x01) |
| 98 | #define SR_TRX_STATUS 0x01, 0x1f, 0 |
| 99 | #define SR_RESERVED_01_3 0x01, 0x20, 5 |
| 100 | #define SR_CCA_STATUS 0x01, 0x40, 6 |
| 101 | #define SR_CCA_DONE 0x01, 0x80, 7 |
| 102 | #define RG_TRX_STATE (0x02) |
| 103 | #define SR_TRX_CMD 0x02, 0x1f, 0 |
| 104 | #define SR_TRAC_STATUS 0x02, 0xe0, 5 |
| 105 | #define RG_TRX_CTRL_0 (0x03) |
| 106 | #define SR_CLKM_CTRL 0x03, 0x07, 0 |
| 107 | #define SR_CLKM_SHA_SEL 0x03, 0x08, 3 |
| 108 | #define SR_PAD_IO_CLKM 0x03, 0x30, 4 |
| 109 | #define SR_PAD_IO 0x03, 0xc0, 6 |
| 110 | #define RG_TRX_CTRL_1 (0x04) |
| 111 | #define SR_IRQ_POLARITY 0x04, 0x01, 0 |
| 112 | #define SR_IRQ_MASK_MODE 0x04, 0x02, 1 |
| 113 | #define SR_SPI_CMD_MODE 0x04, 0x0c, 2 |
| 114 | #define SR_RX_BL_CTRL 0x04, 0x10, 4 |
| 115 | #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5 |
| 116 | #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6 |
| 117 | #define SR_PA_EXT_EN 0x04, 0x80, 7 |
| 118 | #define RG_PHY_TX_PWR (0x05) |
| 119 | #define SR_TX_PWR 0x05, 0x0f, 0 |
| 120 | #define SR_PA_LT 0x05, 0x30, 4 |
| 121 | #define SR_PA_BUF_LT 0x05, 0xc0, 6 |
| 122 | #define RG_PHY_RSSI (0x06) |
| 123 | #define SR_RSSI 0x06, 0x1f, 0 |
| 124 | #define SR_RND_VALUE 0x06, 0x60, 5 |
| 125 | #define SR_RX_CRC_VALID 0x06, 0x80, 7 |
| 126 | #define RG_PHY_ED_LEVEL (0x07) |
| 127 | #define SR_ED_LEVEL 0x07, 0xff, 0 |
| 128 | #define RG_PHY_CC_CCA (0x08) |
| 129 | #define SR_CHANNEL 0x08, 0x1f, 0 |
| 130 | #define SR_CCA_MODE 0x08, 0x60, 5 |
| 131 | #define SR_CCA_REQUEST 0x08, 0x80, 7 |
| 132 | #define RG_CCA_THRES (0x09) |
| 133 | #define SR_CCA_ED_THRES 0x09, 0x0f, 0 |
| 134 | #define SR_RESERVED_09_1 0x09, 0xf0, 4 |
| 135 | #define RG_RX_CTRL (0x0a) |
| 136 | #define SR_PDT_THRES 0x0a, 0x0f, 0 |
| 137 | #define SR_RESERVED_0a_1 0x0a, 0xf0, 4 |
| 138 | #define RG_SFD_VALUE (0x0b) |
| 139 | #define SR_SFD_VALUE 0x0b, 0xff, 0 |
| 140 | #define RG_TRX_CTRL_2 (0x0c) |
| 141 | #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0 |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 142 | #define SR_SUB_MODE 0x0c, 0x04, 2 |
| 143 | #define SR_BPSK_QPSK 0x0c, 0x08, 3 |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 144 | #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4 |
| 145 | #define SR_RESERVED_0c_5 0x0c, 0x60, 5 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 146 | #define SR_RX_SAFE_MODE 0x0c, 0x80, 7 |
| 147 | #define RG_ANT_DIV (0x0d) |
| 148 | #define SR_ANT_CTRL 0x0d, 0x03, 0 |
| 149 | #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2 |
| 150 | #define SR_ANT_DIV_EN 0x0d, 0x08, 3 |
| 151 | #define SR_RESERVED_0d_2 0x0d, 0x70, 4 |
| 152 | #define SR_ANT_SEL 0x0d, 0x80, 7 |
| 153 | #define RG_IRQ_MASK (0x0e) |
| 154 | #define SR_IRQ_MASK 0x0e, 0xff, 0 |
| 155 | #define RG_IRQ_STATUS (0x0f) |
| 156 | #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0 |
| 157 | #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1 |
| 158 | #define SR_IRQ_2_RX_START 0x0f, 0x04, 2 |
| 159 | #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3 |
| 160 | #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4 |
| 161 | #define SR_IRQ_5_AMI 0x0f, 0x20, 5 |
| 162 | #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6 |
| 163 | #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7 |
| 164 | #define RG_VREG_CTRL (0x10) |
| 165 | #define SR_RESERVED_10_6 0x10, 0x03, 0 |
| 166 | #define SR_DVDD_OK 0x10, 0x04, 2 |
| 167 | #define SR_DVREG_EXT 0x10, 0x08, 3 |
| 168 | #define SR_RESERVED_10_3 0x10, 0x30, 4 |
| 169 | #define SR_AVDD_OK 0x10, 0x40, 6 |
| 170 | #define SR_AVREG_EXT 0x10, 0x80, 7 |
| 171 | #define RG_BATMON (0x11) |
| 172 | #define SR_BATMON_VTH 0x11, 0x0f, 0 |
| 173 | #define SR_BATMON_HR 0x11, 0x10, 4 |
| 174 | #define SR_BATMON_OK 0x11, 0x20, 5 |
| 175 | #define SR_RESERVED_11_1 0x11, 0xc0, 6 |
| 176 | #define RG_XOSC_CTRL (0x12) |
| 177 | #define SR_XTAL_TRIM 0x12, 0x0f, 0 |
| 178 | #define SR_XTAL_MODE 0x12, 0xf0, 4 |
| 179 | #define RG_RX_SYN (0x15) |
| 180 | #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0 |
| 181 | #define SR_RESERVED_15_2 0x15, 0x70, 4 |
| 182 | #define SR_RX_PDT_DIS 0x15, 0x80, 7 |
| 183 | #define RG_XAH_CTRL_1 (0x17) |
| 184 | #define SR_RESERVED_17_8 0x17, 0x01, 0 |
| 185 | #define SR_AACK_PROM_MODE 0x17, 0x02, 1 |
| 186 | #define SR_AACK_ACK_TIME 0x17, 0x04, 2 |
| 187 | #define SR_RESERVED_17_5 0x17, 0x08, 3 |
| 188 | #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4 |
| 189 | #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5 |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 190 | #define SR_CSMA_LBT_MODE 0x17, 0x40, 6 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 191 | #define SR_RESERVED_17_1 0x17, 0x80, 7 |
| 192 | #define RG_FTN_CTRL (0x18) |
| 193 | #define SR_RESERVED_18_2 0x18, 0x7f, 0 |
| 194 | #define SR_FTN_START 0x18, 0x80, 7 |
| 195 | #define RG_PLL_CF (0x1a) |
| 196 | #define SR_RESERVED_1a_2 0x1a, 0x7f, 0 |
| 197 | #define SR_PLL_CF_START 0x1a, 0x80, 7 |
| 198 | #define RG_PLL_DCU (0x1b) |
| 199 | #define SR_RESERVED_1b_3 0x1b, 0x3f, 0 |
| 200 | #define SR_RESERVED_1b_2 0x1b, 0x40, 6 |
| 201 | #define SR_PLL_DCU_START 0x1b, 0x80, 7 |
| 202 | #define RG_PART_NUM (0x1c) |
| 203 | #define SR_PART_NUM 0x1c, 0xff, 0 |
| 204 | #define RG_VERSION_NUM (0x1d) |
| 205 | #define SR_VERSION_NUM 0x1d, 0xff, 0 |
| 206 | #define RG_MAN_ID_0 (0x1e) |
| 207 | #define SR_MAN_ID_0 0x1e, 0xff, 0 |
| 208 | #define RG_MAN_ID_1 (0x1f) |
| 209 | #define SR_MAN_ID_1 0x1f, 0xff, 0 |
| 210 | #define RG_SHORT_ADDR_0 (0x20) |
| 211 | #define SR_SHORT_ADDR_0 0x20, 0xff, 0 |
| 212 | #define RG_SHORT_ADDR_1 (0x21) |
| 213 | #define SR_SHORT_ADDR_1 0x21, 0xff, 0 |
| 214 | #define RG_PAN_ID_0 (0x22) |
| 215 | #define SR_PAN_ID_0 0x22, 0xff, 0 |
| 216 | #define RG_PAN_ID_1 (0x23) |
| 217 | #define SR_PAN_ID_1 0x23, 0xff, 0 |
| 218 | #define RG_IEEE_ADDR_0 (0x24) |
| 219 | #define SR_IEEE_ADDR_0 0x24, 0xff, 0 |
| 220 | #define RG_IEEE_ADDR_1 (0x25) |
| 221 | #define SR_IEEE_ADDR_1 0x25, 0xff, 0 |
| 222 | #define RG_IEEE_ADDR_2 (0x26) |
| 223 | #define SR_IEEE_ADDR_2 0x26, 0xff, 0 |
| 224 | #define RG_IEEE_ADDR_3 (0x27) |
| 225 | #define SR_IEEE_ADDR_3 0x27, 0xff, 0 |
| 226 | #define RG_IEEE_ADDR_4 (0x28) |
| 227 | #define SR_IEEE_ADDR_4 0x28, 0xff, 0 |
| 228 | #define RG_IEEE_ADDR_5 (0x29) |
| 229 | #define SR_IEEE_ADDR_5 0x29, 0xff, 0 |
| 230 | #define RG_IEEE_ADDR_6 (0x2a) |
| 231 | #define SR_IEEE_ADDR_6 0x2a, 0xff, 0 |
| 232 | #define RG_IEEE_ADDR_7 (0x2b) |
| 233 | #define SR_IEEE_ADDR_7 0x2b, 0xff, 0 |
| 234 | #define RG_XAH_CTRL_0 (0x2c) |
| 235 | #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0 |
| 236 | #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1 |
| 237 | #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4 |
| 238 | #define RG_CSMA_SEED_0 (0x2d) |
| 239 | #define SR_CSMA_SEED_0 0x2d, 0xff, 0 |
| 240 | #define RG_CSMA_SEED_1 (0x2e) |
| 241 | #define SR_CSMA_SEED_1 0x2e, 0x07, 0 |
| 242 | #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3 |
| 243 | #define SR_AACK_DIS_ACK 0x2e, 0x10, 4 |
| 244 | #define SR_AACK_SET_PD 0x2e, 0x20, 5 |
| 245 | #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6 |
| 246 | #define RG_CSMA_BE (0x2f) |
| 247 | #define SR_MIN_BE 0x2f, 0x0f, 0 |
| 248 | #define SR_MAX_BE 0x2f, 0xf0, 4 |
| 249 | |
| 250 | #define CMD_REG 0x80 |
| 251 | #define CMD_REG_MASK 0x3f |
| 252 | #define CMD_WRITE 0x40 |
| 253 | #define CMD_FB 0x20 |
| 254 | |
| 255 | #define IRQ_BAT_LOW (1 << 7) |
| 256 | #define IRQ_TRX_UR (1 << 6) |
| 257 | #define IRQ_AMI (1 << 5) |
| 258 | #define IRQ_CCA_ED (1 << 4) |
| 259 | #define IRQ_TRX_END (1 << 3) |
| 260 | #define IRQ_RX_START (1 << 2) |
| 261 | #define IRQ_PLL_UNL (1 << 1) |
| 262 | #define IRQ_PLL_LOCK (1 << 0) |
| 263 | |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 264 | #define IRQ_ACTIVE_HIGH 0 |
| 265 | #define IRQ_ACTIVE_LOW 1 |
| 266 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 267 | #define STATE_P_ON 0x00 /* BUSY */ |
| 268 | #define STATE_BUSY_RX 0x01 |
| 269 | #define STATE_BUSY_TX 0x02 |
| 270 | #define STATE_FORCE_TRX_OFF 0x03 |
| 271 | #define STATE_FORCE_TX_ON 0x04 /* IDLE */ |
| 272 | /* 0x05 */ /* INVALID_PARAMETER */ |
| 273 | #define STATE_RX_ON 0x06 |
| 274 | /* 0x07 */ /* SUCCESS */ |
| 275 | #define STATE_TRX_OFF 0x08 |
| 276 | #define STATE_TX_ON 0x09 |
| 277 | /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */ |
| 278 | #define STATE_SLEEP 0x0F |
Thomas Stilwell | 48d5dba | 2014-03-10 19:29:25 -0500 | [diff] [blame] | 279 | #define STATE_PREP_DEEP_SLEEP 0x10 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 280 | #define STATE_BUSY_RX_AACK 0x11 |
| 281 | #define STATE_BUSY_TX_ARET 0x12 |
stefan@datenfreihafen.org | 028889b | 2013-03-26 12:41:31 +0000 | [diff] [blame] | 282 | #define STATE_RX_AACK_ON 0x16 |
| 283 | #define STATE_TX_ARET_ON 0x19 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 284 | #define STATE_RX_ON_NOCLK 0x1C |
| 285 | #define STATE_RX_AACK_ON_NOCLK 0x1D |
| 286 | #define STATE_BUSY_RX_AACK_NOCLK 0x1E |
| 287 | #define STATE_TRANSITION_IN_PROGRESS 0x1F |
| 288 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 289 | #define AT86RF2XX_NUMREGS 0x3F |
| 290 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 291 | static int |
| 292 | at86rf230_async_state_change(struct at86rf230_local *lp, |
| 293 | struct at86rf230_state_change *ctx, |
| 294 | const u8 state, void (*complete)(void *context)); |
| 295 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 296 | static inline int |
| 297 | __at86rf230_write(struct at86rf230_local *lp, |
| 298 | unsigned int addr, unsigned int data) |
| 299 | { |
| 300 | return regmap_write(lp->regmap, addr, data); |
| 301 | } |
| 302 | |
| 303 | static inline int |
| 304 | __at86rf230_read(struct at86rf230_local *lp, |
| 305 | unsigned int addr, unsigned int *data) |
| 306 | { |
| 307 | return regmap_read(lp->regmap, addr, data); |
| 308 | } |
| 309 | |
| 310 | static inline int |
| 311 | at86rf230_read_subreg(struct at86rf230_local *lp, |
| 312 | unsigned int addr, unsigned int mask, |
| 313 | unsigned int shift, unsigned int *data) |
| 314 | { |
| 315 | int rc; |
| 316 | |
| 317 | rc = __at86rf230_read(lp, addr, data); |
| 318 | if (rc > 0) |
| 319 | *data = (*data & mask) >> shift; |
| 320 | |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | static inline int |
| 325 | at86rf230_write_subreg(struct at86rf230_local *lp, |
| 326 | unsigned int addr, unsigned int mask, |
| 327 | unsigned int shift, unsigned int data) |
| 328 | { |
| 329 | return regmap_update_bits(lp->regmap, addr, mask, data << shift); |
| 330 | } |
| 331 | |
| 332 | static bool |
| 333 | at86rf230_reg_writeable(struct device *dev, unsigned int reg) |
| 334 | { |
| 335 | switch (reg) { |
| 336 | case RG_TRX_STATE: |
| 337 | case RG_TRX_CTRL_0: |
| 338 | case RG_TRX_CTRL_1: |
| 339 | case RG_PHY_TX_PWR: |
| 340 | case RG_PHY_ED_LEVEL: |
| 341 | case RG_PHY_CC_CCA: |
| 342 | case RG_CCA_THRES: |
| 343 | case RG_RX_CTRL: |
| 344 | case RG_SFD_VALUE: |
| 345 | case RG_TRX_CTRL_2: |
| 346 | case RG_ANT_DIV: |
| 347 | case RG_IRQ_MASK: |
| 348 | case RG_VREG_CTRL: |
| 349 | case RG_BATMON: |
| 350 | case RG_XOSC_CTRL: |
| 351 | case RG_RX_SYN: |
| 352 | case RG_XAH_CTRL_1: |
| 353 | case RG_FTN_CTRL: |
| 354 | case RG_PLL_CF: |
| 355 | case RG_PLL_DCU: |
| 356 | case RG_SHORT_ADDR_0: |
| 357 | case RG_SHORT_ADDR_1: |
| 358 | case RG_PAN_ID_0: |
| 359 | case RG_PAN_ID_1: |
| 360 | case RG_IEEE_ADDR_0: |
| 361 | case RG_IEEE_ADDR_1: |
| 362 | case RG_IEEE_ADDR_2: |
| 363 | case RG_IEEE_ADDR_3: |
| 364 | case RG_IEEE_ADDR_4: |
| 365 | case RG_IEEE_ADDR_5: |
| 366 | case RG_IEEE_ADDR_6: |
| 367 | case RG_IEEE_ADDR_7: |
| 368 | case RG_XAH_CTRL_0: |
| 369 | case RG_CSMA_SEED_0: |
| 370 | case RG_CSMA_SEED_1: |
| 371 | case RG_CSMA_BE: |
| 372 | return true; |
| 373 | default: |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | static bool |
| 379 | at86rf230_reg_readable(struct device *dev, unsigned int reg) |
| 380 | { |
| 381 | bool rc; |
| 382 | |
| 383 | /* all writeable are also readable */ |
| 384 | rc = at86rf230_reg_writeable(dev, reg); |
| 385 | if (rc) |
| 386 | return rc; |
| 387 | |
| 388 | /* readonly regs */ |
| 389 | switch (reg) { |
| 390 | case RG_TRX_STATUS: |
| 391 | case RG_PHY_RSSI: |
| 392 | case RG_IRQ_STATUS: |
| 393 | case RG_PART_NUM: |
| 394 | case RG_VERSION_NUM: |
| 395 | case RG_MAN_ID_1: |
| 396 | case RG_MAN_ID_0: |
| 397 | return true; |
| 398 | default: |
| 399 | return false; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static bool |
| 404 | at86rf230_reg_volatile(struct device *dev, unsigned int reg) |
| 405 | { |
| 406 | /* can be changed during runtime */ |
| 407 | switch (reg) { |
| 408 | case RG_TRX_STATUS: |
| 409 | case RG_TRX_STATE: |
| 410 | case RG_PHY_RSSI: |
| 411 | case RG_PHY_ED_LEVEL: |
| 412 | case RG_IRQ_STATUS: |
| 413 | case RG_VREG_CTRL: |
| 414 | return true; |
| 415 | default: |
| 416 | return false; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static bool |
| 421 | at86rf230_reg_precious(struct device *dev, unsigned int reg) |
| 422 | { |
| 423 | /* don't clear irq line on read */ |
| 424 | switch (reg) { |
| 425 | case RG_IRQ_STATUS: |
| 426 | return true; |
| 427 | default: |
| 428 | return false; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static struct regmap_config at86rf230_regmap_spi_config = { |
| 433 | .reg_bits = 8, |
| 434 | .val_bits = 8, |
| 435 | .write_flag_mask = CMD_REG | CMD_WRITE, |
| 436 | .read_flag_mask = CMD_REG, |
| 437 | .cache_type = REGCACHE_RBTREE, |
| 438 | .max_register = AT86RF2XX_NUMREGS, |
| 439 | .writeable_reg = at86rf230_reg_writeable, |
| 440 | .readable_reg = at86rf230_reg_readable, |
| 441 | .volatile_reg = at86rf230_reg_volatile, |
| 442 | .precious_reg = at86rf230_reg_precious, |
| 443 | }; |
| 444 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 445 | static void |
| 446 | at86rf230_async_error_recover(void *context) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 447 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 448 | struct at86rf230_state_change *ctx = context; |
| 449 | struct at86rf230_local *lp = ctx->lp; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 450 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 451 | at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL); |
| 452 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 453 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 454 | static void |
| 455 | at86rf230_async_error(struct at86rf230_local *lp, |
| 456 | struct at86rf230_state_change *ctx, int rc) |
| 457 | { |
| 458 | 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] | 459 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 460 | at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF, |
| 461 | at86rf230_async_error_recover); |
| 462 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 463 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 464 | /* Generic function to get some register value in async mode */ |
| 465 | static int |
| 466 | at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg, |
| 467 | struct at86rf230_state_change *ctx, |
| 468 | void (*complete)(void *context)) |
| 469 | { |
| 470 | u8 *tx_buf = ctx->buf; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 471 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 472 | tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG; |
| 473 | ctx->trx.len = 2; |
| 474 | ctx->msg.complete = complete; |
| 475 | return spi_async(lp->spi, &ctx->msg); |
| 476 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 477 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 478 | static void |
| 479 | at86rf230_async_state_assert(void *context) |
| 480 | { |
| 481 | struct at86rf230_state_change *ctx = context; |
| 482 | struct at86rf230_local *lp = ctx->lp; |
| 483 | const u8 *buf = ctx->buf; |
| 484 | const u8 trx_state = buf[1] & 0x1f; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 485 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 486 | /* Assert state change */ |
| 487 | if (trx_state != ctx->to_state) { |
| 488 | /* Special handling if transceiver state is in |
| 489 | * STATE_BUSY_RX_AACK and a SHR was detected. |
| 490 | */ |
| 491 | if (trx_state == STATE_BUSY_RX_AACK) { |
| 492 | /* Undocumented race condition. If we send a state |
| 493 | * change to STATE_RX_AACK_ON the transceiver could |
| 494 | * change his state automatically to STATE_BUSY_RX_AACK |
| 495 | * if a SHR was detected. This is not an error, but we |
| 496 | * can't assert this. |
| 497 | */ |
| 498 | if (ctx->to_state == STATE_RX_AACK_ON) |
| 499 | goto done; |
| 500 | |
| 501 | /* If we change to STATE_TX_ON without forcing and |
| 502 | * transceiver state is STATE_BUSY_RX_AACK, we wait |
| 503 | * 'tFrame + tPAck' receiving time. In this time the |
| 504 | * PDU should be received. If the transceiver is still |
| 505 | * in STATE_BUSY_RX_AACK, we run a force state change |
| 506 | * to STATE_TX_ON. This is a timeout handling, if the |
| 507 | * transceiver stucks in STATE_BUSY_RX_AACK. |
| 508 | */ |
| 509 | if (ctx->to_state == STATE_TX_ON) { |
| 510 | at86rf230_async_state_change(lp, ctx, |
| 511 | STATE_FORCE_TX_ON, |
| 512 | ctx->complete); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | |
| 518 | dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n", |
| 519 | ctx->from_state, ctx->to_state, trx_state); |
| 520 | } |
| 521 | |
| 522 | done: |
| 523 | if (ctx->complete) |
| 524 | ctx->complete(context); |
| 525 | } |
| 526 | |
| 527 | /* Do state change timing delay. */ |
| 528 | static void |
| 529 | at86rf230_async_state_delay(void *context) |
| 530 | { |
| 531 | struct at86rf230_state_change *ctx = context; |
| 532 | struct at86rf230_local *lp = ctx->lp; |
| 533 | struct at86rf2xx_chip_data *c = lp->data; |
| 534 | bool force = false; |
| 535 | int rc; |
| 536 | |
| 537 | /* The force state changes are will show as normal states in the |
| 538 | * state status subregister. We change the to_state to the |
| 539 | * corresponding one and remember if it was a force change, this |
| 540 | * differs if we do a state change from STATE_BUSY_RX_AACK. |
| 541 | */ |
| 542 | switch (ctx->to_state) { |
| 543 | case STATE_FORCE_TX_ON: |
| 544 | ctx->to_state = STATE_TX_ON; |
| 545 | force = true; |
| 546 | break; |
| 547 | case STATE_FORCE_TRX_OFF: |
| 548 | ctx->to_state = STATE_TRX_OFF; |
| 549 | force = true; |
| 550 | break; |
| 551 | default: |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | switch (ctx->from_state) { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 556 | case STATE_TRX_OFF: |
| 557 | switch (ctx->to_state) { |
| 558 | case STATE_RX_AACK_ON: |
| 559 | usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10); |
| 560 | goto change; |
| 561 | case STATE_TX_ON: |
| 562 | usleep_range(c->t_off_to_tx_on, |
| 563 | c->t_off_to_tx_on + 10); |
| 564 | goto change; |
| 565 | default: |
| 566 | break; |
| 567 | } |
| 568 | break; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 569 | case STATE_BUSY_RX_AACK: |
| 570 | switch (ctx->to_state) { |
| 571 | case STATE_TX_ON: |
| 572 | /* Wait for worst case receiving time if we |
| 573 | * didn't make a force change from BUSY_RX_AACK |
| 574 | * to TX_ON. |
| 575 | */ |
| 576 | if (!force) { |
| 577 | usleep_range(c->t_frame + c->t_p_ack, |
| 578 | c->t_frame + c->t_p_ack + 1000); |
| 579 | goto change; |
| 580 | } |
| 581 | break; |
| 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | break; |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame^] | 586 | /* Default value, means RESET state */ |
| 587 | case STATE_P_ON: |
| 588 | switch (ctx->to_state) { |
| 589 | case STATE_TRX_OFF: |
| 590 | usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10); |
| 591 | goto change; |
| 592 | default: |
| 593 | break; |
| 594 | } |
| 595 | break; |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 596 | default: |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | /* Default delay is 1us in the most cases */ |
| 601 | udelay(1); |
| 602 | |
| 603 | change: |
| 604 | rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 605 | at86rf230_async_state_assert); |
| 606 | if (rc) |
| 607 | dev_err(&lp->spi->dev, "spi_async error %d\n", rc); |
| 608 | } |
| 609 | |
| 610 | static void |
| 611 | at86rf230_async_state_change_start(void *context) |
| 612 | { |
| 613 | struct at86rf230_state_change *ctx = context; |
| 614 | struct at86rf230_local *lp = ctx->lp; |
| 615 | u8 *buf = ctx->buf; |
| 616 | const u8 trx_state = buf[1] & 0x1f; |
| 617 | int rc; |
| 618 | |
| 619 | /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */ |
| 620 | if (trx_state == STATE_TRANSITION_IN_PROGRESS) { |
| 621 | udelay(1); |
| 622 | rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 623 | at86rf230_async_state_change_start); |
| 624 | if (rc) |
| 625 | dev_err(&lp->spi->dev, "spi_async error %d\n", rc); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | /* Check if we already are in the state which we change in */ |
| 630 | if (trx_state == ctx->to_state) { |
| 631 | if (ctx->complete) |
| 632 | ctx->complete(context); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | /* Set current state to the context of state change */ |
| 637 | ctx->from_state = trx_state; |
| 638 | |
| 639 | /* Going into the next step for a state change which do a timing |
| 640 | * relevant delay. |
| 641 | */ |
| 642 | buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE; |
| 643 | buf[1] = ctx->to_state; |
| 644 | ctx->trx.len = 2; |
| 645 | ctx->msg.complete = at86rf230_async_state_delay; |
| 646 | rc = spi_async(lp->spi, &ctx->msg); |
| 647 | if (rc) |
| 648 | 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] | 649 | } |
| 650 | |
| 651 | static int |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 652 | at86rf230_async_state_change(struct at86rf230_local *lp, |
| 653 | struct at86rf230_state_change *ctx, |
| 654 | const u8 state, void (*complete)(void *context)) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 655 | { |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 656 | /* Initialization for the state change context */ |
| 657 | ctx->to_state = state; |
| 658 | ctx->complete = complete; |
| 659 | return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx, |
| 660 | at86rf230_async_state_change_start); |
| 661 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 662 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 663 | static void |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 664 | at86rf230_sync_state_change_complete(void *context) |
| 665 | { |
| 666 | struct at86rf230_state_change *ctx = context; |
| 667 | struct at86rf230_local *lp = ctx->lp; |
| 668 | |
| 669 | complete(&lp->state_complete); |
| 670 | } |
| 671 | |
| 672 | /* This function do a sync framework above the async state change. |
| 673 | * Some callbacks of the IEEE 802.15.4 driver interface need to be |
| 674 | * handled synchronously. |
| 675 | */ |
| 676 | static int |
| 677 | at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state) |
| 678 | { |
| 679 | int rc; |
| 680 | |
| 681 | rc = at86rf230_async_state_change(lp, &lp->state, state, |
| 682 | at86rf230_sync_state_change_complete); |
| 683 | if (rc) { |
| 684 | at86rf230_async_error(lp, &lp->state, rc); |
| 685 | return rc; |
| 686 | } |
| 687 | |
| 688 | rc = wait_for_completion_timeout(&lp->state_complete, |
| 689 | msecs_to_jiffies(100)); |
| 690 | if (!rc) |
| 691 | return -ETIMEDOUT; |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static void |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 697 | at86rf230_tx_complete(void *context) |
| 698 | { |
| 699 | struct at86rf230_state_change *ctx = context; |
| 700 | struct at86rf230_local *lp = ctx->lp; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 701 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 702 | complete(&lp->tx_complete); |
| 703 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 704 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 705 | static void |
| 706 | at86rf230_tx_on(void *context) |
| 707 | { |
| 708 | struct at86rf230_state_change *ctx = context; |
| 709 | struct at86rf230_local *lp = ctx->lp; |
| 710 | int rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 711 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 712 | rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON, |
| 713 | at86rf230_tx_complete); |
| 714 | if (rc) |
| 715 | at86rf230_async_error(lp, ctx, rc); |
| 716 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 717 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 718 | static void |
| 719 | at86rf230_tx_trac_error(void *context) |
| 720 | { |
| 721 | struct at86rf230_state_change *ctx = context; |
| 722 | struct at86rf230_local *lp = ctx->lp; |
| 723 | int rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 724 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 725 | rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON, |
| 726 | at86rf230_tx_on); |
| 727 | if (rc) |
| 728 | at86rf230_async_error(lp, ctx, rc); |
| 729 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 730 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 731 | static void |
| 732 | at86rf230_tx_trac_check(void *context) |
| 733 | { |
| 734 | struct at86rf230_state_change *ctx = context; |
| 735 | struct at86rf230_local *lp = ctx->lp; |
| 736 | const u8 *buf = ctx->buf; |
| 737 | const u8 trac = (buf[1] & 0xe0) >> 5; |
| 738 | int rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 739 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 740 | /* If trac status is different than zero we need to do a state change |
| 741 | * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver |
| 742 | * state to TX_ON. |
| 743 | */ |
| 744 | if (trac) { |
| 745 | rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF, |
| 746 | at86rf230_tx_trac_error); |
| 747 | if (rc) |
| 748 | at86rf230_async_error(lp, ctx, rc); |
| 749 | return; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 750 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 751 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 752 | at86rf230_tx_on(context); |
| 753 | } |
| 754 | |
| 755 | |
| 756 | static void |
| 757 | at86rf230_tx_trac_status(void *context) |
| 758 | { |
| 759 | struct at86rf230_state_change *ctx = context; |
| 760 | struct at86rf230_local *lp = ctx->lp; |
| 761 | int rc; |
| 762 | |
| 763 | rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx, |
| 764 | at86rf230_tx_trac_check); |
| 765 | if (rc) |
| 766 | at86rf230_async_error(lp, ctx, rc); |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | at86rf230_rx(struct at86rf230_local *lp, |
| 771 | const u8 *data, u8 len) |
| 772 | { |
| 773 | u8 lqi; |
| 774 | struct sk_buff *skb; |
| 775 | u8 rx_local_buf[AT86RF2XX_MAX_BUF]; |
| 776 | |
| 777 | if (len < 2) |
| 778 | return; |
| 779 | |
| 780 | /* read full frame buffer and invalid lqi value to lowest |
| 781 | * indicator if frame was is in a corrupted state. |
| 782 | */ |
| 783 | if (len > IEEE802154_MTU) { |
| 784 | lqi = 0; |
| 785 | len = IEEE802154_MTU; |
| 786 | dev_vdbg(&lp->spi->dev, "corrupted frame received\n"); |
| 787 | } else { |
| 788 | lqi = data[len]; |
| 789 | } |
| 790 | |
| 791 | memcpy(rx_local_buf, data, len); |
| 792 | enable_irq(lp->spi->irq); |
| 793 | |
| 794 | skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC); |
| 795 | if (!skb) { |
| 796 | dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n"); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | memcpy(skb_put(skb, len), rx_local_buf, len); |
| 801 | |
| 802 | /* We do not put CRC into the frame */ |
| 803 | skb_trim(skb, len - 2); |
| 804 | |
| 805 | ieee802154_rx_irqsafe(lp->dev, skb, lqi); |
| 806 | } |
| 807 | |
| 808 | static void |
| 809 | at86rf230_rx_read_frame_complete(void *context) |
| 810 | { |
| 811 | struct at86rf230_state_change *ctx = context; |
| 812 | struct at86rf230_local *lp = ctx->lp; |
| 813 | const u8 *buf = lp->irq.buf; |
| 814 | const u8 len = buf[1]; |
| 815 | |
| 816 | at86rf230_rx(lp, buf + 2, len); |
| 817 | } |
| 818 | |
| 819 | static int |
| 820 | at86rf230_rx_read_frame(struct at86rf230_local *lp) |
| 821 | { |
| 822 | u8 *buf = lp->irq.buf; |
| 823 | |
| 824 | buf[0] = CMD_FB; |
| 825 | lp->irq.trx.len = AT86RF2XX_MAX_BUF; |
| 826 | lp->irq.msg.complete = at86rf230_rx_read_frame_complete; |
| 827 | return spi_async(lp->spi, &lp->irq.msg); |
| 828 | } |
| 829 | |
| 830 | static void |
| 831 | at86rf230_rx_trac_check(void *context) |
| 832 | { |
| 833 | struct at86rf230_state_change *ctx = context; |
| 834 | struct at86rf230_local *lp = ctx->lp; |
| 835 | int rc; |
| 836 | |
| 837 | /* Possible check on trac status here. This could be useful to make |
| 838 | * some stats why receive is failed. Not used at the moment, but it's |
| 839 | * maybe timing relevant. Datasheet doesn't say anything about this. |
| 840 | * The programming guide say do it so. |
| 841 | */ |
| 842 | |
| 843 | rc = at86rf230_rx_read_frame(lp); |
| 844 | if (rc) { |
| 845 | enable_irq(lp->spi->irq); |
| 846 | at86rf230_async_error(lp, ctx, rc); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | static int |
| 851 | at86rf230_irq_trx_end(struct at86rf230_local *lp) |
| 852 | { |
| 853 | spin_lock(&lp->lock); |
| 854 | if (lp->is_tx) { |
| 855 | lp->is_tx = 0; |
| 856 | spin_unlock(&lp->lock); |
| 857 | enable_irq(lp->spi->irq); |
| 858 | |
| 859 | if (lp->tx_aret) |
| 860 | return at86rf230_async_state_change(lp, &lp->irq, |
| 861 | STATE_FORCE_TX_ON, |
| 862 | at86rf230_tx_trac_status); |
| 863 | else |
| 864 | return at86rf230_async_state_change(lp, &lp->irq, |
| 865 | STATE_RX_AACK_ON, |
| 866 | at86rf230_tx_complete); |
| 867 | } else { |
| 868 | spin_unlock(&lp->lock); |
| 869 | return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq, |
| 870 | at86rf230_rx_trac_check); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | static void |
| 875 | at86rf230_irq_status(void *context) |
| 876 | { |
| 877 | struct at86rf230_state_change *ctx = context; |
| 878 | struct at86rf230_local *lp = ctx->lp; |
| 879 | const u8 *buf = lp->irq.buf; |
| 880 | const u8 irq = buf[1]; |
| 881 | int rc; |
| 882 | |
| 883 | if (irq & IRQ_TRX_END) { |
| 884 | rc = at86rf230_irq_trx_end(lp); |
| 885 | if (rc) |
| 886 | at86rf230_async_error(lp, ctx, rc); |
| 887 | } else { |
| 888 | enable_irq(lp->spi->irq); |
| 889 | dev_err(&lp->spi->dev, "not supported irq %02x received\n", |
| 890 | irq); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | static irqreturn_t at86rf230_isr(int irq, void *data) |
| 895 | { |
| 896 | struct at86rf230_local *lp = data; |
| 897 | struct at86rf230_state_change *ctx = &lp->irq; |
| 898 | u8 *buf = ctx->buf; |
| 899 | int rc; |
| 900 | |
| 901 | disable_irq_nosync(lp->spi->irq); |
| 902 | |
| 903 | buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG; |
| 904 | ctx->trx.len = 2; |
| 905 | ctx->msg.complete = at86rf230_irq_status; |
| 906 | rc = spi_async(lp->spi, &ctx->msg); |
| 907 | if (rc) { |
| 908 | at86rf230_async_error(lp, ctx, rc); |
| 909 | return IRQ_NONE; |
| 910 | } |
| 911 | |
| 912 | return IRQ_HANDLED; |
| 913 | } |
| 914 | |
| 915 | static void |
| 916 | at86rf230_write_frame_complete(void *context) |
| 917 | { |
| 918 | struct at86rf230_state_change *ctx = context; |
| 919 | struct at86rf230_local *lp = ctx->lp; |
| 920 | u8 *buf = ctx->buf; |
| 921 | int rc; |
| 922 | |
| 923 | buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE; |
| 924 | buf[1] = STATE_BUSY_TX; |
| 925 | ctx->trx.len = 2; |
| 926 | ctx->msg.complete = NULL; |
| 927 | rc = spi_async(lp->spi, &ctx->msg); |
| 928 | if (rc) |
| 929 | at86rf230_async_error(lp, ctx, rc); |
| 930 | } |
| 931 | |
| 932 | static void |
| 933 | at86rf230_write_frame(void *context) |
| 934 | { |
| 935 | struct at86rf230_state_change *ctx = context; |
| 936 | struct at86rf230_local *lp = ctx->lp; |
| 937 | struct sk_buff *skb = lp->tx_skb; |
| 938 | u8 *buf = lp->tx.buf; |
| 939 | int rc; |
| 940 | |
| 941 | spin_lock(&lp->lock); |
| 942 | lp->is_tx = 1; |
| 943 | spin_unlock(&lp->lock); |
| 944 | |
| 945 | buf[0] = CMD_FB | CMD_WRITE; |
| 946 | buf[1] = skb->len + 2; |
| 947 | memcpy(buf + 2, skb->data, skb->len); |
| 948 | lp->tx.trx.len = skb->len + 2; |
| 949 | lp->tx.msg.complete = at86rf230_write_frame_complete; |
| 950 | rc = spi_async(lp->spi, &lp->tx.msg); |
| 951 | if (rc) |
| 952 | at86rf230_async_error(lp, ctx, rc); |
| 953 | } |
| 954 | |
| 955 | static void |
| 956 | at86rf230_xmit_tx_on(void *context) |
| 957 | { |
| 958 | struct at86rf230_state_change *ctx = context; |
| 959 | struct at86rf230_local *lp = ctx->lp; |
| 960 | int rc; |
| 961 | |
| 962 | rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON, |
| 963 | at86rf230_write_frame); |
| 964 | if (rc) |
| 965 | at86rf230_async_error(lp, ctx, rc); |
| 966 | } |
| 967 | |
| 968 | static int |
| 969 | at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb) |
| 970 | { |
| 971 | struct at86rf230_local *lp = dev->priv; |
| 972 | struct at86rf230_state_change *ctx = &lp->tx; |
| 973 | |
| 974 | void (*tx_complete)(void *context) = at86rf230_write_frame; |
| 975 | int rc; |
| 976 | |
| 977 | lp->tx_skb = skb; |
| 978 | |
| 979 | /* In ARET mode we need to go into STATE_TX_ARET_ON after we |
| 980 | * are in STATE_TX_ON. The pfad differs here, so we change |
| 981 | * the complete handler. |
| 982 | */ |
| 983 | if (lp->tx_aret) |
| 984 | tx_complete = at86rf230_xmit_tx_on; |
| 985 | |
| 986 | rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON, |
| 987 | tx_complete); |
| 988 | if (rc) { |
| 989 | at86rf230_async_error(lp, ctx, rc); |
| 990 | return rc; |
| 991 | } |
| 992 | rc = wait_for_completion_interruptible_timeout(&lp->tx_complete, |
| 993 | msecs_to_jiffies(lp->data->t_tx_timeout)); |
| 994 | if (!rc) { |
| 995 | at86rf230_async_error(lp, ctx, rc); |
| 996 | return -ETIMEDOUT; |
| 997 | } |
| 998 | |
| 999 | /* Interfame spacing time, which is phy depend. |
| 1000 | * TODO |
| 1001 | * Move this handling in MAC 802.15.4 layer. |
| 1002 | * This is currently a workaround to avoid fragmenation issues. |
| 1003 | */ |
| 1004 | if (skb->len > 18) |
| 1005 | usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10); |
| 1006 | else |
| 1007 | usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10); |
| 1008 | |
| 1009 | return 0; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | static int |
| 1013 | at86rf230_ed(struct ieee802154_dev *dev, u8 *level) |
| 1014 | { |
| 1015 | might_sleep(); |
| 1016 | BUG_ON(!level); |
| 1017 | *level = 0xbe; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1022 | at86rf230_start(struct ieee802154_dev *dev) |
| 1023 | { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1024 | return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static void |
| 1028 | at86rf230_stop(struct ieee802154_dev *dev) |
| 1029 | { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1030 | at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static int |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1034 | at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1035 | { |
| 1036 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 1037 | } |
| 1038 | |
| 1039 | static int |
| 1040 | at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) |
| 1041 | { |
| 1042 | int rc; |
| 1043 | |
| 1044 | if (channel == 0) |
| 1045 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0); |
| 1046 | else |
| 1047 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1); |
| 1048 | if (rc < 0) |
| 1049 | return rc; |
| 1050 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1051 | if (page == 0) { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1052 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1053 | lp->data->rssi_base_val = -100; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1054 | } else { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1055 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1); |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1056 | lp->data->rssi_base_val = -98; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1057 | } |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1058 | if (rc < 0) |
| 1059 | return rc; |
| 1060 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1061 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 1062 | } |
| 1063 | |
| 1064 | static int |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1065 | at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) |
| 1066 | { |
| 1067 | struct at86rf230_local *lp = dev->priv; |
| 1068 | int rc; |
| 1069 | |
| 1070 | might_sleep(); |
| 1071 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1072 | if (page < 0 || page > 31 || |
| 1073 | !(lp->dev->phy->channels_supported[page] & BIT(channel))) { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1074 | WARN_ON(1); |
| 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1078 | rc = lp->data->set_channel(lp, page, channel); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1079 | if (rc < 0) |
| 1080 | return rc; |
| 1081 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1082 | msleep(1); /* Wait for PLL */ |
| 1083 | dev->phy->current_channel = channel; |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1084 | dev->phy->current_page = page; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1090 | at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev, |
| 1091 | struct ieee802154_hw_addr_filt *filt, |
| 1092 | unsigned long changed) |
| 1093 | { |
| 1094 | struct at86rf230_local *lp = dev->priv; |
| 1095 | |
| 1096 | if (changed & IEEE802515_AFILT_SADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1097 | u16 addr = le16_to_cpu(filt->short_addr); |
| 1098 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1099 | dev_vdbg(&lp->spi->dev, |
| 1100 | "at86rf230_set_hw_addr_filt called for saddr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1101 | __at86rf230_write(lp, RG_SHORT_ADDR_0, addr); |
| 1102 | __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | if (changed & IEEE802515_AFILT_PANID_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1106 | u16 pan = le16_to_cpu(filt->pan_id); |
| 1107 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1108 | dev_vdbg(&lp->spi->dev, |
| 1109 | "at86rf230_set_hw_addr_filt called for pan id\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1110 | __at86rf230_write(lp, RG_PAN_ID_0, pan); |
| 1111 | __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1115 | u8 i, addr[8]; |
| 1116 | |
| 1117 | memcpy(addr, &filt->ieee_addr, 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1118 | dev_vdbg(&lp->spi->dev, |
| 1119 | "at86rf230_set_hw_addr_filt called for IEEE addr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 1120 | for (i = 0; i < 8; i++) |
| 1121 | __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | if (changed & IEEE802515_AFILT_PANC_CHANGED) { |
| 1125 | dev_vdbg(&lp->spi->dev, |
| 1126 | "at86rf230_set_hw_addr_filt called for panc change\n"); |
| 1127 | if (filt->pan_coord) |
| 1128 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1); |
| 1129 | else |
| 1130 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0); |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1136 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1137 | at86rf230_set_txpower(struct ieee802154_dev *dev, int db) |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1138 | { |
| 1139 | struct at86rf230_local *lp = dev->priv; |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1140 | |
| 1141 | /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five |
| 1142 | * bits decrease power in 1dB steps. 0x60 represents extra PA gain of |
| 1143 | * 0dB. |
| 1144 | * thus, supported values for db range from -26 to 5, for 31dB of |
| 1145 | * reduction to 0dB of reduction. |
| 1146 | */ |
| 1147 | if (db > 5 || db < -26) |
| 1148 | return -EINVAL; |
| 1149 | |
| 1150 | db = -(db - 5); |
| 1151 | |
Jean Sacren | 677676c | 2014-03-01 15:54:36 -0700 | [diff] [blame] | 1152 | return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db); |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 1153 | } |
| 1154 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 1155 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1156 | at86rf230_set_lbt(struct ieee802154_dev *dev, bool on) |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 1157 | { |
| 1158 | struct at86rf230_local *lp = dev->priv; |
| 1159 | |
| 1160 | return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on); |
| 1161 | } |
| 1162 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1163 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1164 | at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode) |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 1165 | { |
| 1166 | struct at86rf230_local *lp = dev->priv; |
| 1167 | |
| 1168 | return at86rf230_write_subreg(lp, SR_CCA_MODE, mode); |
| 1169 | } |
| 1170 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1171 | static int |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1172 | at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level) |
| 1173 | { |
| 1174 | return (level - lp->data->rssi_base_val) * 100 / 207; |
| 1175 | } |
| 1176 | |
| 1177 | static int |
| 1178 | at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level) |
| 1179 | { |
| 1180 | return (level - lp->data->rssi_base_val) / 2; |
| 1181 | } |
| 1182 | |
| 1183 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1184 | at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level) |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1185 | { |
| 1186 | struct at86rf230_local *lp = dev->priv; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1187 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1188 | if (level < lp->data->rssi_base_val || level > 30) |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1189 | return -EINVAL; |
| 1190 | |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1191 | return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, |
| 1192 | lp->data->get_desense_steps(lp, level)); |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 1193 | } |
| 1194 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1195 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1196 | at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be, |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1197 | u8 retries) |
| 1198 | { |
| 1199 | struct at86rf230_local *lp = dev->priv; |
| 1200 | int rc; |
| 1201 | |
| 1202 | if (min_be > max_be || max_be > 8 || retries > 5) |
| 1203 | return -EINVAL; |
| 1204 | |
| 1205 | rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be); |
| 1206 | if (rc) |
| 1207 | return rc; |
| 1208 | |
| 1209 | rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be); |
| 1210 | if (rc) |
| 1211 | return rc; |
| 1212 | |
Alexander Aring | 39d7f32 | 2014-04-05 13:49:26 +0200 | [diff] [blame] | 1213 | return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries); |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1217 | at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries) |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1218 | { |
| 1219 | struct at86rf230_local *lp = dev->priv; |
| 1220 | int rc = 0; |
| 1221 | |
| 1222 | if (retries < -1 || retries > 15) |
| 1223 | return -EINVAL; |
| 1224 | |
| 1225 | lp->tx_aret = retries >= 0; |
| 1226 | |
| 1227 | if (retries >= 0) |
| 1228 | rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries); |
| 1229 | |
| 1230 | return rc; |
| 1231 | } |
| 1232 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1233 | static struct ieee802154_ops at86rf230_ops = { |
| 1234 | .owner = THIS_MODULE, |
| 1235 | .xmit = at86rf230_xmit, |
| 1236 | .ed = at86rf230_ed, |
| 1237 | .set_channel = at86rf230_channel, |
| 1238 | .start = at86rf230_start, |
| 1239 | .stop = at86rf230_stop, |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 1240 | .set_hw_addr_filt = at86rf230_set_hw_addr_filt, |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1241 | .set_txpower = at86rf230_set_txpower, |
| 1242 | .set_lbt = at86rf230_set_lbt, |
| 1243 | .set_cca_mode = at86rf230_set_cca_mode, |
| 1244 | .set_cca_ed_level = at86rf230_set_cca_ed_level, |
| 1245 | .set_csma_params = at86rf230_set_csma_params, |
| 1246 | .set_frame_retries = at86rf230_set_frame_retries, |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1247 | }; |
| 1248 | |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1249 | static struct at86rf2xx_chip_data at86rf233_data = { |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame^] | 1250 | .t_reset_to_off = 26, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1251 | .t_off_to_aack = 80, |
| 1252 | .t_off_to_tx_on = 80, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1253 | .t_frame = 4096, |
| 1254 | .t_p_ack = 545, |
| 1255 | .t_sifs = 192, |
| 1256 | .t_lifs = 480, |
| 1257 | .t_tx_timeout = 2000, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1258 | .rssi_base_val = -91, |
| 1259 | .set_channel = at86rf23x_set_channel, |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1260 | .get_desense_steps = at86rf23x_get_desens_steps |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1261 | }; |
| 1262 | |
| 1263 | static struct at86rf2xx_chip_data at86rf231_data = { |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame^] | 1264 | .t_reset_to_off = 37, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1265 | .t_off_to_aack = 110, |
| 1266 | .t_off_to_tx_on = 110, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1267 | .t_frame = 4096, |
| 1268 | .t_p_ack = 545, |
| 1269 | .t_sifs = 192, |
| 1270 | .t_lifs = 480, |
| 1271 | .t_tx_timeout = 2000, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1272 | .rssi_base_val = -91, |
| 1273 | .set_channel = at86rf23x_set_channel, |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1274 | .get_desense_steps = at86rf23x_get_desens_steps |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1275 | }; |
| 1276 | |
| 1277 | static struct at86rf2xx_chip_data at86rf212_data = { |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame^] | 1278 | .t_reset_to_off = 26, |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1279 | .t_off_to_aack = 200, |
| 1280 | .t_off_to_tx_on = 200, |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1281 | .t_frame = 4096, |
| 1282 | .t_p_ack = 545, |
| 1283 | .t_sifs = 192, |
| 1284 | .t_lifs = 480, |
| 1285 | .t_tx_timeout = 2000, |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1286 | .rssi_base_val = -100, |
| 1287 | .set_channel = at86rf212_set_channel, |
Alexander Aring | a7d7eda | 2014-07-03 00:20:47 +0200 | [diff] [blame] | 1288 | .get_desense_steps = at86rf212_get_desens_steps |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1289 | }; |
| 1290 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1291 | static int at86rf230_hw_init(struct at86rf230_local *lp) |
| 1292 | { |
Alexander Aring | 1db0558 | 2014-07-03 00:20:50 +0200 | [diff] [blame] | 1293 | int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1294 | unsigned int dvdd; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1295 | u8 csma_seed[2]; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1296 | |
Alexander Aring | 09e536c | 2014-07-03 00:20:52 +0200 | [diff] [blame^] | 1297 | rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF); |
Phoebe Buckheister | 7dcbd22 | 2014-02-17 11:34:13 +0100 | [diff] [blame] | 1298 | if (rc) |
| 1299 | return rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1300 | |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1301 | irq_type = irq_get_trigger_type(lp->spi->irq); |
Alexander Aring | 1db0558 | 2014-07-03 00:20:50 +0200 | [diff] [blame] | 1302 | if (irq_type == IRQ_TYPE_EDGE_FALLING) |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1303 | irq_pol = IRQ_ACTIVE_LOW; |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1304 | |
Alexander Aring | 18c6504 | 2014-04-24 19:09:18 +0200 | [diff] [blame] | 1305 | rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1306 | if (rc) |
| 1307 | return rc; |
| 1308 | |
Alexander Aring | 6bd2b13 | 2014-07-03 00:20:49 +0200 | [diff] [blame] | 1309 | rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1); |
| 1310 | if (rc) |
| 1311 | return rc; |
| 1312 | |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1313 | 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] | 1314 | if (rc) |
| 1315 | return rc; |
| 1316 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1317 | get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed)); |
| 1318 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]); |
| 1319 | if (rc) |
| 1320 | return rc; |
| 1321 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]); |
| 1322 | if (rc) |
| 1323 | return rc; |
| 1324 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1325 | /* CLKM changes are applied immediately */ |
| 1326 | rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00); |
| 1327 | if (rc) |
| 1328 | return rc; |
| 1329 | |
| 1330 | /* Turn CLKM Off */ |
| 1331 | rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00); |
| 1332 | if (rc) |
| 1333 | return rc; |
| 1334 | /* Wait the next SLEEP cycle */ |
| 1335 | msleep(100); |
| 1336 | |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1337 | rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1338 | if (rc) |
| 1339 | return rc; |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1340 | if (!dvdd) { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1341 | dev_err(&lp->spi->dev, "DVDD error\n"); |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1345 | return 0; |
| 1346 | } |
| 1347 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1348 | static struct at86rf230_platform_data * |
| 1349 | at86rf230_get_pdata(struct spi_device *spi) |
| 1350 | { |
| 1351 | struct at86rf230_platform_data *pdata; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1352 | |
| 1353 | if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) |
| 1354 | return spi->dev.platform_data; |
| 1355 | |
| 1356 | pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL); |
| 1357 | if (!pdata) |
| 1358 | goto done; |
| 1359 | |
| 1360 | pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0); |
| 1361 | pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0); |
| 1362 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1363 | spi->dev.platform_data = pdata; |
| 1364 | done: |
| 1365 | return pdata; |
| 1366 | } |
| 1367 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1368 | static int |
| 1369 | at86rf230_detect_device(struct at86rf230_local *lp) |
| 1370 | { |
| 1371 | unsigned int part, version, val; |
| 1372 | u16 man_id = 0; |
| 1373 | const char *chip; |
| 1374 | int rc; |
| 1375 | |
| 1376 | rc = __at86rf230_read(lp, RG_MAN_ID_0, &val); |
| 1377 | if (rc) |
| 1378 | return rc; |
| 1379 | man_id |= val; |
| 1380 | |
| 1381 | rc = __at86rf230_read(lp, RG_MAN_ID_1, &val); |
| 1382 | if (rc) |
| 1383 | return rc; |
| 1384 | man_id |= (val << 8); |
| 1385 | |
| 1386 | rc = __at86rf230_read(lp, RG_PART_NUM, &part); |
| 1387 | if (rc) |
| 1388 | return rc; |
| 1389 | |
| 1390 | rc = __at86rf230_read(lp, RG_PART_NUM, &version); |
| 1391 | if (rc) |
| 1392 | return rc; |
| 1393 | |
| 1394 | if (man_id != 0x001f) { |
| 1395 | dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n", |
| 1396 | man_id >> 8, man_id & 0xFF); |
| 1397 | return -EINVAL; |
| 1398 | } |
| 1399 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1400 | lp->dev->extra_tx_headroom = 0; |
| 1401 | lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK | |
| 1402 | IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA; |
| 1403 | |
| 1404 | switch (part) { |
| 1405 | case 2: |
| 1406 | chip = "at86rf230"; |
| 1407 | rc = -ENOTSUPP; |
| 1408 | break; |
| 1409 | case 3: |
| 1410 | chip = "at86rf231"; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1411 | lp->data = &at86rf231_data; |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1412 | lp->dev->phy->channels_supported[0] = 0x7FFF800; |
| 1413 | break; |
| 1414 | case 7: |
| 1415 | chip = "at86rf212"; |
| 1416 | if (version == 1) { |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1417 | lp->data = &at86rf212_data; |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1418 | lp->dev->flags |= IEEE802154_HW_LBT; |
| 1419 | lp->dev->phy->channels_supported[0] = 0x00007FF; |
| 1420 | lp->dev->phy->channels_supported[2] = 0x00007FF; |
| 1421 | } else { |
| 1422 | rc = -ENOTSUPP; |
| 1423 | } |
| 1424 | break; |
| 1425 | case 11: |
| 1426 | chip = "at86rf233"; |
Alexander Aring | a53d1f7 | 2014-07-03 00:20:46 +0200 | [diff] [blame] | 1427 | lp->data = &at86rf233_data; |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1428 | lp->dev->phy->channels_supported[0] = 0x7FFF800; |
| 1429 | break; |
| 1430 | default: |
| 1431 | chip = "unkown"; |
| 1432 | rc = -ENOTSUPP; |
| 1433 | break; |
| 1434 | } |
| 1435 | |
| 1436 | dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version); |
| 1437 | |
| 1438 | return rc; |
| 1439 | } |
| 1440 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1441 | static void |
| 1442 | at86rf230_setup_spi_messages(struct at86rf230_local *lp) |
| 1443 | { |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1444 | lp->state.lp = lp; |
| 1445 | spi_message_init(&lp->state.msg); |
| 1446 | lp->state.msg.context = &lp->state; |
| 1447 | lp->state.trx.tx_buf = lp->state.buf; |
| 1448 | lp->state.trx.rx_buf = lp->state.buf; |
| 1449 | spi_message_add_tail(&lp->state.trx, &lp->state.msg); |
| 1450 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1451 | lp->irq.lp = lp; |
| 1452 | spi_message_init(&lp->irq.msg); |
| 1453 | lp->irq.msg.context = &lp->irq; |
| 1454 | lp->irq.trx.tx_buf = lp->irq.buf; |
| 1455 | lp->irq.trx.rx_buf = lp->irq.buf; |
| 1456 | spi_message_add_tail(&lp->irq.trx, &lp->irq.msg); |
| 1457 | |
| 1458 | lp->tx.lp = lp; |
| 1459 | spi_message_init(&lp->tx.msg); |
| 1460 | lp->tx.msg.context = &lp->tx; |
| 1461 | lp->tx.trx.tx_buf = lp->tx.buf; |
| 1462 | lp->tx.trx.rx_buf = lp->tx.buf; |
| 1463 | spi_message_add_tail(&lp->tx.trx, &lp->tx.msg); |
| 1464 | } |
| 1465 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1466 | static int at86rf230_probe(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1467 | { |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1468 | struct at86rf230_platform_data *pdata; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1469 | struct ieee802154_dev *dev; |
| 1470 | struct at86rf230_local *lp; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1471 | unsigned int status; |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1472 | int rc, irq_type; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (!spi->irq) { |
| 1475 | dev_err(&spi->dev, "no IRQ specified\n"); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1479 | pdata = at86rf230_get_pdata(spi); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1480 | if (!pdata) { |
| 1481 | dev_err(&spi->dev, "no platform_data\n"); |
| 1482 | return -EINVAL; |
| 1483 | } |
| 1484 | |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1485 | if (gpio_is_valid(pdata->rstn)) { |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1486 | rc = devm_gpio_request_one(&spi->dev, pdata->rstn, |
| 1487 | GPIOF_OUT_INIT_HIGH, "rstn"); |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1488 | if (rc) |
| 1489 | return rc; |
| 1490 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1491 | |
| 1492 | if (gpio_is_valid(pdata->slp_tr)) { |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1493 | rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr, |
| 1494 | GPIOF_OUT_INIT_LOW, "slp_tr"); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1495 | if (rc) |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1496 | return rc; |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /* Reset */ |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1500 | if (gpio_is_valid(pdata->rstn)) { |
| 1501 | udelay(1); |
| 1502 | gpio_set_value(pdata->rstn, 0); |
| 1503 | udelay(1); |
| 1504 | gpio_set_value(pdata->rstn, 1); |
| 1505 | usleep_range(120, 240); |
| 1506 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1507 | |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1508 | dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops); |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1509 | if (!dev) |
| 1510 | return -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1511 | |
| 1512 | lp = dev->priv; |
| 1513 | lp->dev = dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1514 | lp->spi = spi; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1515 | dev->parent = &spi->dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1516 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame] | 1517 | lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config); |
| 1518 | if (IS_ERR(lp->regmap)) { |
| 1519 | rc = PTR_ERR(lp->regmap); |
| 1520 | dev_err(&spi->dev, "Failed to allocate register map: %d\n", |
| 1521 | rc); |
| 1522 | goto free_dev; |
| 1523 | } |
| 1524 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1525 | at86rf230_setup_spi_messages(lp); |
| 1526 | |
Alexander Aring | c8ee0f5 | 2014-07-03 00:20:45 +0200 | [diff] [blame] | 1527 | rc = at86rf230_detect_device(lp); |
| 1528 | if (rc < 0) |
| 1529 | goto free_dev; |
| 1530 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1531 | spin_lock_init(&lp->lock); |
| 1532 | init_completion(&lp->tx_complete); |
Alexander Aring | 2e0571c | 2014-07-03 00:20:51 +0200 | [diff] [blame] | 1533 | init_completion(&lp->state_complete); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1534 | |
| 1535 | spi_set_drvdata(spi, lp); |
| 1536 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1537 | rc = at86rf230_hw_init(lp); |
| 1538 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1539 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1540 | |
Alexander Aring | 1962694 | 2014-04-24 19:09:15 +0200 | [diff] [blame] | 1541 | /* Read irq status register to reset irq line */ |
| 1542 | 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] | 1543 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1544 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1545 | |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1546 | irq_type = irq_get_trigger_type(spi->irq); |
| 1547 | if (!irq_type) |
| 1548 | irq_type = IRQF_TRIGGER_RISING; |
| 1549 | |
| 1550 | rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr, |
| 1551 | IRQF_SHARED | irq_type, dev_name(&spi->dev), lp); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1552 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1553 | goto free_dev; |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1554 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1555 | rc = ieee802154_register_device(lp->dev); |
| 1556 | if (rc) |
Alexander Aring | 1d15d6b | 2014-07-03 00:20:48 +0200 | [diff] [blame] | 1557 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1558 | |
| 1559 | return rc; |
| 1560 | |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1561 | free_dev: |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1562 | ieee802154_free_device(lp->dev); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1563 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1564 | return rc; |
| 1565 | } |
| 1566 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1567 | static int at86rf230_remove(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1568 | { |
| 1569 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
| 1570 | |
Alexander Aring | 17e84a9 | 2014-03-31 03:26:51 +0200 | [diff] [blame] | 1571 | /* mask all at86rf230 irq's */ |
| 1572 | at86rf230_write_subreg(lp, SR_IRQ_MASK, 0); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1573 | ieee802154_unregister_device(lp->dev); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1574 | ieee802154_free_device(lp->dev); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1575 | dev_dbg(&spi->dev, "unregistered at86rf230\n"); |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1576 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1577 | return 0; |
| 1578 | } |
| 1579 | |
Alexander Aring | 1086b4f | 2014-04-24 19:09:11 +0200 | [diff] [blame] | 1580 | static const struct of_device_id at86rf230_of_match[] = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1581 | { .compatible = "atmel,at86rf230", }, |
| 1582 | { .compatible = "atmel,at86rf231", }, |
| 1583 | { .compatible = "atmel,at86rf233", }, |
| 1584 | { .compatible = "atmel,at86rf212", }, |
| 1585 | { }, |
| 1586 | }; |
Alexander Aring | 835cb7d | 2014-04-24 19:09:10 +0200 | [diff] [blame] | 1587 | MODULE_DEVICE_TABLE(of, at86rf230_of_match); |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1588 | |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1589 | static const struct spi_device_id at86rf230_device_id[] = { |
| 1590 | { .name = "at86rf230", }, |
| 1591 | { .name = "at86rf231", }, |
| 1592 | { .name = "at86rf233", }, |
| 1593 | { .name = "at86rf212", }, |
| 1594 | { }, |
| 1595 | }; |
| 1596 | MODULE_DEVICE_TABLE(spi, at86rf230_device_id); |
| 1597 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1598 | static struct spi_driver at86rf230_driver = { |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1599 | .id_table = at86rf230_device_id, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1600 | .driver = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1601 | .of_match_table = of_match_ptr(at86rf230_of_match), |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1602 | .name = "at86rf230", |
| 1603 | .owner = THIS_MODULE, |
| 1604 | }, |
| 1605 | .probe = at86rf230_probe, |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1606 | .remove = at86rf230_remove, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1607 | }; |
| 1608 | |
alex.bluesman.smirnov@gmail.com | 395a573 | 2012-08-26 05:10:10 +0000 | [diff] [blame] | 1609 | module_spi_driver(at86rf230_driver); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1610 | |
| 1611 | MODULE_DESCRIPTION("AT86RF230 Transceiver Driver"); |
| 1612 | MODULE_LICENSE("GPL v2"); |