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