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