1 // SPDX-License-Identifier: GPL-2.0-only
3 * AT86RF230/RF231 driver
5 * Copyright (C) 2009-2012 Siemens AG
8 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10 * Alexander Aring <aar@pengutronix.de>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/hrtimer.h>
15 #include <linux/jiffies.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/gpio.h>
19 #include <linux/delay.h>
20 #include <linux/property.h>
21 #include <linux/spi/spi.h>
22 #include <linux/regmap.h>
23 #include <linux/skbuff.h>
24 #include <linux/of_gpio.h>
25 #include <linux/ieee802154.h>
27 #include <net/mac802154.h>
28 #include <net/cfg802154.h>
30 #include "at86rf230.h"
32 struct at86rf230_local;
33 /* at86rf2xx chip depend data.
34 * All timings are in us.
36 struct at86rf2xx_chip_data {
48 int (*set_channel)(struct at86rf230_local *, u8, u8);
49 int (*set_txpower)(struct at86rf230_local *, s32);
52 #define AT86RF2XX_MAX_BUF (127 + 3)
53 /* tx retries to access the TX_ON state
54 * if it's above then force change will be started.
56 * We assume the max_frame_retries (7) value of 802.15.4 here.
58 #define AT86RF2XX_MAX_TX_RETRIES 7
59 /* We use the recommended 5 minutes timeout to recalibrate */
60 #define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
62 struct at86rf230_state_change {
63 struct at86rf230_local *lp;
67 struct spi_message msg;
68 struct spi_transfer trx;
69 u8 buf[AT86RF2XX_MAX_BUF];
71 void (*complete)(void *context);
79 struct at86rf230_local {
80 struct spi_device *spi;
82 struct ieee802154_hw *hw;
83 struct at86rf2xx_chip_data *data;
84 struct regmap *regmap;
85 struct gpio_desc *slp_tr;
88 struct completion state_complete;
89 struct at86rf230_state_change state;
91 unsigned long cal_timeout;
96 struct sk_buff *tx_skb;
97 struct at86rf230_state_change tx;
100 #define AT86RF2XX_NUMREGS 0x3F
103 at86rf230_async_state_change(struct at86rf230_local *lp,
104 struct at86rf230_state_change *ctx,
105 const u8 state, void (*complete)(void *context));
108 at86rf230_sleep(struct at86rf230_local *lp)
111 gpiod_set_value(lp->slp_tr, 1);
112 usleep_range(lp->data->t_off_to_sleep,
113 lp->data->t_off_to_sleep + 10);
119 at86rf230_awake(struct at86rf230_local *lp)
122 gpiod_set_value(lp->slp_tr, 0);
123 usleep_range(lp->data->t_sleep_to_off,
124 lp->data->t_sleep_to_off + 100);
130 __at86rf230_write(struct at86rf230_local *lp,
131 unsigned int addr, unsigned int data)
133 bool sleep = lp->sleep;
136 /* awake for register setting if sleep */
140 ret = regmap_write(lp->regmap, addr, data);
142 /* sleep again if was sleeping */
150 __at86rf230_read(struct at86rf230_local *lp,
151 unsigned int addr, unsigned int *data)
153 bool sleep = lp->sleep;
156 /* awake for register setting if sleep */
160 ret = regmap_read(lp->regmap, addr, data);
162 /* sleep again if was sleeping */
170 at86rf230_read_subreg(struct at86rf230_local *lp,
171 unsigned int addr, unsigned int mask,
172 unsigned int shift, unsigned int *data)
176 rc = __at86rf230_read(lp, addr, data);
178 *data = (*data & mask) >> shift;
184 at86rf230_write_subreg(struct at86rf230_local *lp,
185 unsigned int addr, unsigned int mask,
186 unsigned int shift, unsigned int data)
188 bool sleep = lp->sleep;
191 /* awake for register setting if sleep */
195 ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
197 /* sleep again if was sleeping */
205 at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
207 gpiod_set_value(lp->slp_tr, 1);
209 gpiod_set_value(lp->slp_tr, 0);
213 at86rf230_reg_writeable(struct device *dev, unsigned int reg)
220 case RG_PHY_ED_LEVEL:
236 case RG_SHORT_ADDR_0:
237 case RG_SHORT_ADDR_1:
259 at86rf230_reg_readable(struct device *dev, unsigned int reg)
263 /* all writeable are also readable */
264 rc = at86rf230_reg_writeable(dev, reg);
284 at86rf230_reg_volatile(struct device *dev, unsigned int reg)
286 /* can be changed during runtime */
291 case RG_PHY_ED_LEVEL:
303 at86rf230_reg_precious(struct device *dev, unsigned int reg)
305 /* don't clear irq line on read */
314 static const struct regmap_config at86rf230_regmap_spi_config = {
317 .write_flag_mask = CMD_REG | CMD_WRITE,
318 .read_flag_mask = CMD_REG,
319 .cache_type = REGCACHE_RBTREE,
320 .max_register = AT86RF2XX_NUMREGS,
321 .writeable_reg = at86rf230_reg_writeable,
322 .readable_reg = at86rf230_reg_readable,
323 .volatile_reg = at86rf230_reg_volatile,
324 .precious_reg = at86rf230_reg_precious,
328 at86rf230_async_error_recover_complete(void *context)
330 struct at86rf230_state_change *ctx = context;
331 struct at86rf230_local *lp = ctx->lp;
338 ieee802154_xmit_hw_error(lp->hw, lp->tx_skb);
343 at86rf230_async_error_recover(void *context)
345 struct at86rf230_state_change *ctx = context;
346 struct at86rf230_local *lp = ctx->lp;
353 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
354 at86rf230_async_error_recover_complete);
358 at86rf230_async_error(struct at86rf230_local *lp,
359 struct at86rf230_state_change *ctx, int rc)
361 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
363 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
364 at86rf230_async_error_recover);
367 /* Generic function to get some register value in async mode */
369 at86rf230_async_read_reg(struct at86rf230_local *lp, u8 reg,
370 struct at86rf230_state_change *ctx,
371 void (*complete)(void *context))
375 u8 *tx_buf = ctx->buf;
377 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
378 ctx->msg.complete = complete;
379 rc = spi_async(lp->spi, &ctx->msg);
381 at86rf230_async_error(lp, ctx, rc);
385 at86rf230_async_write_reg(struct at86rf230_local *lp, u8 reg, u8 val,
386 struct at86rf230_state_change *ctx,
387 void (*complete)(void *context))
391 ctx->buf[0] = (reg & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
393 ctx->msg.complete = complete;
394 rc = spi_async(lp->spi, &ctx->msg);
396 at86rf230_async_error(lp, ctx, rc);
400 at86rf230_async_state_assert(void *context)
402 struct at86rf230_state_change *ctx = context;
403 struct at86rf230_local *lp = ctx->lp;
404 const u8 *buf = ctx->buf;
405 const u8 trx_state = buf[1] & TRX_STATE_MASK;
407 /* Assert state change */
408 if (trx_state != ctx->to_state) {
409 /* Special handling if transceiver state is in
410 * STATE_BUSY_RX_AACK and a SHR was detected.
412 if (trx_state == STATE_BUSY_RX_AACK) {
413 /* Undocumented race condition. If we send a state
414 * change to STATE_RX_AACK_ON the transceiver could
415 * change his state automatically to STATE_BUSY_RX_AACK
416 * if a SHR was detected. This is not an error, but we
419 if (ctx->to_state == STATE_RX_AACK_ON)
422 /* If we change to STATE_TX_ON without forcing and
423 * transceiver state is STATE_BUSY_RX_AACK, we wait
424 * 'tFrame + tPAck' receiving time. In this time the
425 * PDU should be received. If the transceiver is still
426 * in STATE_BUSY_RX_AACK, we run a force state change
427 * to STATE_TX_ON. This is a timeout handling, if the
428 * transceiver stucks in STATE_BUSY_RX_AACK.
430 * Additional we do several retries to try to get into
431 * TX_ON state without forcing. If the retries are
432 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
433 * will do a force change.
435 if (ctx->to_state == STATE_TX_ON ||
436 ctx->to_state == STATE_TRX_OFF) {
437 u8 state = ctx->to_state;
439 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
440 state = STATE_FORCE_TRX_OFF;
443 at86rf230_async_state_change(lp, ctx, state,
449 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
450 ctx->from_state, ctx->to_state, trx_state);
455 ctx->complete(context);
458 static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
460 struct at86rf230_state_change *ctx =
461 container_of(timer, struct at86rf230_state_change, timer);
462 struct at86rf230_local *lp = ctx->lp;
464 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
465 at86rf230_async_state_assert);
467 return HRTIMER_NORESTART;
470 /* Do state change timing delay. */
472 at86rf230_async_state_delay(void *context)
474 struct at86rf230_state_change *ctx = context;
475 struct at86rf230_local *lp = ctx->lp;
476 struct at86rf2xx_chip_data *c = lp->data;
480 /* The force state changes are will show as normal states in the
481 * state status subregister. We change the to_state to the
482 * corresponding one and remember if it was a force change, this
483 * differs if we do a state change from STATE_BUSY_RX_AACK.
485 switch (ctx->to_state) {
486 case STATE_FORCE_TX_ON:
487 ctx->to_state = STATE_TX_ON;
490 case STATE_FORCE_TRX_OFF:
491 ctx->to_state = STATE_TRX_OFF;
498 switch (ctx->from_state) {
500 switch (ctx->to_state) {
501 case STATE_RX_AACK_ON:
502 tim = c->t_off_to_aack * NSEC_PER_USEC;
503 /* state change from TRX_OFF to RX_AACK_ON to do a
504 * calibration, we need to reset the timeout for the
507 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
509 case STATE_TX_ARET_ON:
511 tim = c->t_off_to_tx_on * NSEC_PER_USEC;
512 /* state change from TRX_OFF to TX_ON or ARET_ON to do
513 * a calibration, we need to reset the timeout for the
516 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
522 case STATE_BUSY_RX_AACK:
523 switch (ctx->to_state) {
526 /* Wait for worst case receiving time if we
527 * didn't make a force change from BUSY_RX_AACK
528 * to TX_ON or TRX_OFF.
531 tim = (c->t_frame + c->t_p_ack) * NSEC_PER_USEC;
539 /* Default value, means RESET state */
541 switch (ctx->to_state) {
543 tim = c->t_reset_to_off * NSEC_PER_USEC;
553 /* Default delay is 1us in the most cases */
555 at86rf230_async_state_timer(&ctx->timer);
559 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
563 at86rf230_async_state_change_start(void *context)
565 struct at86rf230_state_change *ctx = context;
566 struct at86rf230_local *lp = ctx->lp;
568 const u8 trx_state = buf[1] & TRX_STATE_MASK;
570 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
571 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
573 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
574 at86rf230_async_state_change_start);
578 /* Check if we already are in the state which we change in */
579 if (trx_state == ctx->to_state) {
581 ctx->complete(context);
585 /* Set current state to the context of state change */
586 ctx->from_state = trx_state;
588 /* Going into the next step for a state change which do a timing
591 at86rf230_async_write_reg(lp, RG_TRX_STATE, ctx->to_state, ctx,
592 at86rf230_async_state_delay);
596 at86rf230_async_state_change(struct at86rf230_local *lp,
597 struct at86rf230_state_change *ctx,
598 const u8 state, void (*complete)(void *context))
600 /* Initialization for the state change context */
601 ctx->to_state = state;
602 ctx->complete = complete;
603 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
604 at86rf230_async_state_change_start);
608 at86rf230_sync_state_change_complete(void *context)
610 struct at86rf230_state_change *ctx = context;
611 struct at86rf230_local *lp = ctx->lp;
613 complete(&lp->state_complete);
616 /* This function do a sync framework above the async state change.
617 * Some callbacks of the IEEE 802.15.4 driver interface need to be
618 * handled synchronously.
621 at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
625 at86rf230_async_state_change(lp, &lp->state, state,
626 at86rf230_sync_state_change_complete);
628 rc = wait_for_completion_timeout(&lp->state_complete,
629 msecs_to_jiffies(100));
631 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
639 at86rf230_tx_complete(void *context)
641 struct at86rf230_state_change *ctx = context;
642 struct at86rf230_local *lp = ctx->lp;
644 if (ctx->trac == IEEE802154_SUCCESS)
645 ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
647 ieee802154_xmit_error(lp->hw, lp->tx_skb, ctx->trac);
653 at86rf230_tx_on(void *context)
655 struct at86rf230_state_change *ctx = context;
656 struct at86rf230_local *lp = ctx->lp;
658 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
659 at86rf230_tx_complete);
663 at86rf230_tx_trac_check(void *context)
665 struct at86rf230_state_change *ctx = context;
666 struct at86rf230_local *lp = ctx->lp;
667 u8 trac = TRAC_MASK(ctx->buf[1]);
671 case TRAC_SUCCESS_DATA_PENDING:
672 ctx->trac = IEEE802154_SUCCESS;
674 case TRAC_CHANNEL_ACCESS_FAILURE:
675 ctx->trac = IEEE802154_CHANNEL_ACCESS_FAILURE;
678 ctx->trac = IEEE802154_NO_ACK;
681 ctx->trac = IEEE802154_SYSTEM_ERROR;
684 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, at86rf230_tx_on);
688 at86rf230_rx_read_frame_complete(void *context)
690 struct at86rf230_state_change *ctx = context;
691 struct at86rf230_local *lp = ctx->lp;
692 const u8 *buf = ctx->buf;
697 if (!ieee802154_is_valid_psdu_len(len)) {
698 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
699 len = IEEE802154_MTU;
703 skb = dev_alloc_skb(IEEE802154_MTU);
705 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
710 skb_put_data(skb, buf + 2, len);
711 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
716 at86rf230_rx_trac_check(void *context)
718 struct at86rf230_state_change *ctx = context;
719 struct at86rf230_local *lp = ctx->lp;
724 ctx->trx.len = AT86RF2XX_MAX_BUF;
725 ctx->msg.complete = at86rf230_rx_read_frame_complete;
726 rc = spi_async(lp->spi, &ctx->msg);
729 at86rf230_async_error(lp, ctx, rc);
734 at86rf230_irq_trx_end(void *context)
736 struct at86rf230_state_change *ctx = context;
737 struct at86rf230_local *lp = ctx->lp;
741 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
742 at86rf230_tx_trac_check);
744 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
745 at86rf230_rx_trac_check);
750 at86rf230_irq_status(void *context)
752 struct at86rf230_state_change *ctx = context;
753 struct at86rf230_local *lp = ctx->lp;
754 const u8 *buf = ctx->buf;
757 enable_irq(lp->spi->irq);
759 if (irq & IRQ_TRX_END) {
760 at86rf230_irq_trx_end(ctx);
762 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
769 at86rf230_setup_spi_messages(struct at86rf230_local *lp,
770 struct at86rf230_state_change *state)
773 state->irq = lp->spi->irq;
774 spi_message_init(&state->msg);
775 state->msg.context = state;
777 state->trx.tx_buf = state->buf;
778 state->trx.rx_buf = state->buf;
779 spi_message_add_tail(&state->trx, &state->msg);
780 hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
781 state->timer.function = at86rf230_async_state_timer;
784 static irqreturn_t at86rf230_isr(int irq, void *data)
786 struct at86rf230_local *lp = data;
787 struct at86rf230_state_change *ctx;
790 disable_irq_nosync(irq);
792 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
798 at86rf230_setup_spi_messages(lp, ctx);
799 /* tell on error handling to free ctx */
802 ctx->buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
803 ctx->msg.complete = at86rf230_irq_status;
804 rc = spi_async(lp->spi, &ctx->msg);
806 at86rf230_async_error(lp, ctx, rc);
815 at86rf230_write_frame_complete(void *context)
817 struct at86rf230_state_change *ctx = context;
818 struct at86rf230_local *lp = ctx->lp;
823 at86rf230_slp_tr_rising_edge(lp);
825 at86rf230_async_write_reg(lp, RG_TRX_STATE, STATE_BUSY_TX, ctx,
830 at86rf230_write_frame(void *context)
832 struct at86rf230_state_change *ctx = context;
833 struct at86rf230_local *lp = ctx->lp;
834 struct sk_buff *skb = lp->tx_skb;
840 buf[0] = CMD_FB | CMD_WRITE;
841 buf[1] = skb->len + 2;
842 memcpy(buf + 2, skb->data, skb->len);
843 ctx->trx.len = skb->len + 2;
844 ctx->msg.complete = at86rf230_write_frame_complete;
845 rc = spi_async(lp->spi, &ctx->msg);
848 at86rf230_async_error(lp, ctx, rc);
853 at86rf230_xmit_tx_on(void *context)
855 struct at86rf230_state_change *ctx = context;
856 struct at86rf230_local *lp = ctx->lp;
858 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
859 at86rf230_write_frame);
863 at86rf230_xmit_start(void *context)
865 struct at86rf230_state_change *ctx = context;
866 struct at86rf230_local *lp = ctx->lp;
868 /* check if we change from off state */
869 if (lp->is_tx_from_off)
870 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
871 at86rf230_write_frame);
873 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
874 at86rf230_xmit_tx_on);
878 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
880 struct at86rf230_local *lp = hw->priv;
881 struct at86rf230_state_change *ctx = &lp->tx;
886 /* After 5 minutes in PLL and the same frequency we run again the
887 * calibration loops which is recommended by at86rf2xx datasheets.
889 * The calibration is initiate by a state change from TRX_OFF
890 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
891 * function then to start in the next 5 minutes.
893 if (time_is_before_jiffies(lp->cal_timeout)) {
894 lp->is_tx_from_off = true;
895 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
896 at86rf230_xmit_start);
898 lp->is_tx_from_off = false;
899 at86rf230_xmit_start(ctx);
906 at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
914 at86rf230_start(struct ieee802154_hw *hw)
916 struct at86rf230_local *lp = hw->priv;
919 enable_irq(lp->spi->irq);
921 return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
925 at86rf230_stop(struct ieee802154_hw *hw)
927 struct at86rf230_local *lp = hw->priv;
930 at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
932 disable_irq(lp->spi->irq);
934 /* It's recommended to set random new csma_seeds before sleep state.
935 * Makes only sense in the stop callback, not doing this inside of
936 * at86rf230_sleep, this is also used when we don't transmit afterwards
937 * when calling start callback again.
939 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
940 at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
941 at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
947 at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
949 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
952 #define AT86RF2XX_MAX_ED_LEVELS 0xF
953 static const s32 at86rf233_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
954 -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, -7800, -7600,
955 -7400, -7200, -7000, -6800, -6600, -6400,
958 static const s32 at86rf231_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
959 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
960 -7100, -6900, -6700, -6500, -6300, -6100,
963 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
964 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
965 -8000, -7800, -7600, -7400, -7200, -7000,
968 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
969 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
970 -7800, -7600, -7400, -7200, -7000, -6800,
974 at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
976 unsigned int cca_ed_thres;
979 rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
983 switch (rssi_base_val) {
985 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
986 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
987 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
990 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
991 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
992 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
1002 at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
1007 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1009 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1014 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
1015 lp->data->rssi_base_val = -100;
1017 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
1018 lp->data->rssi_base_val = -98;
1023 rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
1027 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1031 at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1033 struct at86rf230_local *lp = hw->priv;
1036 rc = lp->data->set_channel(lp, page, channel);
1038 usleep_range(lp->data->t_channel_switch,
1039 lp->data->t_channel_switch + 10);
1041 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
1046 at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1047 struct ieee802154_hw_addr_filt *filt,
1048 unsigned long changed)
1050 struct at86rf230_local *lp = hw->priv;
1052 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1053 u16 addr = le16_to_cpu(filt->short_addr);
1055 dev_vdbg(&lp->spi->dev, "%s called for saddr\n", __func__);
1056 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1057 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1060 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1061 u16 pan = le16_to_cpu(filt->pan_id);
1063 dev_vdbg(&lp->spi->dev, "%s called for pan id\n", __func__);
1064 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1065 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1068 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1071 memcpy(addr, &filt->ieee_addr, 8);
1072 dev_vdbg(&lp->spi->dev, "%s called for IEEE addr\n", __func__);
1073 for (i = 0; i < 8; i++)
1074 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1077 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1078 dev_vdbg(&lp->spi->dev, "%s called for panc change\n", __func__);
1079 if (filt->pan_coord)
1080 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1082 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1088 #define AT86RF23X_MAX_TX_POWERS 0xF
1089 static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1090 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1094 static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1095 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1099 #define AT86RF212_MAX_TX_POWERS 0x1F
1100 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1101 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1102 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1103 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1107 at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1111 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1112 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1113 return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1120 at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1124 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1125 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1126 return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1133 at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
1135 struct at86rf230_local *lp = hw->priv;
1137 return lp->data->set_txpower(lp, mbm);
1141 at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1143 struct at86rf230_local *lp = hw->priv;
1145 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1149 at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1150 const struct wpan_phy_cca *cca)
1152 struct at86rf230_local *lp = hw->priv;
1155 /* mapping 802.15.4 to driver spec */
1156 switch (cca->mode) {
1157 case NL802154_CCA_ENERGY:
1160 case NL802154_CCA_CARRIER:
1163 case NL802154_CCA_ENERGY_CARRIER:
1165 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1168 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1179 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1183 at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
1185 struct at86rf230_local *lp = hw->priv;
1188 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1189 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1190 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1197 at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1200 struct at86rf230_local *lp = hw->priv;
1203 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1207 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1211 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1215 at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1217 struct at86rf230_local *lp = hw->priv;
1219 return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1223 at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1225 struct at86rf230_local *lp = hw->priv;
1229 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1233 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1237 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1241 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1249 static const struct ieee802154_ops at86rf230_ops = {
1250 .owner = THIS_MODULE,
1251 .xmit_async = at86rf230_xmit,
1253 .set_channel = at86rf230_channel,
1254 .start = at86rf230_start,
1255 .stop = at86rf230_stop,
1256 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
1257 .set_txpower = at86rf230_set_txpower,
1258 .set_lbt = at86rf230_set_lbt,
1259 .set_cca_mode = at86rf230_set_cca_mode,
1260 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1261 .set_csma_params = at86rf230_set_csma_params,
1262 .set_frame_retries = at86rf230_set_frame_retries,
1263 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
1266 static struct at86rf2xx_chip_data at86rf233_data = {
1267 .t_sleep_cycle = 330,
1268 .t_channel_switch = 11,
1269 .t_reset_to_off = 26,
1270 .t_off_to_aack = 80,
1271 .t_off_to_tx_on = 80,
1272 .t_off_to_sleep = 35,
1273 .t_sleep_to_off = 1000,
1276 .rssi_base_val = -94,
1277 .set_channel = at86rf23x_set_channel,
1278 .set_txpower = at86rf23x_set_txpower,
1281 static struct at86rf2xx_chip_data at86rf231_data = {
1282 .t_sleep_cycle = 330,
1283 .t_channel_switch = 24,
1284 .t_reset_to_off = 37,
1285 .t_off_to_aack = 110,
1286 .t_off_to_tx_on = 110,
1287 .t_off_to_sleep = 35,
1288 .t_sleep_to_off = 1000,
1291 .rssi_base_val = -91,
1292 .set_channel = at86rf23x_set_channel,
1293 .set_txpower = at86rf23x_set_txpower,
1296 static struct at86rf2xx_chip_data at86rf212_data = {
1297 .t_sleep_cycle = 330,
1298 .t_channel_switch = 11,
1299 .t_reset_to_off = 26,
1300 .t_off_to_aack = 200,
1301 .t_off_to_tx_on = 200,
1302 .t_off_to_sleep = 35,
1303 .t_sleep_to_off = 1000,
1306 .rssi_base_val = -100,
1307 .set_channel = at86rf212_set_channel,
1308 .set_txpower = at86rf212_set_txpower,
1311 static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1313 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
1317 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1321 irq_type = irq_get_trigger_type(lp->spi->irq);
1322 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1323 irq_type == IRQ_TYPE_LEVEL_LOW)
1324 irq_pol = IRQ_ACTIVE_LOW;
1326 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1330 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1334 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1338 /* reset values differs in at86rf231 and at86rf233 */
1339 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1343 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1344 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1347 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1351 /* CLKM changes are applied immediately */
1352 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1357 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1360 /* Wait the next SLEEP cycle */
1361 usleep_range(lp->data->t_sleep_cycle,
1362 lp->data->t_sleep_cycle + 100);
1364 /* xtal_trim value is calculated by:
1365 * CL = 0.5 * (CX + CTRIM + CPAR)
1368 * CL = capacitor of used crystal
1369 * CX = connected capacitors at xtal pins
1370 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1371 * but this is different on each board setup. You need to fine
1372 * tuning this value via CTRIM.
1373 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1377 * atben transceiver:
1381 * CPAR = 3 pF (We assume the magic constant from datasheet)
1384 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1388 * openlabs transceiver:
1392 * CPAR = 3 pF (We assume the magic constant from datasheet)
1395 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1399 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1403 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1407 dev_err(&lp->spi->dev, "DVDD error\n");
1411 /* Force setting slotted operation bit to 0. Sometimes the atben
1412 * sets this bit and I don't know why. We set this always force
1413 * to zero while probing.
1415 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1419 at86rf230_detect_device(struct at86rf230_local *lp)
1421 unsigned int part, version, val;
1426 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1431 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1434 man_id |= (val << 8);
1436 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1440 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1444 if (man_id != 0x001f) {
1445 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1446 man_id >> 8, man_id & 0xFF);
1450 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
1451 IEEE802154_HW_CSMA_PARAMS |
1452 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1453 IEEE802154_HW_PROMISCUOUS;
1455 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1456 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1457 WPAN_PHY_FLAG_CCA_MODE;
1459 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1460 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1461 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1462 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1464 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1473 lp->data = &at86rf231_data;
1474 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1475 lp->hw->phy->current_channel = 11;
1476 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1477 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
1478 lp->hw->phy->supported.cca_ed_levels = at86rf231_ed_levels;
1479 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf231_ed_levels);
1483 lp->data = &at86rf212_data;
1484 lp->hw->flags |= IEEE802154_HW_LBT;
1485 lp->hw->phy->supported.channels[0] = 0x00007FF;
1486 lp->hw->phy->supported.channels[2] = 0x00007FF;
1487 lp->hw->phy->current_channel = 5;
1488 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
1489 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1490 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
1491 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1492 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1496 lp->data = &at86rf233_data;
1497 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1498 lp->hw->phy->current_channel = 13;
1499 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1500 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
1501 lp->hw->phy->supported.cca_ed_levels = at86rf233_ed_levels;
1502 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf233_ed_levels);
1510 lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
1511 lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
1514 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1519 static int at86rf230_probe(struct spi_device *spi)
1521 struct ieee802154_hw *hw;
1522 struct at86rf230_local *lp;
1523 struct gpio_desc *slp_tr;
1524 struct gpio_desc *rstn;
1525 unsigned int status;
1530 dev_err(&spi->dev, "no IRQ specified\n");
1534 rc = device_property_read_u8(&spi->dev, "xtal-trim", &xtal_trim);
1536 if (rc != -EINVAL) {
1538 "failed to parse xtal-trim: %d\n", rc);
1544 rstn = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW);
1545 rc = PTR_ERR_OR_ZERO(rstn);
1549 gpiod_set_consumer_name(rstn, "rstn");
1551 slp_tr = devm_gpiod_get_optional(&spi->dev, "sleep", GPIOD_OUT_LOW);
1552 rc = PTR_ERR_OR_ZERO(slp_tr);
1556 gpiod_set_consumer_name(slp_tr, "slp_tr");
1561 gpiod_set_value_cansleep(rstn, 1);
1563 gpiod_set_value_cansleep(rstn, 0);
1564 usleep_range(120, 240);
1567 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1574 lp->slp_tr = slp_tr;
1575 hw->parent = &spi->dev;
1576 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1578 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1579 if (IS_ERR(lp->regmap)) {
1580 rc = PTR_ERR(lp->regmap);
1581 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1586 at86rf230_setup_spi_messages(lp, &lp->state);
1587 at86rf230_setup_spi_messages(lp, &lp->tx);
1589 rc = at86rf230_detect_device(lp);
1593 init_completion(&lp->state_complete);
1595 spi_set_drvdata(spi, lp);
1597 rc = at86rf230_hw_init(lp, xtal_trim);
1601 /* Read irq status register to reset irq line */
1602 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1606 irq_type = irq_get_trigger_type(spi->irq);
1608 irq_type = IRQF_TRIGGER_HIGH;
1610 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1611 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1615 /* disable_irq by default and wait for starting hardware */
1616 disable_irq(spi->irq);
1618 /* going into sleep by default */
1619 at86rf230_sleep(lp);
1621 rc = ieee802154_register_hw(lp->hw);
1628 ieee802154_free_hw(lp->hw);
1633 static void at86rf230_remove(struct spi_device *spi)
1635 struct at86rf230_local *lp = spi_get_drvdata(spi);
1637 /* mask all at86rf230 irq's */
1638 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
1639 ieee802154_unregister_hw(lp->hw);
1640 ieee802154_free_hw(lp->hw);
1641 dev_dbg(&spi->dev, "unregistered at86rf230\n");
1644 static const struct of_device_id at86rf230_of_match[] = {
1645 { .compatible = "atmel,at86rf230", },
1646 { .compatible = "atmel,at86rf231", },
1647 { .compatible = "atmel,at86rf233", },
1648 { .compatible = "atmel,at86rf212", },
1651 MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1653 static const struct spi_device_id at86rf230_device_id[] = {
1654 { .name = "at86rf230", },
1655 { .name = "at86rf231", },
1656 { .name = "at86rf233", },
1657 { .name = "at86rf212", },
1660 MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1662 static struct spi_driver at86rf230_driver = {
1663 .id_table = at86rf230_device_id,
1665 .of_match_table = at86rf230_of_match,
1666 .name = "at86rf230",
1668 .probe = at86rf230_probe,
1669 .remove = at86rf230_remove,
1672 module_spi_driver(at86rf230_driver);
1674 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1675 MODULE_LICENSE("GPL v2");