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