2 * AT86RF230/RF231 driver
4 * Copyright (C) 2009-2012 Siemens AG
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.
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.
16 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
18 * Alexander Aring <aar@pengutronix.de>
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>
35 #include <net/mac802154.h>
36 #include <net/cfg802154.h>
38 #include "at86rf230.h"
40 struct at86rf230_local
;
41 /* at86rf2xx chip depend data.
42 * All timings are in us.
44 struct at86rf2xx_chip_data
{
56 int (*set_channel
)(struct at86rf230_local
*, u8
, u8
);
57 int (*set_txpower
)(struct at86rf230_local
*, s32
);
60 #define AT86RF2XX_MAX_BUF (127 + 3)
61 /* tx retries to access the TX_ON state
62 * if it's above then force change will be started.
64 * We assume the max_frame_retries (7) value of 802.15.4 here.
66 #define AT86RF2XX_MAX_TX_RETRIES 7
67 /* We use the recommended 5 minutes timeout to recalibrate */
68 #define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
70 struct at86rf230_state_change
{
71 struct at86rf230_local
*lp
;
75 struct spi_message msg
;
76 struct spi_transfer trx
;
77 u8 buf
[AT86RF2XX_MAX_BUF
];
79 void (*complete
)(void *context
);
86 struct at86rf230_local
{
87 struct spi_device
*spi
;
89 struct ieee802154_hw
*hw
;
90 struct at86rf2xx_chip_data
*data
;
91 struct regmap
*regmap
;
94 struct completion state_complete
;
95 struct at86rf230_state_change state
;
97 struct at86rf230_state_change irq
;
100 unsigned long cal_timeout
;
101 s8 max_frame_retries
;
105 struct sk_buff
*tx_skb
;
106 struct at86rf230_state_change tx
;
109 #define AT86RF2XX_NUMREGS 0x3F
112 at86rf230_async_state_change(struct at86rf230_local
*lp
,
113 struct at86rf230_state_change
*ctx
,
114 const u8 state
, void (*complete
)(void *context
),
115 const bool irq_enable
);
118 __at86rf230_write(struct at86rf230_local
*lp
,
119 unsigned int addr
, unsigned int data
)
121 return regmap_write(lp
->regmap
, addr
, data
);
125 __at86rf230_read(struct at86rf230_local
*lp
,
126 unsigned int addr
, unsigned int *data
)
128 return regmap_read(lp
->regmap
, addr
, data
);
132 at86rf230_read_subreg(struct at86rf230_local
*lp
,
133 unsigned int addr
, unsigned int mask
,
134 unsigned int shift
, unsigned int *data
)
138 rc
= __at86rf230_read(lp
, addr
, data
);
140 *data
= (*data
& mask
) >> shift
;
146 at86rf230_write_subreg(struct at86rf230_local
*lp
,
147 unsigned int addr
, unsigned int mask
,
148 unsigned int shift
, unsigned int data
)
150 return regmap_update_bits(lp
->regmap
, addr
, mask
, data
<< shift
);
154 at86rf230_slp_tr_rising_edge(struct at86rf230_local
*lp
)
156 gpio_set_value(lp
->slp_tr
, 1);
158 gpio_set_value(lp
->slp_tr
, 0);
162 at86rf230_reg_writeable(struct device
*dev
, unsigned int reg
)
169 case RG_PHY_ED_LEVEL
:
185 case RG_SHORT_ADDR_0
:
186 case RG_SHORT_ADDR_1
:
208 at86rf230_reg_readable(struct device
*dev
, unsigned int reg
)
212 /* all writeable are also readable */
213 rc
= at86rf230_reg_writeable(dev
, reg
);
233 at86rf230_reg_volatile(struct device
*dev
, unsigned int reg
)
235 /* can be changed during runtime */
240 case RG_PHY_ED_LEVEL
:
252 at86rf230_reg_precious(struct device
*dev
, unsigned int reg
)
254 /* don't clear irq line on read */
263 static const struct regmap_config at86rf230_regmap_spi_config
= {
266 .write_flag_mask
= CMD_REG
| CMD_WRITE
,
267 .read_flag_mask
= CMD_REG
,
268 .cache_type
= REGCACHE_RBTREE
,
269 .max_register
= AT86RF2XX_NUMREGS
,
270 .writeable_reg
= at86rf230_reg_writeable
,
271 .readable_reg
= at86rf230_reg_readable
,
272 .volatile_reg
= at86rf230_reg_volatile
,
273 .precious_reg
= at86rf230_reg_precious
,
277 at86rf230_async_error_recover(void *context
)
279 struct at86rf230_state_change
*ctx
= context
;
280 struct at86rf230_local
*lp
= ctx
->lp
;
283 at86rf230_async_state_change(lp
, ctx
, STATE_RX_AACK_ON
, NULL
, false);
284 ieee802154_wake_queue(lp
->hw
);
288 at86rf230_async_error(struct at86rf230_local
*lp
,
289 struct at86rf230_state_change
*ctx
, int rc
)
291 dev_err(&lp
->spi
->dev
, "spi_async error %d\n", rc
);
293 at86rf230_async_state_change(lp
, ctx
, STATE_FORCE_TRX_OFF
,
294 at86rf230_async_error_recover
, false);
297 /* Generic function to get some register value in async mode */
299 at86rf230_async_read_reg(struct at86rf230_local
*lp
, const u8 reg
,
300 struct at86rf230_state_change
*ctx
,
301 void (*complete
)(void *context
),
302 const bool irq_enable
)
306 u8
*tx_buf
= ctx
->buf
;
308 tx_buf
[0] = (reg
& CMD_REG_MASK
) | CMD_REG
;
309 ctx
->msg
.complete
= complete
;
310 ctx
->irq_enable
= irq_enable
;
311 rc
= spi_async(lp
->spi
, &ctx
->msg
);
314 enable_irq(ctx
->irq
);
316 at86rf230_async_error(lp
, ctx
, rc
);
320 static inline u8
at86rf230_state_to_force(u8 state
)
322 if (state
== STATE_TX_ON
)
323 return STATE_FORCE_TX_ON
;
325 return STATE_FORCE_TRX_OFF
;
329 at86rf230_async_state_assert(void *context
)
331 struct at86rf230_state_change
*ctx
= context
;
332 struct at86rf230_local
*lp
= ctx
->lp
;
333 const u8
*buf
= ctx
->buf
;
334 const u8 trx_state
= buf
[1] & TRX_STATE_MASK
;
336 /* Assert state change */
337 if (trx_state
!= ctx
->to_state
) {
338 /* Special handling if transceiver state is in
339 * STATE_BUSY_RX_AACK and a SHR was detected.
341 if (trx_state
== STATE_BUSY_RX_AACK
) {
342 /* Undocumented race condition. If we send a state
343 * change to STATE_RX_AACK_ON the transceiver could
344 * change his state automatically to STATE_BUSY_RX_AACK
345 * if a SHR was detected. This is not an error, but we
348 if (ctx
->to_state
== STATE_RX_AACK_ON
)
351 /* If we change to STATE_TX_ON without forcing and
352 * transceiver state is STATE_BUSY_RX_AACK, we wait
353 * 'tFrame + tPAck' receiving time. In this time the
354 * PDU should be received. If the transceiver is still
355 * in STATE_BUSY_RX_AACK, we run a force state change
356 * to STATE_TX_ON. This is a timeout handling, if the
357 * transceiver stucks in STATE_BUSY_RX_AACK.
359 * Additional we do several retries to try to get into
360 * TX_ON state without forcing. If the retries are
361 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
362 * will do a force change.
364 if (ctx
->to_state
== STATE_TX_ON
||
365 ctx
->to_state
== STATE_TRX_OFF
) {
366 u8 state
= ctx
->to_state
;
368 if (lp
->tx_retry
>= AT86RF2XX_MAX_TX_RETRIES
)
369 state
= at86rf230_state_to_force(state
);
372 at86rf230_async_state_change(lp
, ctx
, state
,
379 dev_warn(&lp
->spi
->dev
, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
380 ctx
->from_state
, ctx
->to_state
, trx_state
);
385 ctx
->complete(context
);
388 static enum hrtimer_restart
at86rf230_async_state_timer(struct hrtimer
*timer
)
390 struct at86rf230_state_change
*ctx
=
391 container_of(timer
, struct at86rf230_state_change
, timer
);
392 struct at86rf230_local
*lp
= ctx
->lp
;
394 at86rf230_async_read_reg(lp
, RG_TRX_STATUS
, ctx
,
395 at86rf230_async_state_assert
,
398 return HRTIMER_NORESTART
;
401 /* Do state change timing delay. */
403 at86rf230_async_state_delay(void *context
)
405 struct at86rf230_state_change
*ctx
= context
;
406 struct at86rf230_local
*lp
= ctx
->lp
;
407 struct at86rf2xx_chip_data
*c
= lp
->data
;
411 /* The force state changes are will show as normal states in the
412 * state status subregister. We change the to_state to the
413 * corresponding one and remember if it was a force change, this
414 * differs if we do a state change from STATE_BUSY_RX_AACK.
416 switch (ctx
->to_state
) {
417 case STATE_FORCE_TX_ON
:
418 ctx
->to_state
= STATE_TX_ON
;
421 case STATE_FORCE_TRX_OFF
:
422 ctx
->to_state
= STATE_TRX_OFF
;
429 switch (ctx
->from_state
) {
431 switch (ctx
->to_state
) {
432 case STATE_RX_AACK_ON
:
433 tim
= ktime_set(0, c
->t_off_to_aack
* NSEC_PER_USEC
);
434 /* state change from TRX_OFF to RX_AACK_ON to do a
435 * calibration, we need to reset the timeout for the
438 lp
->cal_timeout
= jiffies
+ AT86RF2XX_CAL_LOOP_TIMEOUT
;
440 case STATE_TX_ARET_ON
:
442 tim
= ktime_set(0, c
->t_off_to_tx_on
* NSEC_PER_USEC
);
443 /* state change from TRX_OFF to TX_ON or ARET_ON to do
444 * a calibration, we need to reset the timeout for the
447 lp
->cal_timeout
= jiffies
+ AT86RF2XX_CAL_LOOP_TIMEOUT
;
453 case STATE_BUSY_RX_AACK
:
454 switch (ctx
->to_state
) {
457 /* Wait for worst case receiving time if we
458 * didn't make a force change from BUSY_RX_AACK
459 * to TX_ON or TRX_OFF.
462 tim
= ktime_set(0, (c
->t_frame
+ c
->t_p_ack
) *
471 /* Default value, means RESET state */
473 switch (ctx
->to_state
) {
475 tim
= ktime_set(0, c
->t_reset_to_off
* NSEC_PER_USEC
);
485 /* Default delay is 1us in the most cases */
486 tim
= ktime_set(0, NSEC_PER_USEC
);
489 hrtimer_start(&ctx
->timer
, tim
, HRTIMER_MODE_REL
);
493 at86rf230_async_state_change_start(void *context
)
495 struct at86rf230_state_change
*ctx
= context
;
496 struct at86rf230_local
*lp
= ctx
->lp
;
498 const u8 trx_state
= buf
[1] & TRX_STATE_MASK
;
501 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
502 if (trx_state
== STATE_TRANSITION_IN_PROGRESS
) {
504 at86rf230_async_read_reg(lp
, RG_TRX_STATUS
, ctx
,
505 at86rf230_async_state_change_start
,
510 /* Check if we already are in the state which we change in */
511 if (trx_state
== ctx
->to_state
) {
513 ctx
->complete(context
);
517 /* Set current state to the context of state change */
518 ctx
->from_state
= trx_state
;
520 /* Going into the next step for a state change which do a timing
523 buf
[0] = (RG_TRX_STATE
& CMD_REG_MASK
) | CMD_REG
| CMD_WRITE
;
524 buf
[1] = ctx
->to_state
;
525 ctx
->msg
.complete
= at86rf230_async_state_delay
;
526 rc
= spi_async(lp
->spi
, &ctx
->msg
);
529 enable_irq(ctx
->irq
);
531 at86rf230_async_error(lp
, ctx
, rc
);
536 at86rf230_async_state_change(struct at86rf230_local
*lp
,
537 struct at86rf230_state_change
*ctx
,
538 const u8 state
, void (*complete
)(void *context
),
539 const bool irq_enable
)
541 /* Initialization for the state change context */
542 ctx
->to_state
= state
;
543 ctx
->complete
= complete
;
544 ctx
->irq_enable
= irq_enable
;
545 at86rf230_async_read_reg(lp
, RG_TRX_STATUS
, ctx
,
546 at86rf230_async_state_change_start
,
551 at86rf230_sync_state_change_complete(void *context
)
553 struct at86rf230_state_change
*ctx
= context
;
554 struct at86rf230_local
*lp
= ctx
->lp
;
556 complete(&lp
->state_complete
);
559 /* This function do a sync framework above the async state change.
560 * Some callbacks of the IEEE 802.15.4 driver interface need to be
561 * handled synchronously.
564 at86rf230_sync_state_change(struct at86rf230_local
*lp
, unsigned int state
)
568 at86rf230_async_state_change(lp
, &lp
->state
, state
,
569 at86rf230_sync_state_change_complete
,
572 rc
= wait_for_completion_timeout(&lp
->state_complete
,
573 msecs_to_jiffies(100));
575 at86rf230_async_error(lp
, &lp
->state
, -ETIMEDOUT
);
583 at86rf230_tx_complete(void *context
)
585 struct at86rf230_state_change
*ctx
= context
;
586 struct at86rf230_local
*lp
= ctx
->lp
;
588 enable_irq(ctx
->irq
);
590 ieee802154_xmit_complete(lp
->hw
, lp
->tx_skb
, !lp
->tx_aret
);
594 at86rf230_tx_on(void *context
)
596 struct at86rf230_state_change
*ctx
= context
;
597 struct at86rf230_local
*lp
= ctx
->lp
;
599 at86rf230_async_state_change(lp
, ctx
, STATE_RX_AACK_ON
,
600 at86rf230_tx_complete
, true);
604 at86rf230_tx_trac_check(void *context
)
606 struct at86rf230_state_change
*ctx
= context
;
607 struct at86rf230_local
*lp
= ctx
->lp
;
608 const u8
*buf
= ctx
->buf
;
609 const u8 trac
= (buf
[1] & 0xe0) >> 5;
611 /* If trac status is different than zero we need to do a state change
612 * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the
616 at86rf230_async_state_change(lp
, ctx
, STATE_FORCE_TRX_OFF
,
617 at86rf230_tx_on
, true);
619 at86rf230_tx_on(context
);
623 at86rf230_tx_trac_status(void *context
)
625 struct at86rf230_state_change
*ctx
= context
;
626 struct at86rf230_local
*lp
= ctx
->lp
;
628 at86rf230_async_read_reg(lp
, RG_TRX_STATE
, ctx
,
629 at86rf230_tx_trac_check
, true);
633 at86rf230_rx_read_frame_complete(void *context
)
635 struct at86rf230_state_change
*ctx
= context
;
636 struct at86rf230_local
*lp
= ctx
->lp
;
637 u8 rx_local_buf
[AT86RF2XX_MAX_BUF
];
638 const u8
*buf
= ctx
->buf
;
643 if (!ieee802154_is_valid_psdu_len(len
)) {
644 dev_vdbg(&lp
->spi
->dev
, "corrupted frame received\n");
645 len
= IEEE802154_MTU
;
649 memcpy(rx_local_buf
, buf
+ 2, len
);
651 enable_irq(ctx
->irq
);
653 skb
= dev_alloc_skb(IEEE802154_MTU
);
655 dev_vdbg(&lp
->spi
->dev
, "failed to allocate sk_buff\n");
659 memcpy(skb_put(skb
, len
), rx_local_buf
, len
);
660 ieee802154_rx_irqsafe(lp
->hw
, skb
, lqi
);
664 at86rf230_rx_read_frame(void *context
)
666 struct at86rf230_state_change
*ctx
= context
;
667 struct at86rf230_local
*lp
= ctx
->lp
;
672 ctx
->trx
.len
= AT86RF2XX_MAX_BUF
;
673 ctx
->msg
.complete
= at86rf230_rx_read_frame_complete
;
674 rc
= spi_async(lp
->spi
, &ctx
->msg
);
677 enable_irq(ctx
->irq
);
678 at86rf230_async_error(lp
, ctx
, rc
);
683 at86rf230_rx_trac_check(void *context
)
685 /* Possible check on trac status here. This could be useful to make
686 * some stats why receive is failed. Not used at the moment, but it's
687 * maybe timing relevant. Datasheet doesn't say anything about this.
688 * The programming guide say do it so.
691 at86rf230_rx_read_frame(context
);
695 at86rf230_irq_trx_end(struct at86rf230_local
*lp
)
701 at86rf230_async_state_change(lp
, &lp
->irq
,
703 at86rf230_tx_trac_status
,
706 at86rf230_async_state_change(lp
, &lp
->irq
,
708 at86rf230_tx_complete
,
711 at86rf230_async_read_reg(lp
, RG_TRX_STATE
, &lp
->irq
,
712 at86rf230_rx_trac_check
, true);
717 at86rf230_irq_status(void *context
)
719 struct at86rf230_state_change
*ctx
= context
;
720 struct at86rf230_local
*lp
= ctx
->lp
;
721 const u8
*buf
= ctx
->buf
;
722 const u8 irq
= buf
[1];
724 if (irq
& IRQ_TRX_END
) {
725 at86rf230_irq_trx_end(lp
);
727 enable_irq(ctx
->irq
);
728 dev_err(&lp
->spi
->dev
, "not supported irq %02x received\n",
733 static irqreturn_t
at86rf230_isr(int irq
, void *data
)
735 struct at86rf230_local
*lp
= data
;
736 struct at86rf230_state_change
*ctx
= &lp
->irq
;
740 disable_irq_nosync(irq
);
742 buf
[0] = (RG_IRQ_STATUS
& CMD_REG_MASK
) | CMD_REG
;
743 ctx
->msg
.complete
= at86rf230_irq_status
;
744 rc
= spi_async(lp
->spi
, &ctx
->msg
);
747 at86rf230_async_error(lp
, ctx
, rc
);
755 at86rf230_write_frame_complete(void *context
)
757 struct at86rf230_state_change
*ctx
= context
;
758 struct at86rf230_local
*lp
= ctx
->lp
;
764 if (gpio_is_valid(lp
->slp_tr
)) {
765 at86rf230_slp_tr_rising_edge(lp
);
767 buf
[0] = (RG_TRX_STATE
& CMD_REG_MASK
) | CMD_REG
| CMD_WRITE
;
768 buf
[1] = STATE_BUSY_TX
;
769 ctx
->msg
.complete
= NULL
;
770 rc
= spi_async(lp
->spi
, &ctx
->msg
);
772 at86rf230_async_error(lp
, ctx
, rc
);
777 at86rf230_write_frame(void *context
)
779 struct at86rf230_state_change
*ctx
= context
;
780 struct at86rf230_local
*lp
= ctx
->lp
;
781 struct sk_buff
*skb
= lp
->tx_skb
;
787 buf
[0] = CMD_FB
| CMD_WRITE
;
788 buf
[1] = skb
->len
+ 2;
789 memcpy(buf
+ 2, skb
->data
, skb
->len
);
790 ctx
->trx
.len
= skb
->len
+ 2;
791 ctx
->msg
.complete
= at86rf230_write_frame_complete
;
792 rc
= spi_async(lp
->spi
, &ctx
->msg
);
795 at86rf230_async_error(lp
, ctx
, rc
);
800 at86rf230_xmit_tx_on(void *context
)
802 struct at86rf230_state_change
*ctx
= context
;
803 struct at86rf230_local
*lp
= ctx
->lp
;
805 at86rf230_async_state_change(lp
, ctx
, STATE_TX_ARET_ON
,
806 at86rf230_write_frame
, false);
810 at86rf230_xmit_start(void *context
)
812 struct at86rf230_state_change
*ctx
= context
;
813 struct at86rf230_local
*lp
= ctx
->lp
;
815 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
816 * are in STATE_TX_ON. The pfad differs here, so we change
817 * the complete handler.
820 if (lp
->is_tx_from_off
) {
821 lp
->is_tx_from_off
= false;
822 at86rf230_async_state_change(lp
, ctx
, STATE_TX_ARET_ON
,
823 at86rf230_write_frame
,
826 at86rf230_async_state_change(lp
, ctx
, STATE_TX_ON
,
827 at86rf230_xmit_tx_on
,
831 at86rf230_async_state_change(lp
, ctx
, STATE_TX_ON
,
832 at86rf230_write_frame
, false);
837 at86rf230_xmit(struct ieee802154_hw
*hw
, struct sk_buff
*skb
)
839 struct at86rf230_local
*lp
= hw
->priv
;
840 struct at86rf230_state_change
*ctx
= &lp
->tx
;
845 /* After 5 minutes in PLL and the same frequency we run again the
846 * calibration loops which is recommended by at86rf2xx datasheets.
848 * The calibration is initiate by a state change from TRX_OFF
849 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
850 * function then to start in the next 5 minutes.
852 if (time_is_before_jiffies(lp
->cal_timeout
)) {
853 lp
->is_tx_from_off
= true;
854 at86rf230_async_state_change(lp
, ctx
, STATE_TRX_OFF
,
855 at86rf230_xmit_start
, false);
857 at86rf230_xmit_start(ctx
);
864 at86rf230_ed(struct ieee802154_hw
*hw
, u8
*level
)
872 at86rf230_start(struct ieee802154_hw
*hw
)
874 struct at86rf230_local
*lp
= hw
->priv
;
876 if (gpio_is_valid(lp
->slp_tr
)) {
877 gpio_set_value(lp
->slp_tr
, 0);
878 usleep_range(lp
->data
->t_sleep_to_off
,
879 lp
->data
->t_sleep_to_off
+ 100);
882 enable_irq(lp
->spi
->irq
);
884 return at86rf230_sync_state_change(hw
->priv
, STATE_RX_AACK_ON
);
888 at86rf230_stop(struct ieee802154_hw
*hw
)
890 struct at86rf230_local
*lp
= hw
->priv
;
892 at86rf230_sync_state_change(hw
->priv
, STATE_FORCE_TRX_OFF
);
894 disable_irq(lp
->spi
->irq
);
896 if (gpio_is_valid(lp
->slp_tr
)) {
897 gpio_set_value(lp
->slp_tr
, 1);
898 usleep_range(lp
->data
->t_off_to_sleep
,
899 lp
->data
->t_off_to_sleep
+ 10);
904 at86rf23x_set_channel(struct at86rf230_local
*lp
, u8 page
, u8 channel
)
906 return at86rf230_write_subreg(lp
, SR_CHANNEL
, channel
);
909 #define AT86RF2XX_MAX_ED_LEVELS 0xF
910 static const s32 at86rf23x_ed_levels
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
911 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
912 -7100, -6900, -6700, -6500, -6300, -6100,
915 static const s32 at86rf212_ed_levels_100
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
916 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
917 -8000, -7800, -7600, -7400, -7200, -7000,
920 static const s32 at86rf212_ed_levels_98
[AT86RF2XX_MAX_ED_LEVELS
+ 1] = {
921 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
922 -7800, -7600, -7400, -7200, -7000, -6800,
926 at86rf212_update_cca_ed_level(struct at86rf230_local
*lp
, int rssi_base_val
)
928 unsigned int cca_ed_thres
;
931 rc
= at86rf230_read_subreg(lp
, SR_CCA_ED_THRES
, &cca_ed_thres
);
935 switch (rssi_base_val
) {
937 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_98
;
938 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_98
);
939 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_98
[cca_ed_thres
];
942 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
943 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
944 lp
->hw
->phy
->cca_ed_level
= at86rf212_ed_levels_100
[cca_ed_thres
];
954 at86rf212_set_channel(struct at86rf230_local
*lp
, u8 page
, u8 channel
)
959 rc
= at86rf230_write_subreg(lp
, SR_SUB_MODE
, 0);
961 rc
= at86rf230_write_subreg(lp
, SR_SUB_MODE
, 1);
966 rc
= at86rf230_write_subreg(lp
, SR_BPSK_QPSK
, 0);
967 lp
->data
->rssi_base_val
= -100;
969 rc
= at86rf230_write_subreg(lp
, SR_BPSK_QPSK
, 1);
970 lp
->data
->rssi_base_val
= -98;
975 rc
= at86rf212_update_cca_ed_level(lp
, lp
->data
->rssi_base_val
);
979 /* This sets the symbol_duration according frequency on the 212.
980 * TODO move this handling while set channel and page in cfg802154.
981 * We can do that, this timings are according 802.15.4 standard.
982 * If we do that in cfg802154, this is a more generic calculation.
984 * This should also protected from ifs_timer. Means cancel timer and
985 * init with a new value. For now, this is okay.
989 /* SUB:0 and BPSK:0 -> BPSK-20 */
990 lp
->hw
->phy
->symbol_duration
= 50;
992 /* SUB:1 and BPSK:0 -> BPSK-40 */
993 lp
->hw
->phy
->symbol_duration
= 25;
997 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
998 lp
->hw
->phy
->symbol_duration
= 40;
1000 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1001 lp
->hw
->phy
->symbol_duration
= 16;
1004 lp
->hw
->phy
->lifs_period
= IEEE802154_LIFS_PERIOD
*
1005 lp
->hw
->phy
->symbol_duration
;
1006 lp
->hw
->phy
->sifs_period
= IEEE802154_SIFS_PERIOD
*
1007 lp
->hw
->phy
->symbol_duration
;
1009 return at86rf230_write_subreg(lp
, SR_CHANNEL
, channel
);
1013 at86rf230_channel(struct ieee802154_hw
*hw
, u8 page
, u8 channel
)
1015 struct at86rf230_local
*lp
= hw
->priv
;
1018 rc
= lp
->data
->set_channel(lp
, page
, channel
);
1020 usleep_range(lp
->data
->t_channel_switch
,
1021 lp
->data
->t_channel_switch
+ 10);
1023 lp
->cal_timeout
= jiffies
+ AT86RF2XX_CAL_LOOP_TIMEOUT
;
1028 at86rf230_set_hw_addr_filt(struct ieee802154_hw
*hw
,
1029 struct ieee802154_hw_addr_filt
*filt
,
1030 unsigned long changed
)
1032 struct at86rf230_local
*lp
= hw
->priv
;
1034 if (changed
& IEEE802154_AFILT_SADDR_CHANGED
) {
1035 u16 addr
= le16_to_cpu(filt
->short_addr
);
1037 dev_vdbg(&lp
->spi
->dev
,
1038 "at86rf230_set_hw_addr_filt called for saddr\n");
1039 __at86rf230_write(lp
, RG_SHORT_ADDR_0
, addr
);
1040 __at86rf230_write(lp
, RG_SHORT_ADDR_1
, addr
>> 8);
1043 if (changed
& IEEE802154_AFILT_PANID_CHANGED
) {
1044 u16 pan
= le16_to_cpu(filt
->pan_id
);
1046 dev_vdbg(&lp
->spi
->dev
,
1047 "at86rf230_set_hw_addr_filt called for pan id\n");
1048 __at86rf230_write(lp
, RG_PAN_ID_0
, pan
);
1049 __at86rf230_write(lp
, RG_PAN_ID_1
, pan
>> 8);
1052 if (changed
& IEEE802154_AFILT_IEEEADDR_CHANGED
) {
1055 memcpy(addr
, &filt
->ieee_addr
, 8);
1056 dev_vdbg(&lp
->spi
->dev
,
1057 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
1058 for (i
= 0; i
< 8; i
++)
1059 __at86rf230_write(lp
, RG_IEEE_ADDR_0
+ i
, addr
[i
]);
1062 if (changed
& IEEE802154_AFILT_PANC_CHANGED
) {
1063 dev_vdbg(&lp
->spi
->dev
,
1064 "at86rf230_set_hw_addr_filt called for panc change\n");
1065 if (filt
->pan_coord
)
1066 at86rf230_write_subreg(lp
, SR_AACK_I_AM_COORD
, 1);
1068 at86rf230_write_subreg(lp
, SR_AACK_I_AM_COORD
, 0);
1074 #define AT86RF23X_MAX_TX_POWERS 0xF
1075 static const s32 at86rf233_powers
[AT86RF23X_MAX_TX_POWERS
+ 1] = {
1076 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1080 static const s32 at86rf231_powers
[AT86RF23X_MAX_TX_POWERS
+ 1] = {
1081 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1085 #define AT86RF212_MAX_TX_POWERS 0x1F
1086 static const s32 at86rf212_powers
[AT86RF212_MAX_TX_POWERS
+ 1] = {
1087 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1088 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1089 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1093 at86rf23x_set_txpower(struct at86rf230_local
*lp
, s32 mbm
)
1097 for (i
= 0; i
< lp
->hw
->phy
->supported
.tx_powers_size
; i
++) {
1098 if (lp
->hw
->phy
->supported
.tx_powers
[i
] == mbm
)
1099 return at86rf230_write_subreg(lp
, SR_TX_PWR_23X
, i
);
1106 at86rf212_set_txpower(struct at86rf230_local
*lp
, s32 mbm
)
1110 for (i
= 0; i
< lp
->hw
->phy
->supported
.tx_powers_size
; i
++) {
1111 if (lp
->hw
->phy
->supported
.tx_powers
[i
] == mbm
)
1112 return at86rf230_write_subreg(lp
, SR_TX_PWR_212
, i
);
1119 at86rf230_set_txpower(struct ieee802154_hw
*hw
, s32 mbm
)
1121 struct at86rf230_local
*lp
= hw
->priv
;
1123 return lp
->data
->set_txpower(lp
, mbm
);
1127 at86rf230_set_lbt(struct ieee802154_hw
*hw
, bool on
)
1129 struct at86rf230_local
*lp
= hw
->priv
;
1131 return at86rf230_write_subreg(lp
, SR_CSMA_LBT_MODE
, on
);
1135 at86rf230_set_cca_mode(struct ieee802154_hw
*hw
,
1136 const struct wpan_phy_cca
*cca
)
1138 struct at86rf230_local
*lp
= hw
->priv
;
1141 /* mapping 802.15.4 to driver spec */
1142 switch (cca
->mode
) {
1143 case NL802154_CCA_ENERGY
:
1146 case NL802154_CCA_CARRIER
:
1149 case NL802154_CCA_ENERGY_CARRIER
:
1151 case NL802154_CCA_OPT_ENERGY_CARRIER_AND
:
1154 case NL802154_CCA_OPT_ENERGY_CARRIER_OR
:
1165 return at86rf230_write_subreg(lp
, SR_CCA_MODE
, val
);
1170 at86rf230_set_cca_ed_level(struct ieee802154_hw
*hw
, s32 mbm
)
1172 struct at86rf230_local
*lp
= hw
->priv
;
1175 for (i
= 0; i
< hw
->phy
->supported
.cca_ed_levels_size
; i
++) {
1176 if (hw
->phy
->supported
.cca_ed_levels
[i
] == mbm
)
1177 return at86rf230_write_subreg(lp
, SR_CCA_ED_THRES
, i
);
1184 at86rf230_set_csma_params(struct ieee802154_hw
*hw
, u8 min_be
, u8 max_be
,
1187 struct at86rf230_local
*lp
= hw
->priv
;
1190 rc
= at86rf230_write_subreg(lp
, SR_MIN_BE
, min_be
);
1194 rc
= at86rf230_write_subreg(lp
, SR_MAX_BE
, max_be
);
1198 return at86rf230_write_subreg(lp
, SR_MAX_CSMA_RETRIES
, retries
);
1202 at86rf230_set_frame_retries(struct ieee802154_hw
*hw
, s8 retries
)
1204 struct at86rf230_local
*lp
= hw
->priv
;
1207 lp
->tx_aret
= retries
>= 0;
1208 lp
->max_frame_retries
= retries
;
1211 rc
= at86rf230_write_subreg(lp
, SR_MAX_FRAME_RETRIES
, retries
);
1217 at86rf230_set_promiscuous_mode(struct ieee802154_hw
*hw
, const bool on
)
1219 struct at86rf230_local
*lp
= hw
->priv
;
1223 rc
= at86rf230_write_subreg(lp
, SR_AACK_DIS_ACK
, 1);
1227 rc
= at86rf230_write_subreg(lp
, SR_AACK_PROM_MODE
, 1);
1231 rc
= at86rf230_write_subreg(lp
, SR_AACK_PROM_MODE
, 0);
1235 rc
= at86rf230_write_subreg(lp
, SR_AACK_DIS_ACK
, 0);
1243 static const struct ieee802154_ops at86rf230_ops
= {
1244 .owner
= THIS_MODULE
,
1245 .xmit_async
= at86rf230_xmit
,
1247 .set_channel
= at86rf230_channel
,
1248 .start
= at86rf230_start
,
1249 .stop
= at86rf230_stop
,
1250 .set_hw_addr_filt
= at86rf230_set_hw_addr_filt
,
1251 .set_txpower
= at86rf230_set_txpower
,
1252 .set_lbt
= at86rf230_set_lbt
,
1253 .set_cca_mode
= at86rf230_set_cca_mode
,
1254 .set_cca_ed_level
= at86rf230_set_cca_ed_level
,
1255 .set_csma_params
= at86rf230_set_csma_params
,
1256 .set_frame_retries
= at86rf230_set_frame_retries
,
1257 .set_promiscuous_mode
= at86rf230_set_promiscuous_mode
,
1260 static struct at86rf2xx_chip_data at86rf233_data
= {
1261 .t_sleep_cycle
= 330,
1262 .t_channel_switch
= 11,
1263 .t_reset_to_off
= 26,
1264 .t_off_to_aack
= 80,
1265 .t_off_to_tx_on
= 80,
1266 .t_off_to_sleep
= 35,
1267 .t_sleep_to_off
= 210,
1270 .rssi_base_val
= -91,
1271 .set_channel
= at86rf23x_set_channel
,
1272 .set_txpower
= at86rf23x_set_txpower
,
1275 static struct at86rf2xx_chip_data at86rf231_data
= {
1276 .t_sleep_cycle
= 330,
1277 .t_channel_switch
= 24,
1278 .t_reset_to_off
= 37,
1279 .t_off_to_aack
= 110,
1280 .t_off_to_tx_on
= 110,
1281 .t_off_to_sleep
= 35,
1282 .t_sleep_to_off
= 380,
1285 .rssi_base_val
= -91,
1286 .set_channel
= at86rf23x_set_channel
,
1287 .set_txpower
= at86rf23x_set_txpower
,
1290 static struct at86rf2xx_chip_data at86rf212_data
= {
1291 .t_sleep_cycle
= 330,
1292 .t_channel_switch
= 11,
1293 .t_reset_to_off
= 26,
1294 .t_off_to_aack
= 200,
1295 .t_off_to_tx_on
= 200,
1296 .t_off_to_sleep
= 35,
1297 .t_sleep_to_off
= 380,
1300 .rssi_base_val
= -100,
1301 .set_channel
= at86rf212_set_channel
,
1302 .set_txpower
= at86rf212_set_txpower
,
1305 static int at86rf230_hw_init(struct at86rf230_local
*lp
, u8 xtal_trim
)
1307 int rc
, irq_type
, irq_pol
= IRQ_ACTIVE_HIGH
;
1311 rc
= at86rf230_sync_state_change(lp
, STATE_FORCE_TRX_OFF
);
1315 irq_type
= irq_get_trigger_type(lp
->spi
->irq
);
1316 if (irq_type
== IRQ_TYPE_EDGE_RISING
||
1317 irq_type
== IRQ_TYPE_EDGE_FALLING
)
1318 dev_warn(&lp
->spi
->dev
,
1319 "Using edge triggered irq's are not recommended!\n");
1320 if (irq_type
== IRQ_TYPE_EDGE_FALLING
||
1321 irq_type
== IRQ_TYPE_LEVEL_LOW
)
1322 irq_pol
= IRQ_ACTIVE_LOW
;
1324 rc
= at86rf230_write_subreg(lp
, SR_IRQ_POLARITY
, irq_pol
);
1328 rc
= at86rf230_write_subreg(lp
, SR_RX_SAFE_MODE
, 1);
1332 rc
= at86rf230_write_subreg(lp
, SR_IRQ_MASK
, IRQ_TRX_END
);
1336 /* reset values differs in at86rf231 and at86rf233 */
1337 rc
= at86rf230_write_subreg(lp
, SR_IRQ_MASK_MODE
, 0);
1341 get_random_bytes(csma_seed
, ARRAY_SIZE(csma_seed
));
1342 rc
= at86rf230_write_subreg(lp
, SR_CSMA_SEED_0
, csma_seed
[0]);
1345 rc
= at86rf230_write_subreg(lp
, SR_CSMA_SEED_1
, csma_seed
[1]);
1349 /* CLKM changes are applied immediately */
1350 rc
= at86rf230_write_subreg(lp
, SR_CLKM_SHA_SEL
, 0x00);
1355 rc
= at86rf230_write_subreg(lp
, SR_CLKM_CTRL
, 0x00);
1358 /* Wait the next SLEEP cycle */
1359 usleep_range(lp
->data
->t_sleep_cycle
,
1360 lp
->data
->t_sleep_cycle
+ 100);
1362 /* xtal_trim value is calculated by:
1363 * CL = 0.5 * (CX + CTRIM + CPAR)
1366 * CL = capacitor of used crystal
1367 * CX = connected capacitors at xtal pins
1368 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1369 * but this is different on each board setup. You need to fine
1370 * tuning this value via CTRIM.
1371 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1375 * atben transceiver:
1379 * CPAR = 3 pF (We assume the magic constant from datasheet)
1382 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1386 * openlabs transceiver:
1390 * CPAR = 3 pF (We assume the magic constant from datasheet)
1393 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1397 rc
= at86rf230_write_subreg(lp
, SR_XTAL_TRIM
, xtal_trim
);
1401 rc
= at86rf230_read_subreg(lp
, SR_DVDD_OK
, &dvdd
);
1405 dev_err(&lp
->spi
->dev
, "DVDD error\n");
1409 /* Force setting slotted operation bit to 0. Sometimes the atben
1410 * sets this bit and I don't know why. We set this always force
1411 * to zero while probing.
1413 return at86rf230_write_subreg(lp
, SR_SLOTTED_OPERATION
, 0);
1417 at86rf230_get_pdata(struct spi_device
*spi
, int *rstn
, int *slp_tr
,
1420 struct at86rf230_platform_data
*pdata
= spi
->dev
.platform_data
;
1423 if (!IS_ENABLED(CONFIG_OF
) || !spi
->dev
.of_node
) {
1427 *rstn
= pdata
->rstn
;
1428 *slp_tr
= pdata
->slp_tr
;
1429 *xtal_trim
= pdata
->xtal_trim
;
1433 *rstn
= of_get_named_gpio(spi
->dev
.of_node
, "reset-gpio", 0);
1434 *slp_tr
= of_get_named_gpio(spi
->dev
.of_node
, "sleep-gpio", 0);
1435 ret
= of_property_read_u8(spi
->dev
.of_node
, "xtal-trim", xtal_trim
);
1436 if (ret
< 0 && ret
!= -EINVAL
)
1443 at86rf230_detect_device(struct at86rf230_local
*lp
)
1445 unsigned int part
, version
, val
;
1450 rc
= __at86rf230_read(lp
, RG_MAN_ID_0
, &val
);
1455 rc
= __at86rf230_read(lp
, RG_MAN_ID_1
, &val
);
1458 man_id
|= (val
<< 8);
1460 rc
= __at86rf230_read(lp
, RG_PART_NUM
, &part
);
1464 rc
= __at86rf230_read(lp
, RG_VERSION_NUM
, &version
);
1468 if (man_id
!= 0x001f) {
1469 dev_err(&lp
->spi
->dev
, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1470 man_id
>> 8, man_id
& 0xFF);
1474 lp
->hw
->flags
= IEEE802154_HW_TX_OMIT_CKSUM
|
1475 IEEE802154_HW_CSMA_PARAMS
|
1476 IEEE802154_HW_FRAME_RETRIES
| IEEE802154_HW_AFILT
|
1477 IEEE802154_HW_PROMISCUOUS
;
1479 lp
->hw
->phy
->flags
= WPAN_PHY_FLAG_TXPOWER
|
1480 WPAN_PHY_FLAG_CCA_ED_LEVEL
|
1481 WPAN_PHY_FLAG_CCA_MODE
;
1483 lp
->hw
->phy
->supported
.cca_modes
= BIT(NL802154_CCA_ENERGY
) |
1484 BIT(NL802154_CCA_CARRIER
) | BIT(NL802154_CCA_ENERGY_CARRIER
);
1485 lp
->hw
->phy
->supported
.cca_opts
= BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND
) |
1486 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR
);
1488 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf23x_ed_levels
;
1489 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf23x_ed_levels
);
1491 lp
->hw
->phy
->cca
.mode
= NL802154_CCA_ENERGY
;
1500 lp
->data
= &at86rf231_data
;
1501 lp
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
1502 lp
->hw
->phy
->current_channel
= 11;
1503 lp
->hw
->phy
->symbol_duration
= 16;
1504 lp
->hw
->phy
->supported
.tx_powers
= at86rf231_powers
;
1505 lp
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(at86rf231_powers
);
1509 lp
->data
= &at86rf212_data
;
1510 lp
->hw
->flags
|= IEEE802154_HW_LBT
;
1511 lp
->hw
->phy
->supported
.channels
[0] = 0x00007FF;
1512 lp
->hw
->phy
->supported
.channels
[2] = 0x00007FF;
1513 lp
->hw
->phy
->current_channel
= 5;
1514 lp
->hw
->phy
->symbol_duration
= 25;
1515 lp
->hw
->phy
->supported
.lbt
= NL802154_SUPPORTED_BOOL_BOTH
;
1516 lp
->hw
->phy
->supported
.tx_powers
= at86rf212_powers
;
1517 lp
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(at86rf212_powers
);
1518 lp
->hw
->phy
->supported
.cca_ed_levels
= at86rf212_ed_levels_100
;
1519 lp
->hw
->phy
->supported
.cca_ed_levels_size
= ARRAY_SIZE(at86rf212_ed_levels_100
);
1523 lp
->data
= &at86rf233_data
;
1524 lp
->hw
->phy
->supported
.channels
[0] = 0x7FFF800;
1525 lp
->hw
->phy
->current_channel
= 13;
1526 lp
->hw
->phy
->symbol_duration
= 16;
1527 lp
->hw
->phy
->supported
.tx_powers
= at86rf233_powers
;
1528 lp
->hw
->phy
->supported
.tx_powers_size
= ARRAY_SIZE(at86rf233_powers
);
1536 lp
->hw
->phy
->cca_ed_level
= lp
->hw
->phy
->supported
.cca_ed_levels
[7];
1537 lp
->hw
->phy
->transmit_power
= lp
->hw
->phy
->supported
.tx_powers
[0];
1540 dev_info(&lp
->spi
->dev
, "Detected %s chip version %d\n", chip
, version
);
1546 at86rf230_setup_spi_messages(struct at86rf230_local
*lp
)
1549 lp
->state
.irq
= lp
->spi
->irq
;
1550 spi_message_init(&lp
->state
.msg
);
1551 lp
->state
.msg
.context
= &lp
->state
;
1552 lp
->state
.trx
.len
= 2;
1553 lp
->state
.trx
.tx_buf
= lp
->state
.buf
;
1554 lp
->state
.trx
.rx_buf
= lp
->state
.buf
;
1555 spi_message_add_tail(&lp
->state
.trx
, &lp
->state
.msg
);
1556 hrtimer_init(&lp
->state
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1557 lp
->state
.timer
.function
= at86rf230_async_state_timer
;
1560 lp
->irq
.irq
= lp
->spi
->irq
;
1561 spi_message_init(&lp
->irq
.msg
);
1562 lp
->irq
.msg
.context
= &lp
->irq
;
1563 lp
->irq
.trx
.len
= 2;
1564 lp
->irq
.trx
.tx_buf
= lp
->irq
.buf
;
1565 lp
->irq
.trx
.rx_buf
= lp
->irq
.buf
;
1566 spi_message_add_tail(&lp
->irq
.trx
, &lp
->irq
.msg
);
1567 hrtimer_init(&lp
->irq
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1568 lp
->irq
.timer
.function
= at86rf230_async_state_timer
;
1571 lp
->tx
.irq
= lp
->spi
->irq
;
1572 spi_message_init(&lp
->tx
.msg
);
1573 lp
->tx
.msg
.context
= &lp
->tx
;
1575 lp
->tx
.trx
.tx_buf
= lp
->tx
.buf
;
1576 lp
->tx
.trx
.rx_buf
= lp
->tx
.buf
;
1577 spi_message_add_tail(&lp
->tx
.trx
, &lp
->tx
.msg
);
1578 hrtimer_init(&lp
->tx
.timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
1579 lp
->tx
.timer
.function
= at86rf230_async_state_timer
;
1582 static int at86rf230_probe(struct spi_device
*spi
)
1584 struct ieee802154_hw
*hw
;
1585 struct at86rf230_local
*lp
;
1586 unsigned int status
;
1587 int rc
, irq_type
, rstn
, slp_tr
;
1591 dev_err(&spi
->dev
, "no IRQ specified\n");
1595 rc
= at86rf230_get_pdata(spi
, &rstn
, &slp_tr
, &xtal_trim
);
1597 dev_err(&spi
->dev
, "failed to parse platform_data: %d\n", rc
);
1601 if (gpio_is_valid(rstn
)) {
1602 rc
= devm_gpio_request_one(&spi
->dev
, rstn
,
1603 GPIOF_OUT_INIT_HIGH
, "rstn");
1608 if (gpio_is_valid(slp_tr
)) {
1609 rc
= devm_gpio_request_one(&spi
->dev
, slp_tr
,
1610 GPIOF_OUT_INIT_LOW
, "slp_tr");
1616 if (gpio_is_valid(rstn
)) {
1618 gpio_set_value(rstn
, 0);
1620 gpio_set_value(rstn
, 1);
1621 usleep_range(120, 240);
1624 hw
= ieee802154_alloc_hw(sizeof(*lp
), &at86rf230_ops
);
1631 lp
->slp_tr
= slp_tr
;
1632 hw
->parent
= &spi
->dev
;
1633 ieee802154_random_extended_addr(&hw
->phy
->perm_extended_addr
);
1635 lp
->regmap
= devm_regmap_init_spi(spi
, &at86rf230_regmap_spi_config
);
1636 if (IS_ERR(lp
->regmap
)) {
1637 rc
= PTR_ERR(lp
->regmap
);
1638 dev_err(&spi
->dev
, "Failed to allocate register map: %d\n",
1643 at86rf230_setup_spi_messages(lp
);
1645 rc
= at86rf230_detect_device(lp
);
1649 init_completion(&lp
->state_complete
);
1651 spi_set_drvdata(spi
, lp
);
1653 rc
= at86rf230_hw_init(lp
, xtal_trim
);
1657 /* Read irq status register to reset irq line */
1658 rc
= at86rf230_read_subreg(lp
, RG_IRQ_STATUS
, 0xff, 0, &status
);
1662 irq_type
= irq_get_trigger_type(spi
->irq
);
1664 irq_type
= IRQF_TRIGGER_HIGH
;
1666 rc
= devm_request_irq(&spi
->dev
, spi
->irq
, at86rf230_isr
,
1667 IRQF_SHARED
| irq_type
, dev_name(&spi
->dev
), lp
);
1671 /* disable_irq by default and wait for starting hardware */
1672 disable_irq(spi
->irq
);
1674 /* going into sleep by default */
1675 if (gpio_is_valid(slp_tr
)) {
1676 gpio_set_value(slp_tr
, 1);
1677 usleep_range(lp
->data
->t_off_to_sleep
,
1678 lp
->data
->t_off_to_sleep
+ 10);
1681 rc
= ieee802154_register_hw(lp
->hw
);
1688 ieee802154_free_hw(lp
->hw
);
1693 static int at86rf230_remove(struct spi_device
*spi
)
1695 struct at86rf230_local
*lp
= spi_get_drvdata(spi
);
1697 /* mask all at86rf230 irq's */
1698 at86rf230_write_subreg(lp
, SR_IRQ_MASK
, 0);
1699 ieee802154_unregister_hw(lp
->hw
);
1700 ieee802154_free_hw(lp
->hw
);
1701 dev_dbg(&spi
->dev
, "unregistered at86rf230\n");
1706 static const struct of_device_id at86rf230_of_match
[] = {
1707 { .compatible
= "atmel,at86rf230", },
1708 { .compatible
= "atmel,at86rf231", },
1709 { .compatible
= "atmel,at86rf233", },
1710 { .compatible
= "atmel,at86rf212", },
1713 MODULE_DEVICE_TABLE(of
, at86rf230_of_match
);
1715 static const struct spi_device_id at86rf230_device_id
[] = {
1716 { .name
= "at86rf230", },
1717 { .name
= "at86rf231", },
1718 { .name
= "at86rf233", },
1719 { .name
= "at86rf212", },
1722 MODULE_DEVICE_TABLE(spi
, at86rf230_device_id
);
1724 static struct spi_driver at86rf230_driver
= {
1725 .id_table
= at86rf230_device_id
,
1727 .of_match_table
= of_match_ptr(at86rf230_of_match
),
1728 .name
= "at86rf230",
1729 .owner
= THIS_MODULE
,
1731 .probe
= at86rf230_probe
,
1732 .remove
= at86rf230_remove
,
1735 module_spi_driver(at86rf230_driver
);
1737 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1738 MODULE_LICENSE("GPL v2");