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