]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ieee802154/at86rf230.c
at86rf230: interrupt tx with force trx_off
[karo-tx-linux.git] / drivers / net / ieee802154 / at86rf230.c
1 /*
2  * AT86RF230/RF231 driver
3  *
4  * Copyright (C) 2009-2012 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Written by:
16  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
18  * Alexander Aring <aar@pengutronix.de>
19  */
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/hrtimer.h>
23 #include <linux/jiffies.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/gpio.h>
27 #include <linux/delay.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/at86rf230.h>
30 #include <linux/regmap.h>
31 #include <linux/skbuff.h>
32 #include <linux/of_gpio.h>
33 #include <linux/ieee802154.h>
34
35 #include <net/mac802154.h>
36 #include <net/cfg802154.h>
37
38 #include "at86rf230.h"
39
40 struct at86rf230_local;
41 /* at86rf2xx chip depend data.
42  * All timings are in us.
43  */
44 struct at86rf2xx_chip_data {
45         u16 t_sleep_cycle;
46         u16 t_channel_switch;
47         u16 t_reset_to_off;
48         u16 t_off_to_aack;
49         u16 t_off_to_tx_on;
50         u16 t_off_to_sleep;
51         u16 t_sleep_to_off;
52         u16 t_frame;
53         u16 t_p_ack;
54         int rssi_base_val;
55
56         int (*set_channel)(struct at86rf230_local *, u8, u8);
57         int (*set_txpower)(struct at86rf230_local *, s32);
58 };
59
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.
63  *
64  * We assume the max_frame_retries (7) value of 802.15.4 here.
65  */
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)
69
70 struct at86rf230_state_change {
71         struct at86rf230_local *lp;
72         int irq;
73
74         struct hrtimer timer;
75         struct spi_message msg;
76         struct spi_transfer trx;
77         u8 buf[AT86RF2XX_MAX_BUF];
78
79         void (*complete)(void *context);
80         u8 from_state;
81         u8 to_state;
82
83         bool irq_enable;
84 };
85
86 struct at86rf230_local {
87         struct spi_device *spi;
88
89         struct ieee802154_hw *hw;
90         struct at86rf2xx_chip_data *data;
91         struct regmap *regmap;
92         int slp_tr;
93         bool sleep;
94
95         struct completion state_complete;
96         struct at86rf230_state_change state;
97
98         struct at86rf230_state_change irq;
99
100         unsigned long cal_timeout;
101         bool is_tx;
102         bool is_tx_from_off;
103         u8 tx_retry;
104         struct sk_buff *tx_skb;
105         struct at86rf230_state_change tx;
106 };
107
108 #define AT86RF2XX_NUMREGS 0x3F
109
110 static void
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);
115
116 static inline void
117 at86rf230_sleep(struct at86rf230_local *lp)
118 {
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);
123                 lp->sleep = true;
124         }
125 }
126
127 static inline void
128 at86rf230_awake(struct at86rf230_local *lp)
129 {
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);
134                 lp->sleep = false;
135         }
136 }
137
138 static inline int
139 __at86rf230_write(struct at86rf230_local *lp,
140                   unsigned int addr, unsigned int data)
141 {
142         bool sleep = lp->sleep;
143         int ret;
144
145         /* awake for register setting if sleep */
146         if (sleep)
147                 at86rf230_awake(lp);
148
149         ret = regmap_write(lp->regmap, addr, data);
150
151         /* sleep again if was sleeping */
152         if (sleep)
153                 at86rf230_sleep(lp);
154
155         return ret;
156 }
157
158 static inline int
159 __at86rf230_read(struct at86rf230_local *lp,
160                  unsigned int addr, unsigned int *data)
161 {
162         bool sleep = lp->sleep;
163         int ret;
164
165         /* awake for register setting if sleep */
166         if (sleep)
167                 at86rf230_awake(lp);
168
169         ret = regmap_read(lp->regmap, addr, data);
170
171         /* sleep again if was sleeping */
172         if (sleep)
173                 at86rf230_sleep(lp);
174
175         return ret;
176 }
177
178 static inline int
179 at86rf230_read_subreg(struct at86rf230_local *lp,
180                       unsigned int addr, unsigned int mask,
181                       unsigned int shift, unsigned int *data)
182 {
183         int rc;
184
185         rc = __at86rf230_read(lp, addr, data);
186         if (!rc)
187                 *data = (*data & mask) >> shift;
188
189         return rc;
190 }
191
192 static inline int
193 at86rf230_write_subreg(struct at86rf230_local *lp,
194                        unsigned int addr, unsigned int mask,
195                        unsigned int shift, unsigned int data)
196 {
197         bool sleep = lp->sleep;
198         int ret;
199
200         /* awake for register setting if sleep */
201         if (sleep)
202                 at86rf230_awake(lp);
203
204         ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
205
206         /* sleep again if was sleeping */
207         if (sleep)
208                 at86rf230_sleep(lp);
209
210         return ret;
211 }
212
213 static inline void
214 at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
215 {
216         gpio_set_value(lp->slp_tr, 1);
217         udelay(1);
218         gpio_set_value(lp->slp_tr, 0);
219 }
220
221 static bool
222 at86rf230_reg_writeable(struct device *dev, unsigned int reg)
223 {
224         switch (reg) {
225         case RG_TRX_STATE:
226         case RG_TRX_CTRL_0:
227         case RG_TRX_CTRL_1:
228         case RG_PHY_TX_PWR:
229         case RG_PHY_ED_LEVEL:
230         case RG_PHY_CC_CCA:
231         case RG_CCA_THRES:
232         case RG_RX_CTRL:
233         case RG_SFD_VALUE:
234         case RG_TRX_CTRL_2:
235         case RG_ANT_DIV:
236         case RG_IRQ_MASK:
237         case RG_VREG_CTRL:
238         case RG_BATMON:
239         case RG_XOSC_CTRL:
240         case RG_RX_SYN:
241         case RG_XAH_CTRL_1:
242         case RG_FTN_CTRL:
243         case RG_PLL_CF:
244         case RG_PLL_DCU:
245         case RG_SHORT_ADDR_0:
246         case RG_SHORT_ADDR_1:
247         case RG_PAN_ID_0:
248         case RG_PAN_ID_1:
249         case RG_IEEE_ADDR_0:
250         case RG_IEEE_ADDR_1:
251         case RG_IEEE_ADDR_2:
252         case RG_IEEE_ADDR_3:
253         case RG_IEEE_ADDR_4:
254         case RG_IEEE_ADDR_5:
255         case RG_IEEE_ADDR_6:
256         case RG_IEEE_ADDR_7:
257         case RG_XAH_CTRL_0:
258         case RG_CSMA_SEED_0:
259         case RG_CSMA_SEED_1:
260         case RG_CSMA_BE:
261                 return true;
262         default:
263                 return false;
264         }
265 }
266
267 static bool
268 at86rf230_reg_readable(struct device *dev, unsigned int reg)
269 {
270         bool rc;
271
272         /* all writeable are also readable */
273         rc = at86rf230_reg_writeable(dev, reg);
274         if (rc)
275                 return rc;
276
277         /* readonly regs */
278         switch (reg) {
279         case RG_TRX_STATUS:
280         case RG_PHY_RSSI:
281         case RG_IRQ_STATUS:
282         case RG_PART_NUM:
283         case RG_VERSION_NUM:
284         case RG_MAN_ID_1:
285         case RG_MAN_ID_0:
286                 return true;
287         default:
288                 return false;
289         }
290 }
291
292 static bool
293 at86rf230_reg_volatile(struct device *dev, unsigned int reg)
294 {
295         /* can be changed during runtime */
296         switch (reg) {
297         case RG_TRX_STATUS:
298         case RG_TRX_STATE:
299         case RG_PHY_RSSI:
300         case RG_PHY_ED_LEVEL:
301         case RG_IRQ_STATUS:
302         case RG_VREG_CTRL:
303         case RG_PLL_CF:
304         case RG_PLL_DCU:
305                 return true;
306         default:
307                 return false;
308         }
309 }
310
311 static bool
312 at86rf230_reg_precious(struct device *dev, unsigned int reg)
313 {
314         /* don't clear irq line on read */
315         switch (reg) {
316         case RG_IRQ_STATUS:
317                 return true;
318         default:
319                 return false;
320         }
321 }
322
323 static const struct regmap_config at86rf230_regmap_spi_config = {
324         .reg_bits = 8,
325         .val_bits = 8,
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,
334 };
335
336 static void
337 at86rf230_async_error_recover(void *context)
338 {
339         struct at86rf230_state_change *ctx = context;
340         struct at86rf230_local *lp = ctx->lp;
341
342         lp->is_tx = 0;
343         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
344         ieee802154_wake_queue(lp->hw);
345 }
346
347 static inline void
348 at86rf230_async_error(struct at86rf230_local *lp,
349                       struct at86rf230_state_change *ctx, int rc)
350 {
351         dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
352
353         at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
354                                      at86rf230_async_error_recover, false);
355 }
356
357 /* Generic function to get some register value in async mode */
358 static void
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)
363 {
364         int rc;
365
366         u8 *tx_buf = ctx->buf;
367
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);
372         if (rc) {
373                 if (irq_enable)
374                         enable_irq(ctx->irq);
375
376                 at86rf230_async_error(lp, ctx, rc);
377         }
378 }
379
380 static void
381 at86rf230_async_state_assert(void *context)
382 {
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;
387
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.
392                  */
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
398                          * can't assert this.
399                          */
400                         if (ctx->to_state == STATE_RX_AACK_ON)
401                                 goto done;
402
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.
410                          *
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.
415                          */
416                         if (ctx->to_state == STATE_TX_ON ||
417                             ctx->to_state == STATE_TRX_OFF) {
418                                 u8 state = ctx->to_state;
419
420                                 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
421                                         state = STATE_FORCE_TRX_OFF;
422                                 lp->tx_retry++;
423
424                                 at86rf230_async_state_change(lp, ctx, state,
425                                                              ctx->complete,
426                                                              ctx->irq_enable);
427                                 return;
428                         }
429                 }
430
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);
433         }
434
435 done:
436         if (ctx->complete)
437                 ctx->complete(context);
438 }
439
440 static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
441 {
442         struct at86rf230_state_change *ctx =
443                 container_of(timer, struct at86rf230_state_change, timer);
444         struct at86rf230_local *lp = ctx->lp;
445
446         at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
447                                  at86rf230_async_state_assert,
448                                  ctx->irq_enable);
449
450         return HRTIMER_NORESTART;
451 }
452
453 /* Do state change timing delay. */
454 static void
455 at86rf230_async_state_delay(void *context)
456 {
457         struct at86rf230_state_change *ctx = context;
458         struct at86rf230_local *lp = ctx->lp;
459         struct at86rf2xx_chip_data *c = lp->data;
460         bool force = false;
461         ktime_t tim;
462
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.
467          */
468         switch (ctx->to_state) {
469         case STATE_FORCE_TX_ON:
470                 ctx->to_state = STATE_TX_ON;
471                 force = true;
472                 break;
473         case STATE_FORCE_TRX_OFF:
474                 ctx->to_state = STATE_TRX_OFF;
475                 force = true;
476                 break;
477         default:
478                 break;
479         }
480
481         switch (ctx->from_state) {
482         case STATE_TRX_OFF:
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
488                          * next one.
489                          */
490                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
491                         goto change;
492                 case STATE_TX_ARET_ON:
493                 case STATE_TX_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
497                          * next one.
498                          */
499                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
500                         goto change;
501                 default:
502                         break;
503                 }
504                 break;
505         case STATE_BUSY_RX_AACK:
506                 switch (ctx->to_state) {
507                 case STATE_TRX_OFF:
508                 case STATE_TX_ON:
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.
512                          */
513                         if (!force) {
514                                 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
515                                                    NSEC_PER_USEC);
516                                 goto change;
517                         }
518                         break;
519                 default:
520                         break;
521                 }
522                 break;
523         /* Default value, means RESET state */
524         case STATE_P_ON:
525                 switch (ctx->to_state) {
526                 case STATE_TRX_OFF:
527                         tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
528                         goto change;
529                 default:
530                         break;
531                 }
532                 break;
533         default:
534                 break;
535         }
536
537         /* Default delay is 1us in the most cases */
538         udelay(1);
539         at86rf230_async_state_timer(&ctx->timer);
540         return;
541
542 change:
543         hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
544 }
545
546 static void
547 at86rf230_async_state_change_start(void *context)
548 {
549         struct at86rf230_state_change *ctx = context;
550         struct at86rf230_local *lp = ctx->lp;
551         u8 *buf = ctx->buf;
552         const u8 trx_state = buf[1] & TRX_STATE_MASK;
553         int rc;
554
555         /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
556         if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
557                 udelay(1);
558                 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
559                                          at86rf230_async_state_change_start,
560                                          ctx->irq_enable);
561                 return;
562         }
563
564         /* Check if we already are in the state which we change in */
565         if (trx_state == ctx->to_state) {
566                 if (ctx->complete)
567                         ctx->complete(context);
568                 return;
569         }
570
571         /* Set current state to the context of state change */
572         ctx->from_state = trx_state;
573
574         /* Going into the next step for a state change which do a timing
575          * relevant delay.
576          */
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);
581         if (rc) {
582                 if (ctx->irq_enable)
583                         enable_irq(ctx->irq);
584
585                 at86rf230_async_error(lp, ctx, rc);
586         }
587 }
588
589 static void
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)
594 {
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,
601                                  irq_enable);
602 }
603
604 static void
605 at86rf230_sync_state_change_complete(void *context)
606 {
607         struct at86rf230_state_change *ctx = context;
608         struct at86rf230_local *lp = ctx->lp;
609
610         complete(&lp->state_complete);
611 }
612
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.
616  */
617 static int
618 at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
619 {
620         unsigned long rc;
621
622         at86rf230_async_state_change(lp, &lp->state, state,
623                                      at86rf230_sync_state_change_complete,
624                                      false);
625
626         rc = wait_for_completion_timeout(&lp->state_complete,
627                                          msecs_to_jiffies(100));
628         if (!rc) {
629                 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
630                 return -ETIMEDOUT;
631         }
632
633         return 0;
634 }
635
636 static void
637 at86rf230_tx_complete(void *context)
638 {
639         struct at86rf230_state_change *ctx = context;
640         struct at86rf230_local *lp = ctx->lp;
641
642         enable_irq(ctx->irq);
643
644         ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
645 }
646
647 static void
648 at86rf230_tx_on(void *context)
649 {
650         struct at86rf230_state_change *ctx = context;
651         struct at86rf230_local *lp = ctx->lp;
652
653         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
654                                      at86rf230_tx_complete, true);
655 }
656
657 static void
658 at86rf230_tx_trac_check(void *context)
659 {
660         struct at86rf230_state_change *ctx = context;
661         struct at86rf230_local *lp = ctx->lp;
662
663         at86rf230_async_state_change(lp, &lp->irq, STATE_TX_ON,
664                                      at86rf230_tx_on, true);
665 }
666
667 static void
668 at86rf230_rx_read_frame_complete(void *context)
669 {
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;
674         struct sk_buff *skb;
675         u8 len, lqi;
676
677         len = buf[1];
678         if (!ieee802154_is_valid_psdu_len(len)) {
679                 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
680                 len = IEEE802154_MTU;
681         }
682         lqi = buf[2 + len];
683
684         memcpy(rx_local_buf, buf + 2, len);
685         ctx->trx.len = 2;
686         enable_irq(ctx->irq);
687
688         skb = dev_alloc_skb(IEEE802154_MTU);
689         if (!skb) {
690                 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
691                 return;
692         }
693
694         memcpy(skb_put(skb, len), rx_local_buf, len);
695         ieee802154_rx_irqsafe(lp->hw, skb, lqi);
696 }
697
698 static void
699 at86rf230_rx_read_frame(void *context)
700 {
701         struct at86rf230_state_change *ctx = context;
702         struct at86rf230_local *lp = ctx->lp;
703         u8 *buf = ctx->buf;
704         int rc;
705
706         buf[0] = CMD_FB;
707         ctx->trx.len = AT86RF2XX_MAX_BUF;
708         ctx->msg.complete = at86rf230_rx_read_frame_complete;
709         rc = spi_async(lp->spi, &ctx->msg);
710         if (rc) {
711                 ctx->trx.len = 2;
712                 enable_irq(ctx->irq);
713                 at86rf230_async_error(lp, ctx, rc);
714         }
715 }
716
717 static void
718 at86rf230_rx_trac_check(void *context)
719 {
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.
724          */
725
726         at86rf230_rx_read_frame(context);
727 }
728
729 static void
730 at86rf230_irq_trx_end(struct at86rf230_local *lp)
731 {
732         if (lp->is_tx) {
733                 lp->is_tx = 0;
734                 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
735                                          at86rf230_tx_trac_check, true);
736         } else {
737                 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
738                                          at86rf230_rx_trac_check, true);
739         }
740 }
741
742 static void
743 at86rf230_irq_status(void *context)
744 {
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];
749
750         if (irq & IRQ_TRX_END) {
751                 at86rf230_irq_trx_end(lp);
752         } else {
753                 enable_irq(ctx->irq);
754                 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
755                         irq);
756         }
757 }
758
759 static irqreturn_t at86rf230_isr(int irq, void *data)
760 {
761         struct at86rf230_local *lp = data;
762         struct at86rf230_state_change *ctx = &lp->irq;
763         u8 *buf = ctx->buf;
764         int rc;
765
766         disable_irq_nosync(irq);
767
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);
771         if (rc) {
772                 enable_irq(irq);
773                 at86rf230_async_error(lp, ctx, rc);
774                 return IRQ_NONE;
775         }
776
777         return IRQ_HANDLED;
778 }
779
780 static void
781 at86rf230_write_frame_complete(void *context)
782 {
783         struct at86rf230_state_change *ctx = context;
784         struct at86rf230_local *lp = ctx->lp;
785         u8 *buf = ctx->buf;
786         int rc;
787
788         ctx->trx.len = 2;
789
790         if (gpio_is_valid(lp->slp_tr)) {
791                 at86rf230_slp_tr_rising_edge(lp);
792         } else {
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);
797                 if (rc)
798                         at86rf230_async_error(lp, ctx, rc);
799         }
800 }
801
802 static void
803 at86rf230_write_frame(void *context)
804 {
805         struct at86rf230_state_change *ctx = context;
806         struct at86rf230_local *lp = ctx->lp;
807         struct sk_buff *skb = lp->tx_skb;
808         u8 *buf = ctx->buf;
809         int rc;
810
811         lp->is_tx = 1;
812
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);
819         if (rc) {
820                 ctx->trx.len = 2;
821                 at86rf230_async_error(lp, ctx, rc);
822         }
823 }
824
825 static void
826 at86rf230_xmit_tx_on(void *context)
827 {
828         struct at86rf230_state_change *ctx = context;
829         struct at86rf230_local *lp = ctx->lp;
830
831         at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
832                                      at86rf230_write_frame, false);
833 }
834
835 static void
836 at86rf230_xmit_start(void *context)
837 {
838         struct at86rf230_state_change *ctx = context;
839         struct at86rf230_local *lp = ctx->lp;
840
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,
846                                              false);
847         } else {
848                 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
849                                              at86rf230_xmit_tx_on,
850                                              false);
851         }
852 }
853
854 static int
855 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
856 {
857         struct at86rf230_local *lp = hw->priv;
858         struct at86rf230_state_change *ctx = &lp->tx;
859
860         lp->tx_skb = skb;
861         lp->tx_retry = 0;
862
863         /* After 5 minutes in PLL and the same frequency we run again the
864          * calibration loops which is recommended by at86rf2xx datasheets.
865          *
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.
869          */
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);
874         } else {
875                 at86rf230_xmit_start(ctx);
876         }
877
878         return 0;
879 }
880
881 static int
882 at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
883 {
884         BUG_ON(!level);
885         *level = 0xbe;
886         return 0;
887 }
888
889 static int
890 at86rf230_start(struct ieee802154_hw *hw)
891 {
892         struct at86rf230_local *lp = hw->priv;
893
894         at86rf230_awake(lp);
895         enable_irq(lp->spi->irq);
896
897         return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
898 }
899
900 static void
901 at86rf230_stop(struct ieee802154_hw *hw)
902 {
903         struct at86rf230_local *lp = hw->priv;
904         u8 csma_seed[2];
905
906         at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
907
908         disable_irq(lp->spi->irq);
909
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.
914          */
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]);
918
919         at86rf230_sleep(lp);
920 }
921
922 static int
923 at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
924 {
925         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
926 }
927
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,
932 };
933
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,
937 };
938
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,
942 };
943
944 static inline int
945 at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
946 {
947         unsigned int cca_ed_thres;
948         int rc;
949
950         rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
951         if (rc < 0)
952                 return rc;
953
954         switch (rssi_base_val) {
955         case -98:
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];
959                 break;
960         case -100:
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];
964                 break;
965         default:
966                 WARN_ON(1);
967         }
968
969         return 0;
970 }
971
972 static int
973 at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
974 {
975         int rc;
976
977         if (channel == 0)
978                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
979         else
980                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
981         if (rc < 0)
982                 return rc;
983
984         if (page == 0) {
985                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
986                 lp->data->rssi_base_val = -100;
987         } else {
988                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
989                 lp->data->rssi_base_val = -98;
990         }
991         if (rc < 0)
992                 return rc;
993
994         rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
995         if (rc < 0)
996                 return rc;
997
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.
1002          *
1003          * This should also protected from ifs_timer. Means cancel timer and
1004          * init with a new value. For now, this is okay.
1005          */
1006         if (channel == 0) {
1007                 if (page == 0) {
1008                         /* SUB:0 and BPSK:0 -> BPSK-20 */
1009                         lp->hw->phy->symbol_duration = 50;
1010                 } else {
1011                         /* SUB:1 and BPSK:0 -> BPSK-40 */
1012                         lp->hw->phy->symbol_duration = 25;
1013                 }
1014         } else {
1015                 if (page == 0)
1016                         /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
1017                         lp->hw->phy->symbol_duration = 40;
1018                 else
1019                         /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1020                         lp->hw->phy->symbol_duration = 16;
1021         }
1022
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;
1027
1028         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1029 }
1030
1031 static int
1032 at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1033 {
1034         struct at86rf230_local *lp = hw->priv;
1035         int rc;
1036
1037         rc = lp->data->set_channel(lp, page, channel);
1038         /* Wait for PLL */
1039         usleep_range(lp->data->t_channel_switch,
1040                      lp->data->t_channel_switch + 10);
1041
1042         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
1043         return rc;
1044 }
1045
1046 static int
1047 at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1048                            struct ieee802154_hw_addr_filt *filt,
1049                            unsigned long changed)
1050 {
1051         struct at86rf230_local *lp = hw->priv;
1052
1053         if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1054                 u16 addr = le16_to_cpu(filt->short_addr);
1055
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);
1060         }
1061
1062         if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1063                 u16 pan = le16_to_cpu(filt->pan_id);
1064
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);
1069         }
1070
1071         if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1072                 u8 i, addr[8];
1073
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]);
1079         }
1080
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);
1086                 else
1087                         at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1088         }
1089
1090         return 0;
1091 }
1092
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,
1096         -800, -1200, -1700,
1097 };
1098
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,
1101         -900, -1200, -1700,
1102 };
1103
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,
1109 };
1110
1111 static int
1112 at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1113 {
1114         u32 i;
1115
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);
1119         }
1120
1121         return -EINVAL;
1122 }
1123
1124 static int
1125 at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1126 {
1127         u32 i;
1128
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);
1132         }
1133
1134         return -EINVAL;
1135 }
1136
1137 static int
1138 at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
1139 {
1140         struct at86rf230_local *lp = hw->priv;
1141
1142         return lp->data->set_txpower(lp, mbm);
1143 }
1144
1145 static int
1146 at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1147 {
1148         struct at86rf230_local *lp = hw->priv;
1149
1150         return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1151 }
1152
1153 static int
1154 at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1155                        const struct wpan_phy_cca *cca)
1156 {
1157         struct at86rf230_local *lp = hw->priv;
1158         u8 val;
1159
1160         /* mapping 802.15.4 to driver spec */
1161         switch (cca->mode) {
1162         case NL802154_CCA_ENERGY:
1163                 val = 1;
1164                 break;
1165         case NL802154_CCA_CARRIER:
1166                 val = 2;
1167                 break;
1168         case NL802154_CCA_ENERGY_CARRIER:
1169                 switch (cca->opt) {
1170                 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1171                         val = 3;
1172                         break;
1173                 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1174                         val = 0;
1175                         break;
1176                 default:
1177                         return -EINVAL;
1178                 }
1179                 break;
1180         default:
1181                 return -EINVAL;
1182         }
1183
1184         return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1185 }
1186
1187
1188 static int
1189 at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
1190 {
1191         struct at86rf230_local *lp = hw->priv;
1192         u32 i;
1193
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);
1197         }
1198
1199         return -EINVAL;
1200 }
1201
1202 static int
1203 at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1204                           u8 retries)
1205 {
1206         struct at86rf230_local *lp = hw->priv;
1207         int rc;
1208
1209         rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1210         if (rc)
1211                 return rc;
1212
1213         rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1214         if (rc)
1215                 return rc;
1216
1217         return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1218 }
1219
1220 static int
1221 at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1222 {
1223         struct at86rf230_local *lp = hw->priv;
1224
1225         return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1226 }
1227
1228 static int
1229 at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1230 {
1231         struct at86rf230_local *lp = hw->priv;
1232         int rc;
1233
1234         if (on) {
1235                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1236                 if (rc < 0)
1237                         return rc;
1238
1239                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1240                 if (rc < 0)
1241                         return rc;
1242         } else {
1243                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1244                 if (rc < 0)
1245                         return rc;
1246
1247                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1248                 if (rc < 0)
1249                         return rc;
1250         }
1251
1252         return 0;
1253 }
1254
1255 static const struct ieee802154_ops at86rf230_ops = {
1256         .owner = THIS_MODULE,
1257         .xmit_async = at86rf230_xmit,
1258         .ed = at86rf230_ed,
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,
1270 };
1271
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,
1280         .t_frame = 4096,
1281         .t_p_ack = 545,
1282         .rssi_base_val = -91,
1283         .set_channel = at86rf23x_set_channel,
1284         .set_txpower = at86rf23x_set_txpower,
1285 };
1286
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,
1295         .t_frame = 4096,
1296         .t_p_ack = 545,
1297         .rssi_base_val = -91,
1298         .set_channel = at86rf23x_set_channel,
1299         .set_txpower = at86rf23x_set_txpower,
1300 };
1301
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,
1310         .t_frame = 4096,
1311         .t_p_ack = 545,
1312         .rssi_base_val = -100,
1313         .set_channel = at86rf212_set_channel,
1314         .set_txpower = at86rf212_set_txpower,
1315 };
1316
1317 static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1318 {
1319         int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
1320         unsigned int dvdd;
1321         u8 csma_seed[2];
1322
1323         rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1324         if (rc)
1325                 return rc;
1326
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;
1335
1336         rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1337         if (rc)
1338                 return rc;
1339
1340         rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1341         if (rc)
1342                 return rc;
1343
1344         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1345         if (rc)
1346                 return rc;
1347
1348         /* reset values differs in at86rf231 and at86rf233 */
1349         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1350         if (rc)
1351                 return rc;
1352
1353         get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1354         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1355         if (rc)
1356                 return rc;
1357         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1358         if (rc)
1359                 return rc;
1360
1361         /* CLKM changes are applied immediately */
1362         rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1363         if (rc)
1364                 return rc;
1365
1366         /* Turn CLKM Off */
1367         rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1368         if (rc)
1369                 return rc;
1370         /* Wait the next SLEEP cycle */
1371         usleep_range(lp->data->t_sleep_cycle,
1372                      lp->data->t_sleep_cycle + 100);
1373
1374         /* xtal_trim value is calculated by:
1375          * CL = 0.5 * (CX + CTRIM + CPAR)
1376          *
1377          * whereas:
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
1384          *         0 pF upto 4.5 pF.
1385          *
1386          * Examples:
1387          * atben transceiver:
1388          *
1389          * CL = 8 pF
1390          * CX = 12 pF
1391          * CPAR = 3 pF (We assume the magic constant from datasheet)
1392          * CTRIM = 0.9 pF
1393          *
1394          * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1395          *
1396          * xtal_trim = 0x3
1397          *
1398          * openlabs transceiver:
1399          *
1400          * CL = 16 pF
1401          * CX = 22 pF
1402          * CPAR = 3 pF (We assume the magic constant from datasheet)
1403          * CTRIM = 4.5 pF
1404          *
1405          * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1406          *
1407          * xtal_trim = 0xf
1408          */
1409         rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1410         if (rc)
1411                 return rc;
1412
1413         rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1414         if (rc)
1415                 return rc;
1416         if (!dvdd) {
1417                 dev_err(&lp->spi->dev, "DVDD error\n");
1418                 return -EINVAL;
1419         }
1420
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.
1424          */
1425         return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1426 }
1427
1428 static int
1429 at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1430                     u8 *xtal_trim)
1431 {
1432         struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1433         int ret;
1434
1435         if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1436                 if (!pdata)
1437                         return -ENOENT;
1438
1439                 *rstn = pdata->rstn;
1440                 *slp_tr = pdata->slp_tr;
1441                 *xtal_trim = pdata->xtal_trim;
1442                 return 0;
1443         }
1444
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)
1449                 return ret;
1450
1451         return 0;
1452 }
1453
1454 static int
1455 at86rf230_detect_device(struct at86rf230_local *lp)
1456 {
1457         unsigned int part, version, val;
1458         u16 man_id = 0;
1459         const char *chip;
1460         int rc;
1461
1462         rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1463         if (rc)
1464                 return rc;
1465         man_id |= val;
1466
1467         rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1468         if (rc)
1469                 return rc;
1470         man_id |= (val << 8);
1471
1472         rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1473         if (rc)
1474                 return rc;
1475
1476         rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1477         if (rc)
1478                 return rc;
1479
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);
1483                 return -EINVAL;
1484         }
1485
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;
1490
1491         lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1492                              WPAN_PHY_FLAG_CCA_ED_LEVEL |
1493                              WPAN_PHY_FLAG_CCA_MODE;
1494
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);
1499
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);
1502
1503         lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1504
1505         switch (part) {
1506         case 2:
1507                 chip = "at86rf230";
1508                 rc = -ENOTSUPP;
1509                 goto not_supp;
1510         case 3:
1511                 chip = "at86rf231";
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);
1518                 break;
1519         case 7:
1520                 chip = "at86rf212";
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);
1532                 break;
1533         case 11:
1534                 chip = "at86rf233";
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);
1541                 break;
1542         default:
1543                 chip = "unknown";
1544                 rc = -ENOTSUPP;
1545                 goto not_supp;
1546         }
1547
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];
1550
1551 not_supp:
1552         dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1553
1554         return rc;
1555 }
1556
1557 static void
1558 at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1559 {
1560         lp->state.lp = 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;
1570
1571         lp->irq.lp = lp;
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;
1581
1582         lp->tx.lp = lp;
1583         lp->tx.irq = lp->spi->irq;
1584         spi_message_init(&lp->tx.msg);
1585         lp->tx.msg.context = &lp->tx;
1586         lp->tx.trx.len = 2;
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;
1592 }
1593
1594 static int at86rf230_probe(struct spi_device *spi)
1595 {
1596         struct ieee802154_hw *hw;
1597         struct at86rf230_local *lp;
1598         unsigned int status;
1599         int rc, irq_type, rstn, slp_tr;
1600         u8 xtal_trim = 0;
1601
1602         if (!spi->irq) {
1603                 dev_err(&spi->dev, "no IRQ specified\n");
1604                 return -EINVAL;
1605         }
1606
1607         rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
1608         if (rc < 0) {
1609                 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1610                 return rc;
1611         }
1612
1613         if (gpio_is_valid(rstn)) {
1614                 rc = devm_gpio_request_one(&spi->dev, rstn,
1615                                            GPIOF_OUT_INIT_HIGH, "rstn");
1616                 if (rc)
1617                         return rc;
1618         }
1619
1620         if (gpio_is_valid(slp_tr)) {
1621                 rc = devm_gpio_request_one(&spi->dev, slp_tr,
1622                                            GPIOF_OUT_INIT_LOW, "slp_tr");
1623                 if (rc)
1624                         return rc;
1625         }
1626
1627         /* Reset */
1628         if (gpio_is_valid(rstn)) {
1629                 udelay(1);
1630                 gpio_set_value(rstn, 0);
1631                 udelay(1);
1632                 gpio_set_value(rstn, 1);
1633                 usleep_range(120, 240);
1634         }
1635
1636         hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1637         if (!hw)
1638                 return -ENOMEM;
1639
1640         lp = hw->priv;
1641         lp->hw = hw;
1642         lp->spi = spi;
1643         lp->slp_tr = slp_tr;
1644         hw->parent = &spi->dev;
1645         ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1646
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",
1651                         rc);
1652                 goto free_dev;
1653         }
1654
1655         at86rf230_setup_spi_messages(lp);
1656
1657         rc = at86rf230_detect_device(lp);
1658         if (rc < 0)
1659                 goto free_dev;
1660
1661         init_completion(&lp->state_complete);
1662
1663         spi_set_drvdata(spi, lp);
1664
1665         rc = at86rf230_hw_init(lp, xtal_trim);
1666         if (rc)
1667                 goto free_dev;
1668
1669         /* Read irq status register to reset irq line */
1670         rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1671         if (rc)
1672                 goto free_dev;
1673
1674         irq_type = irq_get_trigger_type(spi->irq);
1675         if (!irq_type)
1676                 irq_type = IRQF_TRIGGER_HIGH;
1677
1678         rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1679                               IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1680         if (rc)
1681                 goto free_dev;
1682
1683         /* disable_irq by default and wait for starting hardware */
1684         disable_irq(spi->irq);
1685
1686         /* going into sleep by default */
1687         at86rf230_sleep(lp);
1688
1689         rc = ieee802154_register_hw(lp->hw);
1690         if (rc)
1691                 goto free_dev;
1692
1693         return rc;
1694
1695 free_dev:
1696         ieee802154_free_hw(lp->hw);
1697
1698         return rc;
1699 }
1700
1701 static int at86rf230_remove(struct spi_device *spi)
1702 {
1703         struct at86rf230_local *lp = spi_get_drvdata(spi);
1704
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");
1710
1711         return 0;
1712 }
1713
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", },
1719         { },
1720 };
1721 MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1722
1723 static const struct spi_device_id at86rf230_device_id[] = {
1724         { .name = "at86rf230", },
1725         { .name = "at86rf231", },
1726         { .name = "at86rf233", },
1727         { .name = "at86rf212", },
1728         { },
1729 };
1730 MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1731
1732 static struct spi_driver at86rf230_driver = {
1733         .id_table = at86rf230_device_id,
1734         .driver = {
1735                 .of_match_table = of_match_ptr(at86rf230_of_match),
1736                 .name   = "at86rf230",
1737                 .owner  = THIS_MODULE,
1738         },
1739         .probe      = at86rf230_probe,
1740         .remove     = at86rf230_remove,
1741 };
1742
1743 module_spi_driver(at86rf230_driver);
1744
1745 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1746 MODULE_LICENSE("GPL v2");