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;
95 struct completion state_complete;
96 struct at86rf230_state_change state;
98 struct at86rf230_state_change irq;
100 unsigned long cal_timeout;
104 struct sk_buff *tx_skb;
105 struct at86rf230_state_change tx;
108 #define AT86RF2XX_NUMREGS 0x3F
111 at86rf230_async_state_change(struct at86rf230_local *lp,
112 struct at86rf230_state_change *ctx,
113 const u8 state, void (*complete)(void *context),
114 const bool irq_enable);
117 at86rf230_sleep(struct at86rf230_local *lp)
119 if (gpio_is_valid(lp->slp_tr)) {
120 gpio_set_value(lp->slp_tr, 1);
121 usleep_range(lp->data->t_off_to_sleep,
122 lp->data->t_off_to_sleep + 10);
128 at86rf230_awake(struct at86rf230_local *lp)
130 if (gpio_is_valid(lp->slp_tr)) {
131 gpio_set_value(lp->slp_tr, 0);
132 usleep_range(lp->data->t_sleep_to_off,
133 lp->data->t_sleep_to_off + 100);
139 __at86rf230_write(struct at86rf230_local *lp,
140 unsigned int addr, unsigned int data)
142 bool sleep = lp->sleep;
145 /* awake for register setting if sleep */
149 ret = regmap_write(lp->regmap, addr, data);
151 /* sleep again if was sleeping */
159 __at86rf230_read(struct at86rf230_local *lp,
160 unsigned int addr, unsigned int *data)
162 bool sleep = lp->sleep;
165 /* awake for register setting if sleep */
169 ret = regmap_read(lp->regmap, addr, data);
171 /* sleep again if was sleeping */
179 at86rf230_read_subreg(struct at86rf230_local *lp,
180 unsigned int addr, unsigned int mask,
181 unsigned int shift, unsigned int *data)
185 rc = __at86rf230_read(lp, addr, data);
187 *data = (*data & mask) >> shift;
193 at86rf230_write_subreg(struct at86rf230_local *lp,
194 unsigned int addr, unsigned int mask,
195 unsigned int shift, unsigned int data)
197 bool sleep = lp->sleep;
200 /* awake for register setting if sleep */
204 ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
206 /* sleep again if was sleeping */
214 at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
216 gpio_set_value(lp->slp_tr, 1);
218 gpio_set_value(lp->slp_tr, 0);
222 at86rf230_reg_writeable(struct device *dev, unsigned int reg)
229 case RG_PHY_ED_LEVEL:
245 case RG_SHORT_ADDR_0:
246 case RG_SHORT_ADDR_1:
268 at86rf230_reg_readable(struct device *dev, unsigned int reg)
272 /* all writeable are also readable */
273 rc = at86rf230_reg_writeable(dev, reg);
293 at86rf230_reg_volatile(struct device *dev, unsigned int reg)
295 /* can be changed during runtime */
300 case RG_PHY_ED_LEVEL:
312 at86rf230_reg_precious(struct device *dev, unsigned int reg)
314 /* don't clear irq line on read */
323 static const struct regmap_config at86rf230_regmap_spi_config = {
326 .write_flag_mask = CMD_REG | CMD_WRITE,
327 .read_flag_mask = CMD_REG,
328 .cache_type = REGCACHE_RBTREE,
329 .max_register = AT86RF2XX_NUMREGS,
330 .writeable_reg = at86rf230_reg_writeable,
331 .readable_reg = at86rf230_reg_readable,
332 .volatile_reg = at86rf230_reg_volatile,
333 .precious_reg = at86rf230_reg_precious,
337 at86rf230_async_error_recover(void *context)
339 struct at86rf230_state_change *ctx = context;
340 struct at86rf230_local *lp = ctx->lp;
343 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
344 ieee802154_wake_queue(lp->hw);
348 at86rf230_async_error(struct at86rf230_local *lp,
349 struct at86rf230_state_change *ctx, int rc)
351 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
353 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
354 at86rf230_async_error_recover, false);
357 /* Generic function to get some register value in async mode */
359 at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
360 struct at86rf230_state_change *ctx,
361 void (*complete)(void *context),
362 const bool irq_enable)
366 u8 *tx_buf = ctx->buf;
368 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
369 ctx->msg.complete = complete;
370 ctx->irq_enable = irq_enable;
371 rc = spi_async(lp->spi, &ctx->msg);
374 enable_irq(ctx->irq);
376 at86rf230_async_error(lp, ctx, rc);
381 at86rf230_async_state_assert(void *context)
383 struct at86rf230_state_change *ctx = context;
384 struct at86rf230_local *lp = ctx->lp;
385 const u8 *buf = ctx->buf;
386 const u8 trx_state = buf[1] & TRX_STATE_MASK;
388 /* Assert state change */
389 if (trx_state != ctx->to_state) {
390 /* Special handling if transceiver state is in
391 * STATE_BUSY_RX_AACK and a SHR was detected.
393 if (trx_state == STATE_BUSY_RX_AACK) {
394 /* Undocumented race condition. If we send a state
395 * change to STATE_RX_AACK_ON the transceiver could
396 * change his state automatically to STATE_BUSY_RX_AACK
397 * if a SHR was detected. This is not an error, but we
400 if (ctx->to_state == STATE_RX_AACK_ON)
403 /* If we change to STATE_TX_ON without forcing and
404 * transceiver state is STATE_BUSY_RX_AACK, we wait
405 * 'tFrame + tPAck' receiving time. In this time the
406 * PDU should be received. If the transceiver is still
407 * in STATE_BUSY_RX_AACK, we run a force state change
408 * to STATE_TX_ON. This is a timeout handling, if the
409 * transceiver stucks in STATE_BUSY_RX_AACK.
411 * Additional we do several retries to try to get into
412 * TX_ON state without forcing. If the retries are
413 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
414 * will do a force change.
416 if (ctx->to_state == STATE_TX_ON ||
417 ctx->to_state == STATE_TRX_OFF) {
418 u8 state = ctx->to_state;
420 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
421 state = STATE_FORCE_TRX_OFF;
424 at86rf230_async_state_change(lp, ctx, state,
431 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
432 ctx->from_state, ctx->to_state, trx_state);
437 ctx->complete(context);
440 static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
442 struct at86rf230_state_change *ctx =
443 container_of(timer, struct at86rf230_state_change, timer);
444 struct at86rf230_local *lp = ctx->lp;
446 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
447 at86rf230_async_state_assert,
450 return HRTIMER_NORESTART;
453 /* Do state change timing delay. */
455 at86rf230_async_state_delay(void *context)
457 struct at86rf230_state_change *ctx = context;
458 struct at86rf230_local *lp = ctx->lp;
459 struct at86rf2xx_chip_data *c = lp->data;
463 /* The force state changes are will show as normal states in the
464 * state status subregister. We change the to_state to the
465 * corresponding one and remember if it was a force change, this
466 * differs if we do a state change from STATE_BUSY_RX_AACK.
468 switch (ctx->to_state) {
469 case STATE_FORCE_TX_ON:
470 ctx->to_state = STATE_TX_ON;
473 case STATE_FORCE_TRX_OFF:
474 ctx->to_state = STATE_TRX_OFF;
481 switch (ctx->from_state) {
483 switch (ctx->to_state) {
484 case STATE_RX_AACK_ON:
485 tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
486 /* state change from TRX_OFF to RX_AACK_ON to do a
487 * calibration, we need to reset the timeout for the
490 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
492 case STATE_TX_ARET_ON:
494 tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
495 /* state change from TRX_OFF to TX_ON or ARET_ON to do
496 * a calibration, we need to reset the timeout for the
499 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
505 case STATE_BUSY_RX_AACK:
506 switch (ctx->to_state) {
509 /* Wait for worst case receiving time if we
510 * didn't make a force change from BUSY_RX_AACK
511 * to TX_ON or TRX_OFF.
514 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
523 /* Default value, means RESET state */
525 switch (ctx->to_state) {
527 tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
537 /* Default delay is 1us in the most cases */
539 at86rf230_async_state_timer(&ctx->timer);
543 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
547 at86rf230_async_state_change_start(void *context)
549 struct at86rf230_state_change *ctx = context;
550 struct at86rf230_local *lp = ctx->lp;
552 const u8 trx_state = buf[1] & TRX_STATE_MASK;
555 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
556 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
558 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
559 at86rf230_async_state_change_start,
564 /* Check if we already are in the state which we change in */
565 if (trx_state == ctx->to_state) {
567 ctx->complete(context);
571 /* Set current state to the context of state change */
572 ctx->from_state = trx_state;
574 /* Going into the next step for a state change which do a timing
577 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
578 buf[1] = ctx->to_state;
579 ctx->msg.complete = at86rf230_async_state_delay;
580 rc = spi_async(lp->spi, &ctx->msg);
583 enable_irq(ctx->irq);
585 at86rf230_async_error(lp, ctx, rc);
590 at86rf230_async_state_change(struct at86rf230_local *lp,
591 struct at86rf230_state_change *ctx,
592 const u8 state, void (*complete)(void *context),
593 const bool irq_enable)
595 /* Initialization for the state change context */
596 ctx->to_state = state;
597 ctx->complete = complete;
598 ctx->irq_enable = irq_enable;
599 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
600 at86rf230_async_state_change_start,
605 at86rf230_sync_state_change_complete(void *context)
607 struct at86rf230_state_change *ctx = context;
608 struct at86rf230_local *lp = ctx->lp;
610 complete(&lp->state_complete);
613 /* This function do a sync framework above the async state change.
614 * Some callbacks of the IEEE 802.15.4 driver interface need to be
615 * handled synchronously.
618 at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
622 at86rf230_async_state_change(lp, &lp->state, state,
623 at86rf230_sync_state_change_complete,
626 rc = wait_for_completion_timeout(&lp->state_complete,
627 msecs_to_jiffies(100));
629 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
637 at86rf230_tx_complete(void *context)
639 struct at86rf230_state_change *ctx = context;
640 struct at86rf230_local *lp = ctx->lp;
642 enable_irq(ctx->irq);
644 ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
648 at86rf230_tx_on(void *context)
650 struct at86rf230_state_change *ctx = context;
651 struct at86rf230_local *lp = ctx->lp;
653 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
654 at86rf230_tx_complete, true);
658 at86rf230_tx_trac_check(void *context)
660 struct at86rf230_state_change *ctx = context;
661 struct at86rf230_local *lp = ctx->lp;
663 at86rf230_async_state_change(lp, &lp->irq, STATE_TX_ON,
664 at86rf230_tx_on, true);
668 at86rf230_rx_read_frame_complete(void *context)
670 struct at86rf230_state_change *ctx = context;
671 struct at86rf230_local *lp = ctx->lp;
672 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
673 const u8 *buf = ctx->buf;
678 if (!ieee802154_is_valid_psdu_len(len)) {
679 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
680 len = IEEE802154_MTU;
684 memcpy(rx_local_buf, buf + 2, len);
686 enable_irq(ctx->irq);
688 skb = dev_alloc_skb(IEEE802154_MTU);
690 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
694 memcpy(skb_put(skb, len), rx_local_buf, len);
695 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
699 at86rf230_rx_read_frame(void *context)
701 struct at86rf230_state_change *ctx = context;
702 struct at86rf230_local *lp = ctx->lp;
707 ctx->trx.len = AT86RF2XX_MAX_BUF;
708 ctx->msg.complete = at86rf230_rx_read_frame_complete;
709 rc = spi_async(lp->spi, &ctx->msg);
712 enable_irq(ctx->irq);
713 at86rf230_async_error(lp, ctx, rc);
718 at86rf230_rx_trac_check(void *context)
720 /* Possible check on trac status here. This could be useful to make
721 * some stats why receive is failed. Not used at the moment, but it's
722 * maybe timing relevant. Datasheet doesn't say anything about this.
723 * The programming guide say do it so.
726 at86rf230_rx_read_frame(context);
730 at86rf230_irq_trx_end(struct at86rf230_local *lp)
734 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
735 at86rf230_tx_trac_check, true);
737 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
738 at86rf230_rx_trac_check, true);
743 at86rf230_irq_status(void *context)
745 struct at86rf230_state_change *ctx = context;
746 struct at86rf230_local *lp = ctx->lp;
747 const u8 *buf = ctx->buf;
748 const u8 irq = buf[1];
750 if (irq & IRQ_TRX_END) {
751 at86rf230_irq_trx_end(lp);
753 enable_irq(ctx->irq);
754 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
759 static irqreturn_t at86rf230_isr(int irq, void *data)
761 struct at86rf230_local *lp = data;
762 struct at86rf230_state_change *ctx = &lp->irq;
766 disable_irq_nosync(irq);
768 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
769 ctx->msg.complete = at86rf230_irq_status;
770 rc = spi_async(lp->spi, &ctx->msg);
773 at86rf230_async_error(lp, ctx, rc);
781 at86rf230_write_frame_complete(void *context)
783 struct at86rf230_state_change *ctx = context;
784 struct at86rf230_local *lp = ctx->lp;
790 if (gpio_is_valid(lp->slp_tr)) {
791 at86rf230_slp_tr_rising_edge(lp);
793 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
794 buf[1] = STATE_BUSY_TX;
795 ctx->msg.complete = NULL;
796 rc = spi_async(lp->spi, &ctx->msg);
798 at86rf230_async_error(lp, ctx, rc);
803 at86rf230_write_frame(void *context)
805 struct at86rf230_state_change *ctx = context;
806 struct at86rf230_local *lp = ctx->lp;
807 struct sk_buff *skb = lp->tx_skb;
813 buf[0] = CMD_FB | CMD_WRITE;
814 buf[1] = skb->len + 2;
815 memcpy(buf + 2, skb->data, skb->len);
816 ctx->trx.len = skb->len + 2;
817 ctx->msg.complete = at86rf230_write_frame_complete;
818 rc = spi_async(lp->spi, &ctx->msg);
821 at86rf230_async_error(lp, ctx, rc);
826 at86rf230_xmit_tx_on(void *context)
828 struct at86rf230_state_change *ctx = context;
829 struct at86rf230_local *lp = ctx->lp;
831 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
832 at86rf230_write_frame, false);
836 at86rf230_xmit_start(void *context)
838 struct at86rf230_state_change *ctx = context;
839 struct at86rf230_local *lp = ctx->lp;
841 /* check if we change from off state */
842 if (lp->is_tx_from_off) {
843 lp->is_tx_from_off = false;
844 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
845 at86rf230_write_frame,
848 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
849 at86rf230_xmit_tx_on,
855 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
857 struct at86rf230_local *lp = hw->priv;
858 struct at86rf230_state_change *ctx = &lp->tx;
863 /* After 5 minutes in PLL and the same frequency we run again the
864 * calibration loops which is recommended by at86rf2xx datasheets.
866 * The calibration is initiate by a state change from TRX_OFF
867 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
868 * function then to start in the next 5 minutes.
870 if (time_is_before_jiffies(lp->cal_timeout)) {
871 lp->is_tx_from_off = true;
872 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
873 at86rf230_xmit_start, false);
875 at86rf230_xmit_start(ctx);
882 at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
890 at86rf230_start(struct ieee802154_hw *hw)
892 struct at86rf230_local *lp = hw->priv;
895 enable_irq(lp->spi->irq);
897 return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
901 at86rf230_stop(struct ieee802154_hw *hw)
903 struct at86rf230_local *lp = hw->priv;
906 at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
908 disable_irq(lp->spi->irq);
910 /* It's recommended to set random new csma_seeds before sleep state.
911 * Makes only sense in the stop callback, not doing this inside of
912 * at86rf230_sleep, this is also used when we don't transmit afterwards
913 * when calling start callback again.
915 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
916 at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
917 at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
923 at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
925 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
928 #define AT86RF2XX_MAX_ED_LEVELS 0xF
929 static const s32 at86rf23x_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
930 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
931 -7100, -6900, -6700, -6500, -6300, -6100,
934 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
935 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
936 -8000, -7800, -7600, -7400, -7200, -7000,
939 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
940 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
941 -7800, -7600, -7400, -7200, -7000, -6800,
945 at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
947 unsigned int cca_ed_thres;
950 rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
954 switch (rssi_base_val) {
956 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
957 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
958 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
961 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
962 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
963 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
973 at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
978 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
980 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
985 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
986 lp->data->rssi_base_val = -100;
988 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
989 lp->data->rssi_base_val = -98;
994 rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
998 /* This sets the symbol_duration according frequency on the 212.
999 * TODO move this handling while set channel and page in cfg802154.
1000 * We can do that, this timings are according 802.15.4 standard.
1001 * If we do that in cfg802154, this is a more generic calculation.
1003 * This should also protected from ifs_timer. Means cancel timer and
1004 * init with a new value. For now, this is okay.
1008 /* SUB:0 and BPSK:0 -> BPSK-20 */
1009 lp->hw->phy->symbol_duration = 50;
1011 /* SUB:1 and BPSK:0 -> BPSK-40 */
1012 lp->hw->phy->symbol_duration = 25;
1016 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
1017 lp->hw->phy->symbol_duration = 40;
1019 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1020 lp->hw->phy->symbol_duration = 16;
1023 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1024 lp->hw->phy->symbol_duration;
1025 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1026 lp->hw->phy->symbol_duration;
1028 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1032 at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1034 struct at86rf230_local *lp = hw->priv;
1037 rc = lp->data->set_channel(lp, page, channel);
1039 usleep_range(lp->data->t_channel_switch,
1040 lp->data->t_channel_switch + 10);
1042 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
1047 at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1048 struct ieee802154_hw_addr_filt *filt,
1049 unsigned long changed)
1051 struct at86rf230_local *lp = hw->priv;
1053 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1054 u16 addr = le16_to_cpu(filt->short_addr);
1056 dev_vdbg(&lp->spi->dev,
1057 "at86rf230_set_hw_addr_filt called for saddr\n");
1058 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1059 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1062 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1063 u16 pan = le16_to_cpu(filt->pan_id);
1065 dev_vdbg(&lp->spi->dev,
1066 "at86rf230_set_hw_addr_filt called for pan id\n");
1067 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1068 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1071 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1074 memcpy(addr, &filt->ieee_addr, 8);
1075 dev_vdbg(&lp->spi->dev,
1076 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
1077 for (i = 0; i < 8; i++)
1078 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1081 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1082 dev_vdbg(&lp->spi->dev,
1083 "at86rf230_set_hw_addr_filt called for panc change\n");
1084 if (filt->pan_coord)
1085 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1087 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1093 #define AT86RF23X_MAX_TX_POWERS 0xF
1094 static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1095 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1099 static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1100 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1104 #define AT86RF212_MAX_TX_POWERS 0x1F
1105 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1106 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1107 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1108 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1112 at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1116 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1117 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1118 return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1125 at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1129 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1130 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1131 return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1138 at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
1140 struct at86rf230_local *lp = hw->priv;
1142 return lp->data->set_txpower(lp, mbm);
1146 at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1148 struct at86rf230_local *lp = hw->priv;
1150 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1154 at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1155 const struct wpan_phy_cca *cca)
1157 struct at86rf230_local *lp = hw->priv;
1160 /* mapping 802.15.4 to driver spec */
1161 switch (cca->mode) {
1162 case NL802154_CCA_ENERGY:
1165 case NL802154_CCA_CARRIER:
1168 case NL802154_CCA_ENERGY_CARRIER:
1170 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1173 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1184 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1189 at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
1191 struct at86rf230_local *lp = hw->priv;
1194 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1195 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1196 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1203 at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1206 struct at86rf230_local *lp = hw->priv;
1209 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1213 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1217 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1221 at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1223 struct at86rf230_local *lp = hw->priv;
1225 return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1229 at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1231 struct at86rf230_local *lp = hw->priv;
1235 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1239 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1243 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1247 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1255 static const struct ieee802154_ops at86rf230_ops = {
1256 .owner = THIS_MODULE,
1257 .xmit_async = at86rf230_xmit,
1259 .set_channel = at86rf230_channel,
1260 .start = at86rf230_start,
1261 .stop = at86rf230_stop,
1262 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
1263 .set_txpower = at86rf230_set_txpower,
1264 .set_lbt = at86rf230_set_lbt,
1265 .set_cca_mode = at86rf230_set_cca_mode,
1266 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1267 .set_csma_params = at86rf230_set_csma_params,
1268 .set_frame_retries = at86rf230_set_frame_retries,
1269 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
1272 static struct at86rf2xx_chip_data at86rf233_data = {
1273 .t_sleep_cycle = 330,
1274 .t_channel_switch = 11,
1275 .t_reset_to_off = 26,
1276 .t_off_to_aack = 80,
1277 .t_off_to_tx_on = 80,
1278 .t_off_to_sleep = 35,
1279 .t_sleep_to_off = 210,
1282 .rssi_base_val = -91,
1283 .set_channel = at86rf23x_set_channel,
1284 .set_txpower = at86rf23x_set_txpower,
1287 static struct at86rf2xx_chip_data at86rf231_data = {
1288 .t_sleep_cycle = 330,
1289 .t_channel_switch = 24,
1290 .t_reset_to_off = 37,
1291 .t_off_to_aack = 110,
1292 .t_off_to_tx_on = 110,
1293 .t_off_to_sleep = 35,
1294 .t_sleep_to_off = 380,
1297 .rssi_base_val = -91,
1298 .set_channel = at86rf23x_set_channel,
1299 .set_txpower = at86rf23x_set_txpower,
1302 static struct at86rf2xx_chip_data at86rf212_data = {
1303 .t_sleep_cycle = 330,
1304 .t_channel_switch = 11,
1305 .t_reset_to_off = 26,
1306 .t_off_to_aack = 200,
1307 .t_off_to_tx_on = 200,
1308 .t_off_to_sleep = 35,
1309 .t_sleep_to_off = 380,
1312 .rssi_base_val = -100,
1313 .set_channel = at86rf212_set_channel,
1314 .set_txpower = at86rf212_set_txpower,
1317 static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1319 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
1323 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1327 irq_type = irq_get_trigger_type(lp->spi->irq);
1328 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1329 irq_type == IRQ_TYPE_EDGE_FALLING)
1330 dev_warn(&lp->spi->dev,
1331 "Using edge triggered irq's are not recommended!\n");
1332 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1333 irq_type == IRQ_TYPE_LEVEL_LOW)
1334 irq_pol = IRQ_ACTIVE_LOW;
1336 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1340 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1344 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1348 /* reset values differs in at86rf231 and at86rf233 */
1349 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1353 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1354 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1357 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1361 /* CLKM changes are applied immediately */
1362 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1367 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1370 /* Wait the next SLEEP cycle */
1371 usleep_range(lp->data->t_sleep_cycle,
1372 lp->data->t_sleep_cycle + 100);
1374 /* xtal_trim value is calculated by:
1375 * CL = 0.5 * (CX + CTRIM + CPAR)
1378 * CL = capacitor of used crystal
1379 * CX = connected capacitors at xtal pins
1380 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1381 * but this is different on each board setup. You need to fine
1382 * tuning this value via CTRIM.
1383 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1387 * atben transceiver:
1391 * CPAR = 3 pF (We assume the magic constant from datasheet)
1394 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1398 * openlabs transceiver:
1402 * CPAR = 3 pF (We assume the magic constant from datasheet)
1405 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1409 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1413 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1417 dev_err(&lp->spi->dev, "DVDD error\n");
1421 /* Force setting slotted operation bit to 0. Sometimes the atben
1422 * sets this bit and I don't know why. We set this always force
1423 * to zero while probing.
1425 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1429 at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1432 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1435 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1439 *rstn = pdata->rstn;
1440 *slp_tr = pdata->slp_tr;
1441 *xtal_trim = pdata->xtal_trim;
1445 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1446 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1447 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1448 if (ret < 0 && ret != -EINVAL)
1455 at86rf230_detect_device(struct at86rf230_local *lp)
1457 unsigned int part, version, val;
1462 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1467 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1470 man_id |= (val << 8);
1472 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1476 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1480 if (man_id != 0x001f) {
1481 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1482 man_id >> 8, man_id & 0xFF);
1486 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
1487 IEEE802154_HW_CSMA_PARAMS |
1488 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1489 IEEE802154_HW_PROMISCUOUS;
1491 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1492 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1493 WPAN_PHY_FLAG_CCA_MODE;
1495 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1496 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1497 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1498 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1500 lp->hw->phy->supported.cca_ed_levels = at86rf23x_ed_levels;
1501 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf23x_ed_levels);
1503 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1512 lp->data = &at86rf231_data;
1513 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1514 lp->hw->phy->current_channel = 11;
1515 lp->hw->phy->symbol_duration = 16;
1516 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1517 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
1521 lp->data = &at86rf212_data;
1522 lp->hw->flags |= IEEE802154_HW_LBT;
1523 lp->hw->phy->supported.channels[0] = 0x00007FF;
1524 lp->hw->phy->supported.channels[2] = 0x00007FF;
1525 lp->hw->phy->current_channel = 5;
1526 lp->hw->phy->symbol_duration = 25;
1527 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
1528 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1529 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
1530 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1531 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1535 lp->data = &at86rf233_data;
1536 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1537 lp->hw->phy->current_channel = 13;
1538 lp->hw->phy->symbol_duration = 16;
1539 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1540 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
1548 lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
1549 lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
1552 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1558 at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1561 lp->state.irq = lp->spi->irq;
1562 spi_message_init(&lp->state.msg);
1563 lp->state.msg.context = &lp->state;
1564 lp->state.trx.len = 2;
1565 lp->state.trx.tx_buf = lp->state.buf;
1566 lp->state.trx.rx_buf = lp->state.buf;
1567 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1568 hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1569 lp->state.timer.function = at86rf230_async_state_timer;
1572 lp->irq.irq = lp->spi->irq;
1573 spi_message_init(&lp->irq.msg);
1574 lp->irq.msg.context = &lp->irq;
1575 lp->irq.trx.len = 2;
1576 lp->irq.trx.tx_buf = lp->irq.buf;
1577 lp->irq.trx.rx_buf = lp->irq.buf;
1578 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1579 hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1580 lp->irq.timer.function = at86rf230_async_state_timer;
1583 lp->tx.irq = lp->spi->irq;
1584 spi_message_init(&lp->tx.msg);
1585 lp->tx.msg.context = &lp->tx;
1587 lp->tx.trx.tx_buf = lp->tx.buf;
1588 lp->tx.trx.rx_buf = lp->tx.buf;
1589 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1590 hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1591 lp->tx.timer.function = at86rf230_async_state_timer;
1594 static int at86rf230_probe(struct spi_device *spi)
1596 struct ieee802154_hw *hw;
1597 struct at86rf230_local *lp;
1598 unsigned int status;
1599 int rc, irq_type, rstn, slp_tr;
1603 dev_err(&spi->dev, "no IRQ specified\n");
1607 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
1609 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1613 if (gpio_is_valid(rstn)) {
1614 rc = devm_gpio_request_one(&spi->dev, rstn,
1615 GPIOF_OUT_INIT_HIGH, "rstn");
1620 if (gpio_is_valid(slp_tr)) {
1621 rc = devm_gpio_request_one(&spi->dev, slp_tr,
1622 GPIOF_OUT_INIT_LOW, "slp_tr");
1628 if (gpio_is_valid(rstn)) {
1630 gpio_set_value(rstn, 0);
1632 gpio_set_value(rstn, 1);
1633 usleep_range(120, 240);
1636 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1643 lp->slp_tr = slp_tr;
1644 hw->parent = &spi->dev;
1645 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1647 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1648 if (IS_ERR(lp->regmap)) {
1649 rc = PTR_ERR(lp->regmap);
1650 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1655 at86rf230_setup_spi_messages(lp);
1657 rc = at86rf230_detect_device(lp);
1661 init_completion(&lp->state_complete);
1663 spi_set_drvdata(spi, lp);
1665 rc = at86rf230_hw_init(lp, xtal_trim);
1669 /* Read irq status register to reset irq line */
1670 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1674 irq_type = irq_get_trigger_type(spi->irq);
1676 irq_type = IRQF_TRIGGER_HIGH;
1678 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1679 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1683 /* disable_irq by default and wait for starting hardware */
1684 disable_irq(spi->irq);
1686 /* going into sleep by default */
1687 at86rf230_sleep(lp);
1689 rc = ieee802154_register_hw(lp->hw);
1696 ieee802154_free_hw(lp->hw);
1701 static int at86rf230_remove(struct spi_device *spi)
1703 struct at86rf230_local *lp = spi_get_drvdata(spi);
1705 /* mask all at86rf230 irq's */
1706 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
1707 ieee802154_unregister_hw(lp->hw);
1708 ieee802154_free_hw(lp->hw);
1709 dev_dbg(&spi->dev, "unregistered at86rf230\n");
1714 static const struct of_device_id at86rf230_of_match[] = {
1715 { .compatible = "atmel,at86rf230", },
1716 { .compatible = "atmel,at86rf231", },
1717 { .compatible = "atmel,at86rf233", },
1718 { .compatible = "atmel,at86rf212", },
1721 MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1723 static const struct spi_device_id at86rf230_device_id[] = {
1724 { .name = "at86rf230", },
1725 { .name = "at86rf231", },
1726 { .name = "at86rf233", },
1727 { .name = "at86rf212", },
1730 MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1732 static struct spi_driver at86rf230_driver = {
1733 .id_table = at86rf230_device_id,
1735 .of_match_table = of_match_ptr(at86rf230_of_match),
1736 .name = "at86rf230",
1737 .owner = THIS_MODULE,
1739 .probe = at86rf230_probe,
1740 .remove = at86rf230_remove,
1743 module_spi_driver(at86rf230_driver);
1745 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1746 MODULE_LICENSE("GPL v2");