alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AT86RF230/RF231 driver |
| 3 | * |
| 4 | * Copyright (C) 2009-2012 Siemens AG |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Written by: |
| 20 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 21 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 22 | */ |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/interrupt.h> |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 26 | #include <linux/irq.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 27 | #include <linux/gpio.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/workqueue.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/spi/spi.h> |
| 33 | #include <linux/spi/at86rf230.h> |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 34 | #include <linux/regmap.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 35 | #include <linux/skbuff.h> |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 36 | #include <linux/of_gpio.h> |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 37 | |
| 38 | #include <net/mac802154.h> |
| 39 | #include <net/wpan-phy.h> |
| 40 | |
| 41 | struct at86rf230_local { |
| 42 | struct spi_device *spi; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 43 | |
| 44 | u8 part; |
| 45 | u8 vers; |
| 46 | |
| 47 | u8 buf[2]; |
| 48 | struct mutex bmux; |
| 49 | |
| 50 | struct work_struct irqwork; |
| 51 | struct completion tx_complete; |
| 52 | |
| 53 | struct ieee802154_dev *dev; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 54 | struct regmap *regmap; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 55 | |
| 56 | spinlock_t lock; |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 57 | bool irq_busy; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 58 | bool is_tx; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 59 | bool tx_aret; |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 60 | |
| 61 | int rssi_base_val; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Jean Sacren | 44a6bd8 | 2014-02-25 22:38:29 -0700 | [diff] [blame] | 64 | static bool is_rf212(struct at86rf230_local *local) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 65 | { |
| 66 | return local->part == 7; |
| 67 | } |
| 68 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 69 | #define RG_TRX_STATUS (0x01) |
| 70 | #define SR_TRX_STATUS 0x01, 0x1f, 0 |
| 71 | #define SR_RESERVED_01_3 0x01, 0x20, 5 |
| 72 | #define SR_CCA_STATUS 0x01, 0x40, 6 |
| 73 | #define SR_CCA_DONE 0x01, 0x80, 7 |
| 74 | #define RG_TRX_STATE (0x02) |
| 75 | #define SR_TRX_CMD 0x02, 0x1f, 0 |
| 76 | #define SR_TRAC_STATUS 0x02, 0xe0, 5 |
| 77 | #define RG_TRX_CTRL_0 (0x03) |
| 78 | #define SR_CLKM_CTRL 0x03, 0x07, 0 |
| 79 | #define SR_CLKM_SHA_SEL 0x03, 0x08, 3 |
| 80 | #define SR_PAD_IO_CLKM 0x03, 0x30, 4 |
| 81 | #define SR_PAD_IO 0x03, 0xc0, 6 |
| 82 | #define RG_TRX_CTRL_1 (0x04) |
| 83 | #define SR_IRQ_POLARITY 0x04, 0x01, 0 |
| 84 | #define SR_IRQ_MASK_MODE 0x04, 0x02, 1 |
| 85 | #define SR_SPI_CMD_MODE 0x04, 0x0c, 2 |
| 86 | #define SR_RX_BL_CTRL 0x04, 0x10, 4 |
| 87 | #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5 |
| 88 | #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6 |
| 89 | #define SR_PA_EXT_EN 0x04, 0x80, 7 |
| 90 | #define RG_PHY_TX_PWR (0x05) |
| 91 | #define SR_TX_PWR 0x05, 0x0f, 0 |
| 92 | #define SR_PA_LT 0x05, 0x30, 4 |
| 93 | #define SR_PA_BUF_LT 0x05, 0xc0, 6 |
| 94 | #define RG_PHY_RSSI (0x06) |
| 95 | #define SR_RSSI 0x06, 0x1f, 0 |
| 96 | #define SR_RND_VALUE 0x06, 0x60, 5 |
| 97 | #define SR_RX_CRC_VALID 0x06, 0x80, 7 |
| 98 | #define RG_PHY_ED_LEVEL (0x07) |
| 99 | #define SR_ED_LEVEL 0x07, 0xff, 0 |
| 100 | #define RG_PHY_CC_CCA (0x08) |
| 101 | #define SR_CHANNEL 0x08, 0x1f, 0 |
| 102 | #define SR_CCA_MODE 0x08, 0x60, 5 |
| 103 | #define SR_CCA_REQUEST 0x08, 0x80, 7 |
| 104 | #define RG_CCA_THRES (0x09) |
| 105 | #define SR_CCA_ED_THRES 0x09, 0x0f, 0 |
| 106 | #define SR_RESERVED_09_1 0x09, 0xf0, 4 |
| 107 | #define RG_RX_CTRL (0x0a) |
| 108 | #define SR_PDT_THRES 0x0a, 0x0f, 0 |
| 109 | #define SR_RESERVED_0a_1 0x0a, 0xf0, 4 |
| 110 | #define RG_SFD_VALUE (0x0b) |
| 111 | #define SR_SFD_VALUE 0x0b, 0xff, 0 |
| 112 | #define RG_TRX_CTRL_2 (0x0c) |
| 113 | #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0 |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 114 | #define SR_SUB_MODE 0x0c, 0x04, 2 |
| 115 | #define SR_BPSK_QPSK 0x0c, 0x08, 3 |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 116 | #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4 |
| 117 | #define SR_RESERVED_0c_5 0x0c, 0x60, 5 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 118 | #define SR_RX_SAFE_MODE 0x0c, 0x80, 7 |
| 119 | #define RG_ANT_DIV (0x0d) |
| 120 | #define SR_ANT_CTRL 0x0d, 0x03, 0 |
| 121 | #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2 |
| 122 | #define SR_ANT_DIV_EN 0x0d, 0x08, 3 |
| 123 | #define SR_RESERVED_0d_2 0x0d, 0x70, 4 |
| 124 | #define SR_ANT_SEL 0x0d, 0x80, 7 |
| 125 | #define RG_IRQ_MASK (0x0e) |
| 126 | #define SR_IRQ_MASK 0x0e, 0xff, 0 |
| 127 | #define RG_IRQ_STATUS (0x0f) |
| 128 | #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0 |
| 129 | #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1 |
| 130 | #define SR_IRQ_2_RX_START 0x0f, 0x04, 2 |
| 131 | #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3 |
| 132 | #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4 |
| 133 | #define SR_IRQ_5_AMI 0x0f, 0x20, 5 |
| 134 | #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6 |
| 135 | #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7 |
| 136 | #define RG_VREG_CTRL (0x10) |
| 137 | #define SR_RESERVED_10_6 0x10, 0x03, 0 |
| 138 | #define SR_DVDD_OK 0x10, 0x04, 2 |
| 139 | #define SR_DVREG_EXT 0x10, 0x08, 3 |
| 140 | #define SR_RESERVED_10_3 0x10, 0x30, 4 |
| 141 | #define SR_AVDD_OK 0x10, 0x40, 6 |
| 142 | #define SR_AVREG_EXT 0x10, 0x80, 7 |
| 143 | #define RG_BATMON (0x11) |
| 144 | #define SR_BATMON_VTH 0x11, 0x0f, 0 |
| 145 | #define SR_BATMON_HR 0x11, 0x10, 4 |
| 146 | #define SR_BATMON_OK 0x11, 0x20, 5 |
| 147 | #define SR_RESERVED_11_1 0x11, 0xc0, 6 |
| 148 | #define RG_XOSC_CTRL (0x12) |
| 149 | #define SR_XTAL_TRIM 0x12, 0x0f, 0 |
| 150 | #define SR_XTAL_MODE 0x12, 0xf0, 4 |
| 151 | #define RG_RX_SYN (0x15) |
| 152 | #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0 |
| 153 | #define SR_RESERVED_15_2 0x15, 0x70, 4 |
| 154 | #define SR_RX_PDT_DIS 0x15, 0x80, 7 |
| 155 | #define RG_XAH_CTRL_1 (0x17) |
| 156 | #define SR_RESERVED_17_8 0x17, 0x01, 0 |
| 157 | #define SR_AACK_PROM_MODE 0x17, 0x02, 1 |
| 158 | #define SR_AACK_ACK_TIME 0x17, 0x04, 2 |
| 159 | #define SR_RESERVED_17_5 0x17, 0x08, 3 |
| 160 | #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4 |
| 161 | #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5 |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 162 | #define SR_CSMA_LBT_MODE 0x17, 0x40, 6 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 163 | #define SR_RESERVED_17_1 0x17, 0x80, 7 |
| 164 | #define RG_FTN_CTRL (0x18) |
| 165 | #define SR_RESERVED_18_2 0x18, 0x7f, 0 |
| 166 | #define SR_FTN_START 0x18, 0x80, 7 |
| 167 | #define RG_PLL_CF (0x1a) |
| 168 | #define SR_RESERVED_1a_2 0x1a, 0x7f, 0 |
| 169 | #define SR_PLL_CF_START 0x1a, 0x80, 7 |
| 170 | #define RG_PLL_DCU (0x1b) |
| 171 | #define SR_RESERVED_1b_3 0x1b, 0x3f, 0 |
| 172 | #define SR_RESERVED_1b_2 0x1b, 0x40, 6 |
| 173 | #define SR_PLL_DCU_START 0x1b, 0x80, 7 |
| 174 | #define RG_PART_NUM (0x1c) |
| 175 | #define SR_PART_NUM 0x1c, 0xff, 0 |
| 176 | #define RG_VERSION_NUM (0x1d) |
| 177 | #define SR_VERSION_NUM 0x1d, 0xff, 0 |
| 178 | #define RG_MAN_ID_0 (0x1e) |
| 179 | #define SR_MAN_ID_0 0x1e, 0xff, 0 |
| 180 | #define RG_MAN_ID_1 (0x1f) |
| 181 | #define SR_MAN_ID_1 0x1f, 0xff, 0 |
| 182 | #define RG_SHORT_ADDR_0 (0x20) |
| 183 | #define SR_SHORT_ADDR_0 0x20, 0xff, 0 |
| 184 | #define RG_SHORT_ADDR_1 (0x21) |
| 185 | #define SR_SHORT_ADDR_1 0x21, 0xff, 0 |
| 186 | #define RG_PAN_ID_0 (0x22) |
| 187 | #define SR_PAN_ID_0 0x22, 0xff, 0 |
| 188 | #define RG_PAN_ID_1 (0x23) |
| 189 | #define SR_PAN_ID_1 0x23, 0xff, 0 |
| 190 | #define RG_IEEE_ADDR_0 (0x24) |
| 191 | #define SR_IEEE_ADDR_0 0x24, 0xff, 0 |
| 192 | #define RG_IEEE_ADDR_1 (0x25) |
| 193 | #define SR_IEEE_ADDR_1 0x25, 0xff, 0 |
| 194 | #define RG_IEEE_ADDR_2 (0x26) |
| 195 | #define SR_IEEE_ADDR_2 0x26, 0xff, 0 |
| 196 | #define RG_IEEE_ADDR_3 (0x27) |
| 197 | #define SR_IEEE_ADDR_3 0x27, 0xff, 0 |
| 198 | #define RG_IEEE_ADDR_4 (0x28) |
| 199 | #define SR_IEEE_ADDR_4 0x28, 0xff, 0 |
| 200 | #define RG_IEEE_ADDR_5 (0x29) |
| 201 | #define SR_IEEE_ADDR_5 0x29, 0xff, 0 |
| 202 | #define RG_IEEE_ADDR_6 (0x2a) |
| 203 | #define SR_IEEE_ADDR_6 0x2a, 0xff, 0 |
| 204 | #define RG_IEEE_ADDR_7 (0x2b) |
| 205 | #define SR_IEEE_ADDR_7 0x2b, 0xff, 0 |
| 206 | #define RG_XAH_CTRL_0 (0x2c) |
| 207 | #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0 |
| 208 | #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1 |
| 209 | #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4 |
| 210 | #define RG_CSMA_SEED_0 (0x2d) |
| 211 | #define SR_CSMA_SEED_0 0x2d, 0xff, 0 |
| 212 | #define RG_CSMA_SEED_1 (0x2e) |
| 213 | #define SR_CSMA_SEED_1 0x2e, 0x07, 0 |
| 214 | #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3 |
| 215 | #define SR_AACK_DIS_ACK 0x2e, 0x10, 4 |
| 216 | #define SR_AACK_SET_PD 0x2e, 0x20, 5 |
| 217 | #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6 |
| 218 | #define RG_CSMA_BE (0x2f) |
| 219 | #define SR_MIN_BE 0x2f, 0x0f, 0 |
| 220 | #define SR_MAX_BE 0x2f, 0xf0, 4 |
| 221 | |
| 222 | #define CMD_REG 0x80 |
| 223 | #define CMD_REG_MASK 0x3f |
| 224 | #define CMD_WRITE 0x40 |
| 225 | #define CMD_FB 0x20 |
| 226 | |
| 227 | #define IRQ_BAT_LOW (1 << 7) |
| 228 | #define IRQ_TRX_UR (1 << 6) |
| 229 | #define IRQ_AMI (1 << 5) |
| 230 | #define IRQ_CCA_ED (1 << 4) |
| 231 | #define IRQ_TRX_END (1 << 3) |
| 232 | #define IRQ_RX_START (1 << 2) |
| 233 | #define IRQ_PLL_UNL (1 << 1) |
| 234 | #define IRQ_PLL_LOCK (1 << 0) |
| 235 | |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 236 | #define IRQ_ACTIVE_HIGH 0 |
| 237 | #define IRQ_ACTIVE_LOW 1 |
| 238 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 239 | #define STATE_P_ON 0x00 /* BUSY */ |
| 240 | #define STATE_BUSY_RX 0x01 |
| 241 | #define STATE_BUSY_TX 0x02 |
| 242 | #define STATE_FORCE_TRX_OFF 0x03 |
| 243 | #define STATE_FORCE_TX_ON 0x04 /* IDLE */ |
| 244 | /* 0x05 */ /* INVALID_PARAMETER */ |
| 245 | #define STATE_RX_ON 0x06 |
| 246 | /* 0x07 */ /* SUCCESS */ |
| 247 | #define STATE_TRX_OFF 0x08 |
| 248 | #define STATE_TX_ON 0x09 |
| 249 | /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */ |
| 250 | #define STATE_SLEEP 0x0F |
Thomas Stilwell | 48d5dba | 2014-03-10 19:29:25 -0500 | [diff] [blame] | 251 | #define STATE_PREP_DEEP_SLEEP 0x10 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 252 | #define STATE_BUSY_RX_AACK 0x11 |
| 253 | #define STATE_BUSY_TX_ARET 0x12 |
stefan@datenfreihafen.org | 028889b | 2013-03-26 12:41:31 +0000 | [diff] [blame] | 254 | #define STATE_RX_AACK_ON 0x16 |
| 255 | #define STATE_TX_ARET_ON 0x19 |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 256 | #define STATE_RX_ON_NOCLK 0x1C |
| 257 | #define STATE_RX_AACK_ON_NOCLK 0x1D |
| 258 | #define STATE_BUSY_RX_AACK_NOCLK 0x1E |
| 259 | #define STATE_TRANSITION_IN_PROGRESS 0x1F |
| 260 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 261 | #define AT86RF2XX_NUMREGS 0x3F |
| 262 | |
| 263 | static inline int |
| 264 | __at86rf230_write(struct at86rf230_local *lp, |
| 265 | unsigned int addr, unsigned int data) |
| 266 | { |
| 267 | return regmap_write(lp->regmap, addr, data); |
| 268 | } |
| 269 | |
| 270 | static inline int |
| 271 | __at86rf230_read(struct at86rf230_local *lp, |
| 272 | unsigned int addr, unsigned int *data) |
| 273 | { |
| 274 | return regmap_read(lp->regmap, addr, data); |
| 275 | } |
| 276 | |
| 277 | static inline int |
| 278 | at86rf230_read_subreg(struct at86rf230_local *lp, |
| 279 | unsigned int addr, unsigned int mask, |
| 280 | unsigned int shift, unsigned int *data) |
| 281 | { |
| 282 | int rc; |
| 283 | |
| 284 | rc = __at86rf230_read(lp, addr, data); |
| 285 | if (rc > 0) |
| 286 | *data = (*data & mask) >> shift; |
| 287 | |
| 288 | return rc; |
| 289 | } |
| 290 | |
| 291 | static inline int |
| 292 | at86rf230_write_subreg(struct at86rf230_local *lp, |
| 293 | unsigned int addr, unsigned int mask, |
| 294 | unsigned int shift, unsigned int data) |
| 295 | { |
| 296 | return regmap_update_bits(lp->regmap, addr, mask, data << shift); |
| 297 | } |
| 298 | |
| 299 | static bool |
| 300 | at86rf230_reg_writeable(struct device *dev, unsigned int reg) |
| 301 | { |
| 302 | switch (reg) { |
| 303 | case RG_TRX_STATE: |
| 304 | case RG_TRX_CTRL_0: |
| 305 | case RG_TRX_CTRL_1: |
| 306 | case RG_PHY_TX_PWR: |
| 307 | case RG_PHY_ED_LEVEL: |
| 308 | case RG_PHY_CC_CCA: |
| 309 | case RG_CCA_THRES: |
| 310 | case RG_RX_CTRL: |
| 311 | case RG_SFD_VALUE: |
| 312 | case RG_TRX_CTRL_2: |
| 313 | case RG_ANT_DIV: |
| 314 | case RG_IRQ_MASK: |
| 315 | case RG_VREG_CTRL: |
| 316 | case RG_BATMON: |
| 317 | case RG_XOSC_CTRL: |
| 318 | case RG_RX_SYN: |
| 319 | case RG_XAH_CTRL_1: |
| 320 | case RG_FTN_CTRL: |
| 321 | case RG_PLL_CF: |
| 322 | case RG_PLL_DCU: |
| 323 | case RG_SHORT_ADDR_0: |
| 324 | case RG_SHORT_ADDR_1: |
| 325 | case RG_PAN_ID_0: |
| 326 | case RG_PAN_ID_1: |
| 327 | case RG_IEEE_ADDR_0: |
| 328 | case RG_IEEE_ADDR_1: |
| 329 | case RG_IEEE_ADDR_2: |
| 330 | case RG_IEEE_ADDR_3: |
| 331 | case RG_IEEE_ADDR_4: |
| 332 | case RG_IEEE_ADDR_5: |
| 333 | case RG_IEEE_ADDR_6: |
| 334 | case RG_IEEE_ADDR_7: |
| 335 | case RG_XAH_CTRL_0: |
| 336 | case RG_CSMA_SEED_0: |
| 337 | case RG_CSMA_SEED_1: |
| 338 | case RG_CSMA_BE: |
| 339 | return true; |
| 340 | default: |
| 341 | return false; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static bool |
| 346 | at86rf230_reg_readable(struct device *dev, unsigned int reg) |
| 347 | { |
| 348 | bool rc; |
| 349 | |
| 350 | /* all writeable are also readable */ |
| 351 | rc = at86rf230_reg_writeable(dev, reg); |
| 352 | if (rc) |
| 353 | return rc; |
| 354 | |
| 355 | /* readonly regs */ |
| 356 | switch (reg) { |
| 357 | case RG_TRX_STATUS: |
| 358 | case RG_PHY_RSSI: |
| 359 | case RG_IRQ_STATUS: |
| 360 | case RG_PART_NUM: |
| 361 | case RG_VERSION_NUM: |
| 362 | case RG_MAN_ID_1: |
| 363 | case RG_MAN_ID_0: |
| 364 | return true; |
| 365 | default: |
| 366 | return false; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static bool |
| 371 | at86rf230_reg_volatile(struct device *dev, unsigned int reg) |
| 372 | { |
| 373 | /* can be changed during runtime */ |
| 374 | switch (reg) { |
| 375 | case RG_TRX_STATUS: |
| 376 | case RG_TRX_STATE: |
| 377 | case RG_PHY_RSSI: |
| 378 | case RG_PHY_ED_LEVEL: |
| 379 | case RG_IRQ_STATUS: |
| 380 | case RG_VREG_CTRL: |
| 381 | return true; |
| 382 | default: |
| 383 | return false; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static bool |
| 388 | at86rf230_reg_precious(struct device *dev, unsigned int reg) |
| 389 | { |
| 390 | /* don't clear irq line on read */ |
| 391 | switch (reg) { |
| 392 | case RG_IRQ_STATUS: |
| 393 | return true; |
| 394 | default: |
| 395 | return false; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | static struct regmap_config at86rf230_regmap_spi_config = { |
| 400 | .reg_bits = 8, |
| 401 | .val_bits = 8, |
| 402 | .write_flag_mask = CMD_REG | CMD_WRITE, |
| 403 | .read_flag_mask = CMD_REG, |
| 404 | .cache_type = REGCACHE_RBTREE, |
| 405 | .max_register = AT86RF2XX_NUMREGS, |
| 406 | .writeable_reg = at86rf230_reg_writeable, |
| 407 | .readable_reg = at86rf230_reg_readable, |
| 408 | .volatile_reg = at86rf230_reg_volatile, |
| 409 | .precious_reg = at86rf230_reg_precious, |
| 410 | }; |
| 411 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 412 | static int |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 413 | __at86rf230_detect_device(struct spi_device *spi, u16 *man_id, u8 *part, |
| 414 | u8 *version) |
| 415 | { |
| 416 | u8 data[4]; |
| 417 | u8 *buf = kmalloc(2, GFP_KERNEL); |
| 418 | int status; |
| 419 | struct spi_message msg; |
| 420 | struct spi_transfer xfer = { |
| 421 | .len = 2, |
| 422 | .tx_buf = buf, |
| 423 | .rx_buf = buf, |
| 424 | }; |
| 425 | u8 reg; |
| 426 | |
| 427 | if (!buf) |
| 428 | return -ENOMEM; |
| 429 | |
| 430 | for (reg = RG_PART_NUM; reg <= RG_MAN_ID_1; reg++) { |
| 431 | buf[0] = (reg & CMD_REG_MASK) | CMD_REG; |
| 432 | buf[1] = 0xff; |
| 433 | dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]); |
| 434 | spi_message_init(&msg); |
| 435 | spi_message_add_tail(&xfer, &msg); |
| 436 | |
| 437 | status = spi_sync(spi, &msg); |
| 438 | dev_vdbg(&spi->dev, "status = %d\n", status); |
| 439 | if (msg.status) |
| 440 | status = msg.status; |
| 441 | |
| 442 | dev_vdbg(&spi->dev, "status = %d\n", status); |
| 443 | dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]); |
| 444 | dev_vdbg(&spi->dev, "buf[1] = %02x\n", buf[1]); |
| 445 | |
| 446 | if (status == 0) |
| 447 | data[reg - RG_PART_NUM] = buf[1]; |
| 448 | else |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | if (status == 0) { |
| 453 | *part = data[0]; |
| 454 | *version = data[1]; |
| 455 | *man_id = (data[3] << 8) | data[2]; |
| 456 | } |
| 457 | |
| 458 | kfree(buf); |
| 459 | |
| 460 | return status; |
| 461 | } |
| 462 | |
| 463 | static int |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 464 | at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len) |
| 465 | { |
| 466 | u8 *buf = lp->buf; |
| 467 | int status; |
| 468 | struct spi_message msg; |
| 469 | struct spi_transfer xfer_head = { |
| 470 | .len = 2, |
| 471 | .tx_buf = buf, |
| 472 | |
| 473 | }; |
| 474 | struct spi_transfer xfer_buf = { |
| 475 | .len = len, |
| 476 | .tx_buf = data, |
| 477 | }; |
| 478 | |
| 479 | mutex_lock(&lp->bmux); |
| 480 | buf[0] = CMD_WRITE | CMD_FB; |
| 481 | buf[1] = len + 2; /* 2 bytes for CRC that isn't written */ |
| 482 | |
| 483 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
| 484 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
| 485 | |
| 486 | spi_message_init(&msg); |
| 487 | spi_message_add_tail(&xfer_head, &msg); |
| 488 | spi_message_add_tail(&xfer_buf, &msg); |
| 489 | |
| 490 | status = spi_sync(lp->spi, &msg); |
| 491 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
| 492 | if (msg.status) |
| 493 | status = msg.status; |
| 494 | |
| 495 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
| 496 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
| 497 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
| 498 | |
| 499 | mutex_unlock(&lp->bmux); |
| 500 | return status; |
| 501 | } |
| 502 | |
| 503 | static int |
| 504 | at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi) |
| 505 | { |
| 506 | u8 *buf = lp->buf; |
| 507 | int status; |
| 508 | struct spi_message msg; |
| 509 | struct spi_transfer xfer_head = { |
| 510 | .len = 2, |
| 511 | .tx_buf = buf, |
| 512 | .rx_buf = buf, |
| 513 | }; |
| 514 | struct spi_transfer xfer_head1 = { |
| 515 | .len = 2, |
| 516 | .tx_buf = buf, |
| 517 | .rx_buf = buf, |
| 518 | }; |
| 519 | struct spi_transfer xfer_buf = { |
| 520 | .len = 0, |
| 521 | .rx_buf = data, |
| 522 | }; |
| 523 | |
| 524 | mutex_lock(&lp->bmux); |
| 525 | |
| 526 | buf[0] = CMD_FB; |
| 527 | buf[1] = 0x00; |
| 528 | |
| 529 | spi_message_init(&msg); |
| 530 | spi_message_add_tail(&xfer_head, &msg); |
| 531 | |
| 532 | status = spi_sync(lp->spi, &msg); |
| 533 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
| 534 | |
| 535 | xfer_buf.len = *(buf + 1) + 1; |
| 536 | *len = buf[1]; |
| 537 | |
| 538 | buf[0] = CMD_FB; |
| 539 | buf[1] = 0x00; |
| 540 | |
| 541 | spi_message_init(&msg); |
| 542 | spi_message_add_tail(&xfer_head1, &msg); |
| 543 | spi_message_add_tail(&xfer_buf, &msg); |
| 544 | |
| 545 | status = spi_sync(lp->spi, &msg); |
| 546 | |
| 547 | if (msg.status) |
| 548 | status = msg.status; |
| 549 | |
| 550 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
| 551 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
| 552 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
| 553 | |
| 554 | if (status) { |
| 555 | if (lqi && (*len > lp->buf[1])) |
| 556 | *lqi = data[lp->buf[1]]; |
| 557 | } |
| 558 | mutex_unlock(&lp->bmux); |
| 559 | |
| 560 | return status; |
| 561 | } |
| 562 | |
| 563 | static int |
| 564 | at86rf230_ed(struct ieee802154_dev *dev, u8 *level) |
| 565 | { |
| 566 | might_sleep(); |
| 567 | BUG_ON(!level); |
| 568 | *level = 0xbe; |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int |
| 573 | at86rf230_state(struct ieee802154_dev *dev, int state) |
| 574 | { |
| 575 | struct at86rf230_local *lp = dev->priv; |
| 576 | int rc; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 577 | unsigned int val; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 578 | u8 desired_status; |
| 579 | |
| 580 | might_sleep(); |
| 581 | |
| 582 | if (state == STATE_FORCE_TX_ON) |
| 583 | desired_status = STATE_TX_ON; |
| 584 | else if (state == STATE_FORCE_TRX_OFF) |
| 585 | desired_status = STATE_TRX_OFF; |
| 586 | else |
| 587 | desired_status = state; |
| 588 | |
| 589 | do { |
| 590 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val); |
| 591 | if (rc) |
| 592 | goto err; |
| 593 | } while (val == STATE_TRANSITION_IN_PROGRESS); |
| 594 | |
| 595 | if (val == desired_status) |
| 596 | return 0; |
| 597 | |
| 598 | /* state is equal to phy states */ |
| 599 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state); |
| 600 | if (rc) |
| 601 | goto err; |
| 602 | |
| 603 | do { |
| 604 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val); |
| 605 | if (rc) |
| 606 | goto err; |
| 607 | } while (val == STATE_TRANSITION_IN_PROGRESS); |
| 608 | |
| 609 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 610 | if (val == desired_status || |
| 611 | (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) || |
| 612 | (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK)) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 613 | return 0; |
| 614 | |
| 615 | pr_err("unexpected state change: %d, asked for %d\n", val, state); |
| 616 | return -EBUSY; |
| 617 | |
| 618 | err: |
| 619 | pr_err("error: %d\n", rc); |
| 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | static int |
| 624 | at86rf230_start(struct ieee802154_dev *dev) |
| 625 | { |
| 626 | struct at86rf230_local *lp = dev->priv; |
| 627 | u8 rc; |
| 628 | |
| 629 | rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1); |
| 630 | if (rc) |
| 631 | return rc; |
| 632 | |
Alexander Aring | 7332fcb | 2014-03-15 09:29:03 +0100 | [diff] [blame] | 633 | rc = at86rf230_state(dev, STATE_TX_ON); |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 634 | if (rc) |
| 635 | return rc; |
| 636 | |
Phoebe Buckheister | 5b520bb | 2014-02-17 11:34:07 +0100 | [diff] [blame] | 637 | return at86rf230_state(dev, STATE_RX_AACK_ON); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static void |
| 641 | at86rf230_stop(struct ieee802154_dev *dev) |
| 642 | { |
| 643 | at86rf230_state(dev, STATE_FORCE_TRX_OFF); |
| 644 | } |
| 645 | |
| 646 | static int |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 647 | at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel) |
| 648 | { |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 649 | lp->rssi_base_val = -91; |
| 650 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 651 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 652 | } |
| 653 | |
| 654 | static int |
| 655 | at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) |
| 656 | { |
| 657 | int rc; |
| 658 | |
| 659 | if (channel == 0) |
| 660 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0); |
| 661 | else |
| 662 | rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1); |
| 663 | if (rc < 0) |
| 664 | return rc; |
| 665 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 666 | if (page == 0) { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 667 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0); |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 668 | lp->rssi_base_val = -100; |
| 669 | } else { |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 670 | rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1); |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 671 | lp->rssi_base_val = -98; |
| 672 | } |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 673 | if (rc < 0) |
| 674 | return rc; |
| 675 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 676 | return at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
| 677 | } |
| 678 | |
| 679 | static int |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 680 | at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) |
| 681 | { |
| 682 | struct at86rf230_local *lp = dev->priv; |
| 683 | int rc; |
| 684 | |
| 685 | might_sleep(); |
| 686 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 687 | if (page < 0 || page > 31 || |
| 688 | !(lp->dev->phy->channels_supported[page] & BIT(channel))) { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 689 | WARN_ON(1); |
| 690 | return -EINVAL; |
| 691 | } |
| 692 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 693 | if (is_rf212(lp)) |
| 694 | rc = at86rf212_set_channel(lp, page, channel); |
| 695 | else |
| 696 | rc = at86rf230_set_channel(lp, page, channel); |
| 697 | if (rc < 0) |
| 698 | return rc; |
| 699 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 700 | msleep(1); /* Wait for PLL */ |
| 701 | dev->phy->current_channel = channel; |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 702 | dev->phy->current_page = page; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int |
| 708 | at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb) |
| 709 | { |
| 710 | struct at86rf230_local *lp = dev->priv; |
| 711 | int rc; |
| 712 | unsigned long flags; |
| 713 | |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 714 | spin_lock_irqsave(&lp->lock, flags); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 715 | if (lp->irq_busy) { |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 716 | spin_unlock_irqrestore(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 717 | return -EBUSY; |
| 718 | } |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 719 | spin_unlock_irqrestore(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 720 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 721 | might_sleep(); |
| 722 | |
| 723 | rc = at86rf230_state(dev, STATE_FORCE_TX_ON); |
| 724 | if (rc) |
| 725 | goto err; |
| 726 | |
| 727 | spin_lock_irqsave(&lp->lock, flags); |
| 728 | lp->is_tx = 1; |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 729 | reinit_completion(&lp->tx_complete); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 730 | spin_unlock_irqrestore(&lp->lock, flags); |
| 731 | |
| 732 | rc = at86rf230_write_fbuf(lp, skb->data, skb->len); |
| 733 | if (rc) |
| 734 | goto err_rx; |
| 735 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 736 | if (lp->tx_aret) { |
| 737 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ARET_ON); |
| 738 | if (rc) |
| 739 | goto err_rx; |
| 740 | } |
| 741 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 742 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX); |
| 743 | if (rc) |
| 744 | goto err_rx; |
| 745 | |
| 746 | rc = wait_for_completion_interruptible(&lp->tx_complete); |
| 747 | if (rc < 0) |
| 748 | goto err_rx; |
| 749 | |
Alexander Aring | ee69559 | 2014-04-24 19:09:14 +0200 | [diff] [blame] | 750 | return at86rf230_start(dev); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 751 | err_rx: |
| 752 | at86rf230_start(dev); |
| 753 | err: |
| 754 | pr_err("error: %d\n", rc); |
| 755 | |
| 756 | spin_lock_irqsave(&lp->lock, flags); |
| 757 | lp->is_tx = 0; |
| 758 | spin_unlock_irqrestore(&lp->lock, flags); |
| 759 | |
| 760 | return rc; |
| 761 | } |
| 762 | |
| 763 | static int at86rf230_rx(struct at86rf230_local *lp) |
| 764 | { |
| 765 | u8 len = 128, lqi = 0; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 766 | struct sk_buff *skb; |
| 767 | |
| 768 | skb = alloc_skb(len, GFP_KERNEL); |
| 769 | |
| 770 | if (!skb) |
| 771 | return -ENOMEM; |
| 772 | |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 773 | if (at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi)) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 774 | goto err; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 775 | |
| 776 | if (len < 2) |
| 777 | goto err; |
| 778 | |
| 779 | skb_trim(skb, len - 2); /* We do not put CRC into the frame */ |
| 780 | |
| 781 | ieee802154_rx_irqsafe(lp->dev, skb, lqi); |
| 782 | |
alex.bluesman.smirnov@gmail.com | 23c3421 | 2012-07-01 20:18:32 +0000 | [diff] [blame] | 783 | dev_dbg(&lp->spi->dev, "READ_FBUF: %d %x\n", len, lqi); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 784 | |
| 785 | return 0; |
| 786 | err: |
| 787 | pr_debug("received frame is too small\n"); |
| 788 | |
| 789 | kfree_skb(skb); |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 793 | static int |
| 794 | at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev, |
| 795 | struct ieee802154_hw_addr_filt *filt, |
| 796 | unsigned long changed) |
| 797 | { |
| 798 | struct at86rf230_local *lp = dev->priv; |
| 799 | |
| 800 | if (changed & IEEE802515_AFILT_SADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 801 | u16 addr = le16_to_cpu(filt->short_addr); |
| 802 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 803 | dev_vdbg(&lp->spi->dev, |
| 804 | "at86rf230_set_hw_addr_filt called for saddr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 805 | __at86rf230_write(lp, RG_SHORT_ADDR_0, addr); |
| 806 | __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (changed & IEEE802515_AFILT_PANID_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 810 | u16 pan = le16_to_cpu(filt->pan_id); |
| 811 | |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 812 | dev_vdbg(&lp->spi->dev, |
| 813 | "at86rf230_set_hw_addr_filt called for pan id\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 814 | __at86rf230_write(lp, RG_PAN_ID_0, pan); |
| 815 | __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) { |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 819 | u8 i, addr[8]; |
| 820 | |
| 821 | memcpy(addr, &filt->ieee_addr, 8); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 822 | dev_vdbg(&lp->spi->dev, |
| 823 | "at86rf230_set_hw_addr_filt called for IEEE addr\n"); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 824 | for (i = 0; i < 8; i++) |
| 825 | __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]); |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | if (changed & IEEE802515_AFILT_PANC_CHANGED) { |
| 829 | dev_vdbg(&lp->spi->dev, |
| 830 | "at86rf230_set_hw_addr_filt called for panc change\n"); |
| 831 | if (filt->pan_coord) |
| 832 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1); |
| 833 | else |
| 834 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0); |
| 835 | } |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 840 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 841 | at86rf230_set_txpower(struct ieee802154_dev *dev, int db) |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 842 | { |
| 843 | struct at86rf230_local *lp = dev->priv; |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 844 | |
| 845 | /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five |
| 846 | * bits decrease power in 1dB steps. 0x60 represents extra PA gain of |
| 847 | * 0dB. |
| 848 | * thus, supported values for db range from -26 to 5, for 31dB of |
| 849 | * reduction to 0dB of reduction. |
| 850 | */ |
| 851 | if (db > 5 || db < -26) |
| 852 | return -EINVAL; |
| 853 | |
| 854 | db = -(db - 5); |
| 855 | |
Jean Sacren | 677676c | 2014-03-01 15:54:36 -0700 | [diff] [blame] | 856 | return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db); |
Phoebe Buckheister | 9b2777d | 2014-02-17 11:34:08 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 859 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 860 | at86rf230_set_lbt(struct ieee802154_dev *dev, bool on) |
Phoebe Buckheister | 84dda3c | 2014-02-17 11:34:10 +0100 | [diff] [blame] | 861 | { |
| 862 | struct at86rf230_local *lp = dev->priv; |
| 863 | |
| 864 | return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on); |
| 865 | } |
| 866 | |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 867 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 868 | at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode) |
Phoebe Buckheister | ba08fea | 2014-02-17 11:34:11 +0100 | [diff] [blame] | 869 | { |
| 870 | struct at86rf230_local *lp = dev->priv; |
| 871 | |
| 872 | return at86rf230_write_subreg(lp, SR_CCA_MODE, mode); |
| 873 | } |
| 874 | |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 875 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 876 | at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level) |
Phoebe Buckheister | 6ca0019 | 2014-02-17 11:34:12 +0100 | [diff] [blame] | 877 | { |
| 878 | struct at86rf230_local *lp = dev->priv; |
| 879 | int desens_steps; |
| 880 | |
| 881 | if (level < lp->rssi_base_val || level > 30) |
| 882 | return -EINVAL; |
| 883 | |
| 884 | desens_steps = (level - lp->rssi_base_val) * 100 / 207; |
| 885 | |
| 886 | return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, desens_steps); |
| 887 | } |
| 888 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 889 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 890 | at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be, |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 891 | u8 retries) |
| 892 | { |
| 893 | struct at86rf230_local *lp = dev->priv; |
| 894 | int rc; |
| 895 | |
| 896 | if (min_be > max_be || max_be > 8 || retries > 5) |
| 897 | return -EINVAL; |
| 898 | |
| 899 | rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be); |
| 900 | if (rc) |
| 901 | return rc; |
| 902 | |
| 903 | rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be); |
| 904 | if (rc) |
| 905 | return rc; |
| 906 | |
Alexander Aring | 39d7f32 | 2014-04-05 13:49:26 +0200 | [diff] [blame] | 907 | return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries); |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static int |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 911 | at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries) |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 912 | { |
| 913 | struct at86rf230_local *lp = dev->priv; |
| 914 | int rc = 0; |
| 915 | |
| 916 | if (retries < -1 || retries > 15) |
| 917 | return -EINVAL; |
| 918 | |
| 919 | lp->tx_aret = retries >= 0; |
| 920 | |
| 921 | if (retries >= 0) |
| 922 | rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries); |
| 923 | |
| 924 | return rc; |
| 925 | } |
| 926 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 927 | static struct ieee802154_ops at86rf230_ops = { |
| 928 | .owner = THIS_MODULE, |
| 929 | .xmit = at86rf230_xmit, |
| 930 | .ed = at86rf230_ed, |
| 931 | .set_channel = at86rf230_channel, |
| 932 | .start = at86rf230_start, |
| 933 | .stop = at86rf230_stop, |
stefan@datenfreihafen.org | 1486774 | 2013-03-26 12:41:30 +0000 | [diff] [blame] | 934 | .set_hw_addr_filt = at86rf230_set_hw_addr_filt, |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 935 | .set_txpower = at86rf230_set_txpower, |
| 936 | .set_lbt = at86rf230_set_lbt, |
| 937 | .set_cca_mode = at86rf230_set_cca_mode, |
| 938 | .set_cca_ed_level = at86rf230_set_cca_ed_level, |
| 939 | .set_csma_params = at86rf230_set_csma_params, |
| 940 | .set_frame_retries = at86rf230_set_frame_retries, |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 941 | }; |
| 942 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 943 | static void at86rf230_irqwork(struct work_struct *work) |
| 944 | { |
| 945 | struct at86rf230_local *lp = |
| 946 | container_of(work, struct at86rf230_local, irqwork); |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 947 | unsigned int status; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 948 | int rc; |
| 949 | unsigned long flags; |
| 950 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 951 | 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] | 952 | |
| 953 | status &= ~IRQ_PLL_LOCK; /* ignore */ |
| 954 | status &= ~IRQ_RX_START; /* ignore */ |
| 955 | status &= ~IRQ_AMI; /* ignore */ |
| 956 | status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/ |
| 957 | |
| 958 | if (status & IRQ_TRX_END) { |
| 959 | status &= ~IRQ_TRX_END; |
Alexander Aring | 7e81461 | 2014-03-15 09:29:04 +0100 | [diff] [blame] | 960 | spin_lock_irqsave(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 961 | if (lp->is_tx) { |
| 962 | lp->is_tx = 0; |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 963 | spin_unlock_irqrestore(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 964 | complete(&lp->tx_complete); |
| 965 | } else { |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 966 | spin_unlock_irqrestore(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 967 | at86rf230_rx(lp); |
| 968 | } |
| 969 | } |
| 970 | |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 971 | spin_lock_irqsave(&lp->lock, flags); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 972 | lp->irq_busy = 0; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 973 | spin_unlock_irqrestore(&lp->lock, flags); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static void at86rf230_irqwork_level(struct work_struct *work) |
| 977 | { |
| 978 | struct at86rf230_local *lp = |
| 979 | container_of(work, struct at86rf230_local, irqwork); |
| 980 | |
| 981 | at86rf230_irqwork(work); |
alex.bluesman.smirnov@gmail.com | 5b00f2e | 2012-07-10 21:22:43 +0000 | [diff] [blame] | 982 | |
| 983 | enable_irq(lp->spi->irq); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static irqreturn_t at86rf230_isr(int irq, void *data) |
| 987 | { |
| 988 | struct at86rf230_local *lp = data; |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 989 | unsigned long flags; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 990 | |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 991 | spin_lock_irqsave(&lp->lock, flags); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 992 | lp->irq_busy = 1; |
Alexander Aring | 6e07a1e | 2014-03-12 08:21:24 +0100 | [diff] [blame] | 993 | spin_unlock_irqrestore(&lp->lock, flags); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 994 | |
| 995 | schedule_work(&lp->irqwork); |
| 996 | |
| 997 | return IRQ_HANDLED; |
| 998 | } |
| 999 | |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1000 | static irqreturn_t at86rf230_isr_level(int irq, void *data) |
| 1001 | { |
| 1002 | disable_irq_nosync(irq); |
| 1003 | |
| 1004 | return at86rf230_isr(irq, data); |
| 1005 | } |
| 1006 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1007 | static int at86rf230_hw_init(struct at86rf230_local *lp) |
| 1008 | { |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1009 | int rc, irq_pol, irq_type; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 1010 | unsigned int dvdd; |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1011 | u8 csma_seed[2]; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1012 | |
Phoebe Buckheister | 7dcbd22 | 2014-02-17 11:34:13 +0100 | [diff] [blame] | 1013 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF); |
| 1014 | if (rc) |
| 1015 | return rc; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1016 | |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1017 | irq_type = irq_get_trigger_type(lp->spi->irq); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1018 | /* configure irq polarity, defaults to high active */ |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1019 | if (irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW)) |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1020 | irq_pol = IRQ_ACTIVE_LOW; |
| 1021 | else |
| 1022 | irq_pol = IRQ_ACTIVE_HIGH; |
| 1023 | |
Alexander Aring | 18c6504 | 2014-04-24 19:09:18 +0200 | [diff] [blame] | 1024 | rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1025 | if (rc) |
| 1026 | return rc; |
| 1027 | |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1028 | 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] | 1029 | if (rc) |
| 1030 | return rc; |
| 1031 | |
Phoebe Buckheister | f2fdd67 | 2014-02-17 11:34:15 +0100 | [diff] [blame] | 1032 | get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed)); |
| 1033 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]); |
| 1034 | if (rc) |
| 1035 | return rc; |
| 1036 | rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]); |
| 1037 | if (rc) |
| 1038 | return rc; |
| 1039 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1040 | /* CLKM changes are applied immediately */ |
| 1041 | rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00); |
| 1042 | if (rc) |
| 1043 | return rc; |
| 1044 | |
| 1045 | /* Turn CLKM Off */ |
| 1046 | rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00); |
| 1047 | if (rc) |
| 1048 | return rc; |
| 1049 | /* Wait the next SLEEP cycle */ |
| 1050 | msleep(100); |
| 1051 | |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1052 | rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1053 | if (rc) |
| 1054 | return rc; |
Alexander Aring | 1cc9fc5 | 2014-04-24 19:09:17 +0200 | [diff] [blame] | 1055 | if (!dvdd) { |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1056 | dev_err(&lp->spi->dev, "DVDD error\n"); |
| 1057 | return -EINVAL; |
| 1058 | } |
| 1059 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
| 1062 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1063 | static struct at86rf230_platform_data * |
| 1064 | at86rf230_get_pdata(struct spi_device *spi) |
| 1065 | { |
| 1066 | struct at86rf230_platform_data *pdata; |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1067 | |
| 1068 | if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) |
| 1069 | return spi->dev.platform_data; |
| 1070 | |
| 1071 | pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL); |
| 1072 | if (!pdata) |
| 1073 | goto done; |
| 1074 | |
| 1075 | pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0); |
| 1076 | pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0); |
| 1077 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1078 | spi->dev.platform_data = pdata; |
| 1079 | done: |
| 1080 | return pdata; |
| 1081 | } |
| 1082 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1083 | static int at86rf230_probe(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1084 | { |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1085 | struct at86rf230_platform_data *pdata; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1086 | struct ieee802154_dev *dev; |
| 1087 | struct at86rf230_local *lp; |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1088 | u16 man_id = 0; |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 1089 | u8 part = 0, version = 0; |
| 1090 | unsigned int status; |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1091 | irq_handler_t irq_handler; |
| 1092 | work_func_t irq_worker; |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1093 | int rc, irq_type; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1094 | const char *chip; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (!spi->irq) { |
| 1097 | dev_err(&spi->dev, "no IRQ specified\n"); |
| 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1101 | pdata = at86rf230_get_pdata(spi); |
Sascha Herrmann | 43b5abe | 2013-04-14 22:33:28 +0000 | [diff] [blame] | 1102 | if (!pdata) { |
| 1103 | dev_err(&spi->dev, "no platform_data\n"); |
| 1104 | return -EINVAL; |
| 1105 | } |
| 1106 | |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1107 | if (gpio_is_valid(pdata->rstn)) { |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1108 | rc = devm_gpio_request_one(&spi->dev, pdata->rstn, |
| 1109 | GPIOF_OUT_INIT_HIGH, "rstn"); |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1110 | if (rc) |
| 1111 | return rc; |
| 1112 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1113 | |
| 1114 | if (gpio_is_valid(pdata->slp_tr)) { |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1115 | rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr, |
| 1116 | GPIOF_OUT_INIT_LOW, "slp_tr"); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1117 | if (rc) |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1118 | return rc; |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* Reset */ |
Alexander Aring | 3fa2757 | 2014-03-15 09:29:06 +0100 | [diff] [blame] | 1122 | if (gpio_is_valid(pdata->rstn)) { |
| 1123 | udelay(1); |
| 1124 | gpio_set_value(pdata->rstn, 0); |
| 1125 | udelay(1); |
| 1126 | gpio_set_value(pdata->rstn, 1); |
| 1127 | usleep_range(120, 240); |
| 1128 | } |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1129 | |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1130 | dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops); |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1131 | if (!dev) |
| 1132 | return -ENOMEM; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1133 | |
| 1134 | lp = dev->priv; |
| 1135 | lp->dev = dev; |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1136 | lp->part = part; |
| 1137 | lp->vers = version; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1138 | |
| 1139 | lp->spi = spi; |
| 1140 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1141 | dev->parent = &spi->dev; |
| 1142 | dev->extra_tx_headroom = 0; |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1143 | dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK | |
| 1144 | IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA; |
| 1145 | |
| 1146 | rc = __at86rf230_detect_device(spi, &man_id, &part, &version); |
| 1147 | if (rc < 0) |
| 1148 | goto free_dev; |
| 1149 | |
| 1150 | if (man_id != 0x001f) { |
| 1151 | dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n", |
| 1152 | man_id >> 8, man_id & 0xFF); |
| 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | |
| 1156 | switch (part) { |
| 1157 | case 2: |
| 1158 | chip = "at86rf230"; |
| 1159 | rc = -ENOTSUPP; |
| 1160 | /* FIXME: should be easy to support; */ |
| 1161 | break; |
| 1162 | case 3: |
| 1163 | chip = "at86rf231"; |
| 1164 | break; |
| 1165 | case 7: |
| 1166 | chip = "at86rf212"; |
| 1167 | if (version == 1) |
| 1168 | dev->flags |= IEEE802154_HW_LBT; |
| 1169 | else |
| 1170 | rc = -ENOTSUPP; |
| 1171 | break; |
| 1172 | case 11: |
| 1173 | chip = "at86rf233"; |
| 1174 | break; |
| 1175 | default: |
| 1176 | chip = "UNKNOWN"; |
| 1177 | rc = -ENOTSUPP; |
| 1178 | break; |
| 1179 | } |
| 1180 | |
| 1181 | dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version); |
| 1182 | if (rc < 0) |
| 1183 | goto free_dev; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1184 | |
Alexander Aring | f76014f77 | 2014-07-03 00:20:44 +0200 | [diff] [blame^] | 1185 | lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config); |
| 1186 | if (IS_ERR(lp->regmap)) { |
| 1187 | rc = PTR_ERR(lp->regmap); |
| 1188 | dev_err(&spi->dev, "Failed to allocate register map: %d\n", |
| 1189 | rc); |
| 1190 | goto free_dev; |
| 1191 | } |
| 1192 | |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1193 | irq_type = irq_get_trigger_type(spi->irq); |
Phoebe Buckheister | 363c2cd | 2014-06-18 16:28:49 +0200 | [diff] [blame] | 1194 | if (!irq_type) |
| 1195 | irq_type = IRQF_TRIGGER_RISING; |
Alexander Aring | 4af619a | 2014-04-24 19:09:05 +0200 | [diff] [blame] | 1196 | if (irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1197 | irq_worker = at86rf230_irqwork; |
| 1198 | irq_handler = at86rf230_isr; |
| 1199 | } else { |
| 1200 | irq_worker = at86rf230_irqwork_level; |
| 1201 | irq_handler = at86rf230_isr_level; |
| 1202 | } |
| 1203 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1204 | mutex_init(&lp->bmux); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1205 | INIT_WORK(&lp->irqwork, irq_worker); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1206 | spin_lock_init(&lp->lock); |
| 1207 | init_completion(&lp->tx_complete); |
| 1208 | |
| 1209 | spi_set_drvdata(spi, lp); |
| 1210 | |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1211 | if (is_rf212(lp)) { |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1212 | dev->phy->channels_supported[0] = 0x00007FF; |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1213 | dev->phy->channels_supported[2] = 0x00007FF; |
| 1214 | } else { |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1215 | dev->phy->channels_supported[0] = 0x7FFF800; |
Phoebe Buckheister | 643e53c | 2014-02-17 11:34:09 +0100 | [diff] [blame] | 1216 | } |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1217 | |
| 1218 | rc = at86rf230_hw_init(lp); |
| 1219 | if (rc) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1220 | goto err_hw_init; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1221 | |
Alexander Aring | 1962694 | 2014-04-24 19:09:15 +0200 | [diff] [blame] | 1222 | /* Read irq status register to reset irq line */ |
| 1223 | 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] | 1224 | if (rc) |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1225 | goto err_hw_init; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1226 | |
Phoebe Buckheister | 363c2cd | 2014-06-18 16:28:49 +0200 | [diff] [blame] | 1227 | rc = devm_request_irq(&spi->dev, spi->irq, irq_handler, |
| 1228 | IRQF_SHARED | irq_type, |
Alexander Aring | 1962694 | 2014-04-24 19:09:15 +0200 | [diff] [blame] | 1229 | dev_name(&spi->dev), lp); |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1230 | if (rc) |
Alexander Aring | 652355c | 2014-04-24 19:09:08 +0200 | [diff] [blame] | 1231 | goto err_hw_init; |
Sascha Herrmann | 057dad6 | 2013-04-14 22:33:29 +0000 | [diff] [blame] | 1232 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1233 | rc = ieee802154_register_device(lp->dev); |
| 1234 | if (rc) |
Alexander Aring | 652355c | 2014-04-24 19:09:08 +0200 | [diff] [blame] | 1235 | goto err_hw_init; |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1236 | |
| 1237 | return rc; |
| 1238 | |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1239 | err_hw_init: |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1240 | flush_work(&lp->irqwork); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1241 | mutex_destroy(&lp->bmux); |
Alexander Aring | 640985e | 2014-07-03 00:20:43 +0200 | [diff] [blame] | 1242 | free_dev: |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1243 | ieee802154_free_device(lp->dev); |
Phoebe Buckheister | 8fad346 | 2014-02-17 11:34:06 +0100 | [diff] [blame] | 1244 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1245 | return rc; |
| 1246 | } |
| 1247 | |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1248 | static int at86rf230_remove(struct spi_device *spi) |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1249 | { |
| 1250 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
| 1251 | |
Alexander Aring | 17e84a9 | 2014-03-31 03:26:51 +0200 | [diff] [blame] | 1252 | /* mask all at86rf230 irq's */ |
| 1253 | at86rf230_write_subreg(lp, SR_IRQ_MASK, 0); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1254 | ieee802154_unregister_device(lp->dev); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1255 | flush_work(&lp->irqwork); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1256 | mutex_destroy(&lp->bmux); |
| 1257 | ieee802154_free_device(lp->dev); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1258 | dev_dbg(&spi->dev, "unregistered at86rf230\n"); |
Alexander Aring | 0679e29 | 2014-04-24 19:09:09 +0200 | [diff] [blame] | 1259 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
Alexander Aring | 1086b4f | 2014-04-24 19:09:11 +0200 | [diff] [blame] | 1263 | static const struct of_device_id at86rf230_of_match[] = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1264 | { .compatible = "atmel,at86rf230", }, |
| 1265 | { .compatible = "atmel,at86rf231", }, |
| 1266 | { .compatible = "atmel,at86rf233", }, |
| 1267 | { .compatible = "atmel,at86rf212", }, |
| 1268 | { }, |
| 1269 | }; |
Alexander Aring | 835cb7d | 2014-04-24 19:09:10 +0200 | [diff] [blame] | 1270 | MODULE_DEVICE_TABLE(of, at86rf230_of_match); |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1271 | |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1272 | static const struct spi_device_id at86rf230_device_id[] = { |
| 1273 | { .name = "at86rf230", }, |
| 1274 | { .name = "at86rf231", }, |
| 1275 | { .name = "at86rf233", }, |
| 1276 | { .name = "at86rf212", }, |
| 1277 | { }, |
| 1278 | }; |
| 1279 | MODULE_DEVICE_TABLE(spi, at86rf230_device_id); |
| 1280 | |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1281 | static struct spi_driver at86rf230_driver = { |
Alexander Aring | 90b1552 | 2014-04-24 19:09:12 +0200 | [diff] [blame] | 1282 | .id_table = at86rf230_device_id, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1283 | .driver = { |
Alexander Aring | fa2d3e9 | 2014-03-15 09:29:07 +0100 | [diff] [blame] | 1284 | .of_match_table = of_match_ptr(at86rf230_of_match), |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1285 | .name = "at86rf230", |
| 1286 | .owner = THIS_MODULE, |
| 1287 | }, |
| 1288 | .probe = at86rf230_probe, |
Bill Pemberton | bb1f460 | 2012-12-03 09:24:12 -0500 | [diff] [blame] | 1289 | .remove = at86rf230_remove, |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1290 | }; |
| 1291 | |
alex.bluesman.smirnov@gmail.com | 395a573 | 2012-08-26 05:10:10 +0000 | [diff] [blame] | 1292 | module_spi_driver(at86rf230_driver); |
alex.bluesman.smirnov@gmail.com | 7b8e19b | 2012-06-25 23:24:53 +0000 | [diff] [blame] | 1293 | |
| 1294 | MODULE_DESCRIPTION("AT86RF230 Transceiver Driver"); |
| 1295 | MODULE_LICENSE("GPL v2"); |