mac802154: fix transmission power datatype
[deliverable/linux.git] / drivers / net / ieee802154 / at86rf230.c
index c7a30ce71dcf10a4035eac82d3ff0d5177c3bc30..38026650c0387ecb101d085e24273bf24ded2783 100644 (file)
  */
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/hrtimer.h>
+#include <linux/jiffies.h>
 #include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/gpio.h>
 #include <linux/delay.h>
-#include <linux/spinlock.h>
 #include <linux/spi/spi.h>
 #include <linux/spi/at86rf230.h>
 #include <linux/regmap.h>
@@ -52,11 +53,21 @@ struct at86rf2xx_chip_data {
        int (*get_desense_steps)(struct at86rf230_local *, s32);
 };
 
-#define AT86RF2XX_MAX_BUF (127 + 3)
+#define AT86RF2XX_MAX_BUF              (127 + 3)
+/* tx retries to access the TX_ON state
+ * if it's above then force change will be started.
+ *
+ * We assume the max_frame_retries (7) value of 802.15.4 here.
+ */
+#define AT86RF2XX_MAX_TX_RETRIES       7
+/* We use the recommended 5 minutes timeout to recalibrate */
+#define AT86RF2XX_CAL_LOOP_TIMEOUT     (5 * 60 * HZ)
 
 struct at86rf230_state_change {
        struct at86rf230_local *lp;
+       int irq;
 
+       struct hrtimer timer;
        struct spi_message msg;
        struct spi_transfer trx;
        u8 buf[AT86RF2XX_MAX_BUF];
@@ -81,10 +92,10 @@ struct at86rf230_local {
        struct at86rf230_state_change irq;
 
        bool tx_aret;
+       unsigned long cal_timeout;
        s8 max_frame_retries;
        bool is_tx;
-       /* spinlock for is_tx protection */
-       spinlock_t lock;
+       u8 tx_retry;
        struct sk_buff *tx_skb;
        struct at86rf230_state_change tx;
 };
@@ -311,7 +322,7 @@ at86rf230_read_subreg(struct at86rf230_local *lp,
        int rc;
 
        rc = __at86rf230_read(lp, addr, data);
-       if (rc > 0)
+       if (!rc)
                *data = (*data & mask) >> shift;
 
        return rc;
@@ -407,6 +418,8 @@ at86rf230_reg_volatile(struct device *dev, unsigned int reg)
        case RG_PHY_ED_LEVEL:
        case RG_IRQ_STATUS:
        case RG_VREG_CTRL:
+       case RG_PLL_CF:
+       case RG_PLL_DCU:
                return true;
        default:
                return false;
@@ -444,6 +457,7 @@ at86rf230_async_error_recover(void *context)
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
 
+       lp->is_tx = 0;
        at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
        ieee802154_wake_queue(lp->hw);
 }
@@ -470,18 +484,25 @@ at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
        u8 *tx_buf = ctx->buf;
 
        tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
-       ctx->trx.len = 2;
        ctx->msg.complete = complete;
        ctx->irq_enable = irq_enable;
        rc = spi_async(lp->spi, &ctx->msg);
        if (rc) {
                if (irq_enable)
-                       enable_irq(lp->spi->irq);
+                       enable_irq(ctx->irq);
 
                at86rf230_async_error(lp, ctx, rc);
        }
 }
 
+static inline u8 at86rf230_state_to_force(u8 state)
+{
+       if (state == STATE_TX_ON)
+               return STATE_FORCE_TX_ON;
+       else
+               return STATE_FORCE_TRX_OFF;
+}
+
 static void
 at86rf230_async_state_assert(void *context)
 {
@@ -512,10 +533,21 @@ at86rf230_async_state_assert(void *context)
                         * in STATE_BUSY_RX_AACK, we run a force state change
                         * to STATE_TX_ON. This is a timeout handling, if the
                         * transceiver stucks in STATE_BUSY_RX_AACK.
+                        *
+                        * Additional we do several retries to try to get into
+                        * TX_ON state without forcing. If the retries are
+                        * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
+                        * will do a force change.
                         */
-                       if (ctx->to_state == STATE_TX_ON) {
-                               at86rf230_async_state_change(lp, ctx,
-                                                            STATE_FORCE_TX_ON,
+                       if (ctx->to_state == STATE_TX_ON ||
+                           ctx->to_state == STATE_TRX_OFF) {
+                               u8 state = ctx->to_state;
+
+                               if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
+                                       state = at86rf230_state_to_force(state);
+                               lp->tx_retry++;
+
+                               at86rf230_async_state_change(lp, ctx, state,
                                                             ctx->complete,
                                                             ctx->irq_enable);
                                return;
@@ -531,6 +563,19 @@ done:
                ctx->complete(context);
 }
 
+static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
+{
+       struct at86rf230_state_change *ctx =
+               container_of(timer, struct at86rf230_state_change, timer);
+       struct at86rf230_local *lp = ctx->lp;
+
+       at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
+                                at86rf230_async_state_assert,
+                                ctx->irq_enable);
+
+       return HRTIMER_NORESTART;
+}
+
 /* Do state change timing delay. */
 static void
 at86rf230_async_state_delay(void *context)
@@ -539,6 +584,7 @@ at86rf230_async_state_delay(void *context)
        struct at86rf230_local *lp = ctx->lp;
        struct at86rf2xx_chip_data *c = lp->data;
        bool force = false;
+       ktime_t tim;
 
        /* The force state changes are will show as normal states in the
         * state status subregister. We change the to_state to the
@@ -562,11 +608,15 @@ at86rf230_async_state_delay(void *context)
        case STATE_TRX_OFF:
                switch (ctx->to_state) {
                case STATE_RX_AACK_ON:
-                       usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
+                       tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
                        goto change;
                case STATE_TX_ON:
-                       usleep_range(c->t_off_to_tx_on,
-                                    c->t_off_to_tx_on + 10);
+                       tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
+                       /* state change from TRX_OFF to TX_ON to do a
+                        * calibration, we need to reset the timeout for the
+                        * next one.
+                        */
+                       lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
                        goto change;
                default:
                        break;
@@ -574,14 +624,15 @@ at86rf230_async_state_delay(void *context)
                break;
        case STATE_BUSY_RX_AACK:
                switch (ctx->to_state) {
+               case STATE_TRX_OFF:
                case STATE_TX_ON:
                        /* Wait for worst case receiving time if we
                         * didn't make a force change from BUSY_RX_AACK
-                        * to TX_ON.
+                        * to TX_ON or TRX_OFF.
                         */
                        if (!force) {
-                               usleep_range(c->t_frame + c->t_p_ack,
-                                            c->t_frame + c->t_p_ack + 1000);
+                               tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
+                                                  NSEC_PER_USEC);
                                goto change;
                        }
                        break;
@@ -593,7 +644,7 @@ at86rf230_async_state_delay(void *context)
        case STATE_P_ON:
                switch (ctx->to_state) {
                case STATE_TRX_OFF:
-                       usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
+                       tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
                        goto change;
                default:
                        break;
@@ -604,12 +655,10 @@ at86rf230_async_state_delay(void *context)
        }
 
        /* Default delay is 1us in the most cases */
-       udelay(1);
+       tim = ktime_set(0, NSEC_PER_USEC);
 
 change:
-       at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
-                                at86rf230_async_state_assert,
-                                ctx->irq_enable);
+       hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
 }
 
 static void
@@ -645,12 +694,11 @@ at86rf230_async_state_change_start(void *context)
         */
        buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
        buf[1] = ctx->to_state;
-       ctx->trx.len = 2;
        ctx->msg.complete = at86rf230_async_state_delay;
        rc = spi_async(lp->spi, &ctx->msg);
        if (rc) {
                if (ctx->irq_enable)
-                       enable_irq(lp->spi->irq);
+                       enable_irq(ctx->irq);
 
                at86rf230_async_error(lp, ctx, rc);
        }
@@ -708,11 +756,10 @@ at86rf230_tx_complete(void *context)
 {
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
-       struct sk_buff *skb = lp->tx_skb;
 
-       enable_irq(lp->spi->irq);
+       enable_irq(ctx->irq);
 
-       ieee802154_xmit_complete(lp->hw, skb, !lp->tx_aret);
+       ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
 }
 
 static void
@@ -721,7 +768,7 @@ at86rf230_tx_on(void *context)
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
 
-       at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
+       at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
                                     at86rf230_tx_complete, true);
 }
 
@@ -765,14 +812,25 @@ at86rf230_tx_trac_status(void *context)
 }
 
 static void
-at86rf230_rx(struct at86rf230_local *lp,
-            const u8 *data, const u8 len, const u8 lqi)
+at86rf230_rx_read_frame_complete(void *context)
 {
-       struct sk_buff *skb;
+       struct at86rf230_state_change *ctx = context;
+       struct at86rf230_local *lp = ctx->lp;
        u8 rx_local_buf[AT86RF2XX_MAX_BUF];
+       const u8 *buf = ctx->buf;
+       struct sk_buff *skb;
+       u8 len, lqi;
 
-       memcpy(rx_local_buf, data, len);
-       enable_irq(lp->spi->irq);
+       len = buf[1];
+       if (!ieee802154_is_valid_psdu_len(len)) {
+               dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
+               len = IEEE802154_MTU;
+       }
+       lqi = buf[2 + len];
+
+       memcpy(rx_local_buf, buf + 2, len);
+       ctx->trx.len = 2;
+       enable_irq(ctx->irq);
 
        skb = dev_alloc_skb(IEEE802154_MTU);
        if (!skb) {
@@ -785,60 +843,41 @@ at86rf230_rx(struct at86rf230_local *lp,
 }
 
 static void
-at86rf230_rx_read_frame_complete(void *context)
+at86rf230_rx_read_frame(void *context)
 {
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
-       const u8 *buf = lp->irq.buf;
-       u8 len = buf[1];
-
-       if (!ieee802154_is_valid_psdu_len(len)) {
-               dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
-               len = IEEE802154_MTU;
-       }
-
-       at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
-}
-
-static void
-at86rf230_rx_read_frame(struct at86rf230_local *lp)
-{
+       u8 *buf = ctx->buf;
        int rc;
 
-       u8 *buf = lp->irq.buf;
-
        buf[0] = CMD_FB;
-       lp->irq.trx.len = AT86RF2XX_MAX_BUF;
-       lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
-       rc = spi_async(lp->spi, &lp->irq.msg);
+       ctx->trx.len = AT86RF2XX_MAX_BUF;
+       ctx->msg.complete = at86rf230_rx_read_frame_complete;
+       rc = spi_async(lp->spi, &ctx->msg);
        if (rc) {
-               enable_irq(lp->spi->irq);
-               at86rf230_async_error(lp, &lp->irq, rc);
+               ctx->trx.len = 2;
+               enable_irq(ctx->irq);
+               at86rf230_async_error(lp, ctx, rc);
        }
 }
 
 static void
 at86rf230_rx_trac_check(void *context)
 {
-       struct at86rf230_state_change *ctx = context;
-       struct at86rf230_local *lp = ctx->lp;
-
        /* Possible check on trac status here. This could be useful to make
         * some stats why receive is failed. Not used at the moment, but it's
         * maybe timing relevant. Datasheet doesn't say anything about this.
         * The programming guide say do it so.
         */
 
-       at86rf230_rx_read_frame(lp);
+       at86rf230_rx_read_frame(context);
 }
 
 static void
 at86rf230_irq_trx_end(struct at86rf230_local *lp)
 {
-       spin_lock(&lp->lock);
        if (lp->is_tx) {
                lp->is_tx = 0;
-               spin_unlock(&lp->lock);
 
                if (lp->tx_aret)
                        at86rf230_async_state_change(lp, &lp->irq,
@@ -851,7 +890,6 @@ at86rf230_irq_trx_end(struct at86rf230_local *lp)
                                                     at86rf230_tx_complete,
                                                     true);
        } else {
-               spin_unlock(&lp->lock);
                at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
                                         at86rf230_rx_trac_check, true);
        }
@@ -862,13 +900,13 @@ at86rf230_irq_status(void *context)
 {
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
-       const u8 *buf = lp->irq.buf;
+       const u8 *buf = ctx->buf;
        const u8 irq = buf[1];
 
        if (irq & IRQ_TRX_END) {
                at86rf230_irq_trx_end(lp);
        } else {
-               enable_irq(lp->spi->irq);
+               enable_irq(ctx->irq);
                dev_err(&lp->spi->dev, "not supported irq %02x received\n",
                        irq);
        }
@@ -884,7 +922,6 @@ static irqreturn_t at86rf230_isr(int irq, void *data)
        disable_irq_nosync(irq);
 
        buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
-       ctx->trx.len = 2;
        ctx->msg.complete = at86rf230_irq_status;
        rc = spi_async(lp->spi, &ctx->msg);
        if (rc) {
@@ -919,21 +956,21 @@ at86rf230_write_frame(void *context)
        struct at86rf230_state_change *ctx = context;
        struct at86rf230_local *lp = ctx->lp;
        struct sk_buff *skb = lp->tx_skb;
-       u8 *buf = lp->tx.buf;
+       u8 *buf = ctx->buf;
        int rc;
 
-       spin_lock(&lp->lock);
        lp->is_tx = 1;
-       spin_unlock(&lp->lock);
 
        buf[0] = CMD_FB | CMD_WRITE;
        buf[1] = skb->len + 2;
        memcpy(buf + 2, skb->data, skb->len);
-       lp->tx.trx.len = skb->len + 2;
-       lp->tx.msg.complete = at86rf230_write_frame_complete;
-       rc = spi_async(lp->spi, &lp->tx.msg);
-       if (rc)
+       ctx->trx.len = skb->len + 2;
+       ctx->msg.complete = at86rf230_write_frame_complete;
+       rc = spi_async(lp->spi, &ctx->msg);
+       if (rc) {
+               ctx->trx.len = 2;
                at86rf230_async_error(lp, ctx, rc);
+       }
 }
 
 static void
@@ -946,24 +983,45 @@ at86rf230_xmit_tx_on(void *context)
                                     at86rf230_write_frame, false);
 }
 
+static void
+at86rf230_xmit_start(void *context)
+{
+       struct at86rf230_state_change *ctx = context;
+       struct at86rf230_local *lp = ctx->lp;
+
+       /* In ARET mode we need to go into STATE_TX_ARET_ON after we
+        * are in STATE_TX_ON. The pfad differs here, so we change
+        * the complete handler.
+        */
+       if (lp->tx_aret)
+               at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
+                                            at86rf230_xmit_tx_on, false);
+       else
+               at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
+                                            at86rf230_write_frame, false);
+}
+
 static int
 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
 {
        struct at86rf230_local *lp = hw->priv;
        struct at86rf230_state_change *ctx = &lp->tx;
 
-       void (*tx_complete)(void *context) = at86rf230_write_frame;
-
        lp->tx_skb = skb;
+       lp->tx_retry = 0;
 
-       /* In ARET mode we need to go into STATE_TX_ARET_ON after we
-        * are in STATE_TX_ON. The pfad differs here, so we change
-        * the complete handler.
+       /* After 5 minutes in PLL and the same frequency we run again the
+        * calibration loops which is recommended by at86rf2xx datasheets.
+        *
+        * The calibration is initiate by a state change from TRX_OFF
+        * to TX_ON, the lp->cal_timeout should be reinit by state_delay
+        * function then to start in the next 5 minutes.
         */
-       if (lp->tx_aret)
-               tx_complete = at86rf230_xmit_tx_on;
-
-       at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
+       if (time_is_before_jiffies(lp->cal_timeout))
+               at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
+                                            at86rf230_xmit_start, false);
+       else
+               at86rf230_xmit_start(ctx);
 
        return 0;
 }
@@ -979,6 +1037,9 @@ at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
 static int
 at86rf230_start(struct ieee802154_hw *hw)
 {
+       struct at86rf230_local *lp = hw->priv;
+
+       lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
        return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
 }
 
@@ -1059,6 +1120,8 @@ at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
        /* Wait for PLL */
        usleep_range(lp->data->t_channel_switch,
                     lp->data->t_channel_switch + 10);
+
+       lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
        return rc;
 }
 
@@ -1110,7 +1173,7 @@ at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
 }
 
 static int
-at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
+at86rf230_set_txpower(struct ieee802154_hw *hw, s8 db)
 {
        struct at86rf230_local *lp = hw->priv;
 
@@ -1321,6 +1384,10 @@ static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
                return rc;
 
        irq_type = irq_get_trigger_type(lp->spi->irq);
+       if (irq_type == IRQ_TYPE_EDGE_RISING ||
+           irq_type == IRQ_TYPE_EDGE_FALLING)
+               dev_warn(&lp->spi->dev,
+                        "Using edge triggered irq's are not recommended!\n");
        if (irq_type == IRQ_TYPE_EDGE_FALLING ||
            irq_type == IRQ_TYPE_LEVEL_LOW)
                irq_pol = IRQ_ACTIVE_LOW;
@@ -1524,25 +1591,37 @@ static void
 at86rf230_setup_spi_messages(struct at86rf230_local *lp)
 {
        lp->state.lp = lp;
+       lp->state.irq = lp->spi->irq;
        spi_message_init(&lp->state.msg);
        lp->state.msg.context = &lp->state;
+       lp->state.trx.len = 2;
        lp->state.trx.tx_buf = lp->state.buf;
        lp->state.trx.rx_buf = lp->state.buf;
        spi_message_add_tail(&lp->state.trx, &lp->state.msg);
+       hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+       lp->state.timer.function = at86rf230_async_state_timer;
 
        lp->irq.lp = lp;
+       lp->irq.irq = lp->spi->irq;
        spi_message_init(&lp->irq.msg);
        lp->irq.msg.context = &lp->irq;
+       lp->irq.trx.len = 2;
        lp->irq.trx.tx_buf = lp->irq.buf;
        lp->irq.trx.rx_buf = lp->irq.buf;
        spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
+       hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+       lp->irq.timer.function = at86rf230_async_state_timer;
 
        lp->tx.lp = lp;
+       lp->tx.irq = lp->spi->irq;
        spi_message_init(&lp->tx.msg);
        lp->tx.msg.context = &lp->tx;
+       lp->tx.trx.len = 2;
        lp->tx.trx.tx_buf = lp->tx.buf;
        lp->tx.trx.rx_buf = lp->tx.buf;
        spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
+       hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+       lp->tx.timer.function = at86rf230_async_state_timer;
 }
 
 static int at86rf230_probe(struct spi_device *spi)
@@ -1551,7 +1630,7 @@ static int at86rf230_probe(struct spi_device *spi)
        struct at86rf230_local *lp;
        unsigned int status;
        int rc, irq_type, rstn, slp_tr;
-       u8 xtal_trim;
+       u8 xtal_trim = 0;
 
        if (!spi->irq) {
                dev_err(&spi->dev, "no IRQ specified\n");
@@ -1612,7 +1691,6 @@ static int at86rf230_probe(struct spi_device *spi)
        if (rc < 0)
                goto free_dev;
 
-       spin_lock_init(&lp->lock);
        init_completion(&lp->state_complete);
 
        spi_set_drvdata(spi, lp);
This page took 0.03614 seconds and 5 git commands to generate.