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