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