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