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