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