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