]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ieee802154/at86rf230.c
Merge tag 'mac80211-next-for-davem-2015-08-14' of git://git.kernel.org/pub/scm/linux...
[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         bool tx_aret;
101         unsigned long cal_timeout;
102         s8 max_frame_retries;
103         bool is_tx;
104         bool is_tx_from_off;
105         u8 tx_retry;
106         struct sk_buff *tx_skb;
107         struct at86rf230_state_change tx;
108 };
109
110 #define AT86RF2XX_NUMREGS 0x3F
111
112 static void
113 at86rf230_async_state_change(struct at86rf230_local *lp,
114                              struct at86rf230_state_change *ctx,
115                              const u8 state, void (*complete)(void *context),
116                              const bool irq_enable);
117
118 static inline void
119 at86rf230_sleep(struct at86rf230_local *lp)
120 {
121         if (gpio_is_valid(lp->slp_tr)) {
122                 gpio_set_value(lp->slp_tr, 1);
123                 usleep_range(lp->data->t_off_to_sleep,
124                              lp->data->t_off_to_sleep + 10);
125                 lp->sleep = true;
126         }
127 }
128
129 static inline void
130 at86rf230_awake(struct at86rf230_local *lp)
131 {
132         if (gpio_is_valid(lp->slp_tr)) {
133                 gpio_set_value(lp->slp_tr, 0);
134                 usleep_range(lp->data->t_sleep_to_off,
135                              lp->data->t_sleep_to_off + 100);
136                 lp->sleep = false;
137         }
138 }
139
140 static inline int
141 __at86rf230_write(struct at86rf230_local *lp,
142                   unsigned int addr, unsigned int data)
143 {
144         bool sleep = lp->sleep;
145         int ret;
146
147         /* awake for register setting if sleep */
148         if (sleep)
149                 at86rf230_awake(lp);
150
151         ret = regmap_write(lp->regmap, addr, data);
152
153         /* sleep again if was sleeping */
154         if (sleep)
155                 at86rf230_sleep(lp);
156
157         return ret;
158 }
159
160 static inline int
161 __at86rf230_read(struct at86rf230_local *lp,
162                  unsigned int addr, unsigned int *data)
163 {
164         bool sleep = lp->sleep;
165         int ret;
166
167         /* awake for register setting if sleep */
168         if (sleep)
169                 at86rf230_awake(lp);
170
171         ret = regmap_read(lp->regmap, addr, data);
172
173         /* sleep again if was sleeping */
174         if (sleep)
175                 at86rf230_sleep(lp);
176
177         return ret;
178 }
179
180 static inline int
181 at86rf230_read_subreg(struct at86rf230_local *lp,
182                       unsigned int addr, unsigned int mask,
183                       unsigned int shift, unsigned int *data)
184 {
185         int rc;
186
187         rc = __at86rf230_read(lp, addr, data);
188         if (!rc)
189                 *data = (*data & mask) >> shift;
190
191         return rc;
192 }
193
194 static inline int
195 at86rf230_write_subreg(struct at86rf230_local *lp,
196                        unsigned int addr, unsigned int mask,
197                        unsigned int shift, unsigned int data)
198 {
199         bool sleep = lp->sleep;
200         int ret;
201
202         /* awake for register setting if sleep */
203         if (sleep)
204                 at86rf230_awake(lp);
205
206         ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
207
208         /* sleep again if was sleeping */
209         if (sleep)
210                 at86rf230_sleep(lp);
211
212         return ret;
213 }
214
215 static inline void
216 at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
217 {
218         gpio_set_value(lp->slp_tr, 1);
219         udelay(1);
220         gpio_set_value(lp->slp_tr, 0);
221 }
222
223 static bool
224 at86rf230_reg_writeable(struct device *dev, unsigned int reg)
225 {
226         switch (reg) {
227         case RG_TRX_STATE:
228         case RG_TRX_CTRL_0:
229         case RG_TRX_CTRL_1:
230         case RG_PHY_TX_PWR:
231         case RG_PHY_ED_LEVEL:
232         case RG_PHY_CC_CCA:
233         case RG_CCA_THRES:
234         case RG_RX_CTRL:
235         case RG_SFD_VALUE:
236         case RG_TRX_CTRL_2:
237         case RG_ANT_DIV:
238         case RG_IRQ_MASK:
239         case RG_VREG_CTRL:
240         case RG_BATMON:
241         case RG_XOSC_CTRL:
242         case RG_RX_SYN:
243         case RG_XAH_CTRL_1:
244         case RG_FTN_CTRL:
245         case RG_PLL_CF:
246         case RG_PLL_DCU:
247         case RG_SHORT_ADDR_0:
248         case RG_SHORT_ADDR_1:
249         case RG_PAN_ID_0:
250         case RG_PAN_ID_1:
251         case RG_IEEE_ADDR_0:
252         case RG_IEEE_ADDR_1:
253         case RG_IEEE_ADDR_2:
254         case RG_IEEE_ADDR_3:
255         case RG_IEEE_ADDR_4:
256         case RG_IEEE_ADDR_5:
257         case RG_IEEE_ADDR_6:
258         case RG_IEEE_ADDR_7:
259         case RG_XAH_CTRL_0:
260         case RG_CSMA_SEED_0:
261         case RG_CSMA_SEED_1:
262         case RG_CSMA_BE:
263                 return true;
264         default:
265                 return false;
266         }
267 }
268
269 static bool
270 at86rf230_reg_readable(struct device *dev, unsigned int reg)
271 {
272         bool rc;
273
274         /* all writeable are also readable */
275         rc = at86rf230_reg_writeable(dev, reg);
276         if (rc)
277                 return rc;
278
279         /* readonly regs */
280         switch (reg) {
281         case RG_TRX_STATUS:
282         case RG_PHY_RSSI:
283         case RG_IRQ_STATUS:
284         case RG_PART_NUM:
285         case RG_VERSION_NUM:
286         case RG_MAN_ID_1:
287         case RG_MAN_ID_0:
288                 return true;
289         default:
290                 return false;
291         }
292 }
293
294 static bool
295 at86rf230_reg_volatile(struct device *dev, unsigned int reg)
296 {
297         /* can be changed during runtime */
298         switch (reg) {
299         case RG_TRX_STATUS:
300         case RG_TRX_STATE:
301         case RG_PHY_RSSI:
302         case RG_PHY_ED_LEVEL:
303         case RG_IRQ_STATUS:
304         case RG_VREG_CTRL:
305         case RG_PLL_CF:
306         case RG_PLL_DCU:
307                 return true;
308         default:
309                 return false;
310         }
311 }
312
313 static bool
314 at86rf230_reg_precious(struct device *dev, unsigned int reg)
315 {
316         /* don't clear irq line on read */
317         switch (reg) {
318         case RG_IRQ_STATUS:
319                 return true;
320         default:
321                 return false;
322         }
323 }
324
325 static const struct regmap_config at86rf230_regmap_spi_config = {
326         .reg_bits = 8,
327         .val_bits = 8,
328         .write_flag_mask = CMD_REG | CMD_WRITE,
329         .read_flag_mask = CMD_REG,
330         .cache_type = REGCACHE_RBTREE,
331         .max_register = AT86RF2XX_NUMREGS,
332         .writeable_reg = at86rf230_reg_writeable,
333         .readable_reg = at86rf230_reg_readable,
334         .volatile_reg = at86rf230_reg_volatile,
335         .precious_reg = at86rf230_reg_precious,
336 };
337
338 static void
339 at86rf230_async_error_recover(void *context)
340 {
341         struct at86rf230_state_change *ctx = context;
342         struct at86rf230_local *lp = ctx->lp;
343
344         lp->is_tx = 0;
345         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
346         ieee802154_wake_queue(lp->hw);
347 }
348
349 static inline void
350 at86rf230_async_error(struct at86rf230_local *lp,
351                       struct at86rf230_state_change *ctx, int rc)
352 {
353         dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
354
355         at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
356                                      at86rf230_async_error_recover, false);
357 }
358
359 /* Generic function to get some register value in async mode */
360 static void
361 at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
362                          struct at86rf230_state_change *ctx,
363                          void (*complete)(void *context),
364                          const bool irq_enable)
365 {
366         int rc;
367
368         u8 *tx_buf = ctx->buf;
369
370         tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
371         ctx->msg.complete = complete;
372         ctx->irq_enable = irq_enable;
373         rc = spi_async(lp->spi, &ctx->msg);
374         if (rc) {
375                 if (irq_enable)
376                         enable_irq(ctx->irq);
377
378                 at86rf230_async_error(lp, ctx, rc);
379         }
380 }
381
382 static inline u8 at86rf230_state_to_force(u8 state)
383 {
384         if (state == STATE_TX_ON)
385                 return STATE_FORCE_TX_ON;
386         else
387                 return STATE_FORCE_TRX_OFF;
388 }
389
390 static void
391 at86rf230_async_state_assert(void *context)
392 {
393         struct at86rf230_state_change *ctx = context;
394         struct at86rf230_local *lp = ctx->lp;
395         const u8 *buf = ctx->buf;
396         const u8 trx_state = buf[1] & TRX_STATE_MASK;
397
398         /* Assert state change */
399         if (trx_state != ctx->to_state) {
400                 /* Special handling if transceiver state is in
401                  * STATE_BUSY_RX_AACK and a SHR was detected.
402                  */
403                 if  (trx_state == STATE_BUSY_RX_AACK) {
404                         /* Undocumented race condition. If we send a state
405                          * change to STATE_RX_AACK_ON the transceiver could
406                          * change his state automatically to STATE_BUSY_RX_AACK
407                          * if a SHR was detected. This is not an error, but we
408                          * can't assert this.
409                          */
410                         if (ctx->to_state == STATE_RX_AACK_ON)
411                                 goto done;
412
413                         /* If we change to STATE_TX_ON without forcing and
414                          * transceiver state is STATE_BUSY_RX_AACK, we wait
415                          * 'tFrame + tPAck' receiving time. In this time the
416                          * PDU should be received. If the transceiver is still
417                          * in STATE_BUSY_RX_AACK, we run a force state change
418                          * to STATE_TX_ON. This is a timeout handling, if the
419                          * transceiver stucks in STATE_BUSY_RX_AACK.
420                          *
421                          * Additional we do several retries to try to get into
422                          * TX_ON state without forcing. If the retries are
423                          * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
424                          * will do a force change.
425                          */
426                         if (ctx->to_state == STATE_TX_ON ||
427                             ctx->to_state == STATE_TRX_OFF) {
428                                 u8 state = ctx->to_state;
429
430                                 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
431                                         state = at86rf230_state_to_force(state);
432                                 lp->tx_retry++;
433
434                                 at86rf230_async_state_change(lp, ctx, state,
435                                                              ctx->complete,
436                                                              ctx->irq_enable);
437                                 return;
438                         }
439                 }
440
441                 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
442                          ctx->from_state, ctx->to_state, trx_state);
443         }
444
445 done:
446         if (ctx->complete)
447                 ctx->complete(context);
448 }
449
450 static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
451 {
452         struct at86rf230_state_change *ctx =
453                 container_of(timer, struct at86rf230_state_change, timer);
454         struct at86rf230_local *lp = ctx->lp;
455
456         at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
457                                  at86rf230_async_state_assert,
458                                  ctx->irq_enable);
459
460         return HRTIMER_NORESTART;
461 }
462
463 /* Do state change timing delay. */
464 static void
465 at86rf230_async_state_delay(void *context)
466 {
467         struct at86rf230_state_change *ctx = context;
468         struct at86rf230_local *lp = ctx->lp;
469         struct at86rf2xx_chip_data *c = lp->data;
470         bool force = false;
471         ktime_t tim;
472
473         /* The force state changes are will show as normal states in the
474          * state status subregister. We change the to_state to the
475          * corresponding one and remember if it was a force change, this
476          * differs if we do a state change from STATE_BUSY_RX_AACK.
477          */
478         switch (ctx->to_state) {
479         case STATE_FORCE_TX_ON:
480                 ctx->to_state = STATE_TX_ON;
481                 force = true;
482                 break;
483         case STATE_FORCE_TRX_OFF:
484                 ctx->to_state = STATE_TRX_OFF;
485                 force = true;
486                 break;
487         default:
488                 break;
489         }
490
491         switch (ctx->from_state) {
492         case STATE_TRX_OFF:
493                 switch (ctx->to_state) {
494                 case STATE_RX_AACK_ON:
495                         tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
496                         /* state change from TRX_OFF to RX_AACK_ON to do a
497                          * calibration, we need to reset the timeout for the
498                          * next one.
499                          */
500                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
501                         goto change;
502                 case STATE_TX_ARET_ON:
503                 case STATE_TX_ON:
504                         tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
505                         /* state change from TRX_OFF to TX_ON or ARET_ON to do
506                          * a calibration, we need to reset the timeout for the
507                          * next one.
508                          */
509                         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
510                         goto change;
511                 default:
512                         break;
513                 }
514                 break;
515         case STATE_BUSY_RX_AACK:
516                 switch (ctx->to_state) {
517                 case STATE_TRX_OFF:
518                 case STATE_TX_ON:
519                         /* Wait for worst case receiving time if we
520                          * didn't make a force change from BUSY_RX_AACK
521                          * to TX_ON or TRX_OFF.
522                          */
523                         if (!force) {
524                                 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
525                                                    NSEC_PER_USEC);
526                                 goto change;
527                         }
528                         break;
529                 default:
530                         break;
531                 }
532                 break;
533         /* Default value, means RESET state */
534         case STATE_P_ON:
535                 switch (ctx->to_state) {
536                 case STATE_TRX_OFF:
537                         tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
538                         goto change;
539                 default:
540                         break;
541                 }
542                 break;
543         default:
544                 break;
545         }
546
547         /* Default delay is 1us in the most cases */
548         udelay(1);
549         at86rf230_async_state_timer(&ctx->timer);
550         return;
551
552 change:
553         hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
554 }
555
556 static void
557 at86rf230_async_state_change_start(void *context)
558 {
559         struct at86rf230_state_change *ctx = context;
560         struct at86rf230_local *lp = ctx->lp;
561         u8 *buf = ctx->buf;
562         const u8 trx_state = buf[1] & TRX_STATE_MASK;
563         int rc;
564
565         /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
566         if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
567                 udelay(1);
568                 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
569                                          at86rf230_async_state_change_start,
570                                          ctx->irq_enable);
571                 return;
572         }
573
574         /* Check if we already are in the state which we change in */
575         if (trx_state == ctx->to_state) {
576                 if (ctx->complete)
577                         ctx->complete(context);
578                 return;
579         }
580
581         /* Set current state to the context of state change */
582         ctx->from_state = trx_state;
583
584         /* Going into the next step for a state change which do a timing
585          * relevant delay.
586          */
587         buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
588         buf[1] = ctx->to_state;
589         ctx->msg.complete = at86rf230_async_state_delay;
590         rc = spi_async(lp->spi, &ctx->msg);
591         if (rc) {
592                 if (ctx->irq_enable)
593                         enable_irq(ctx->irq);
594
595                 at86rf230_async_error(lp, ctx, rc);
596         }
597 }
598
599 static void
600 at86rf230_async_state_change(struct at86rf230_local *lp,
601                              struct at86rf230_state_change *ctx,
602                              const u8 state, void (*complete)(void *context),
603                              const bool irq_enable)
604 {
605         /* Initialization for the state change context */
606         ctx->to_state = state;
607         ctx->complete = complete;
608         ctx->irq_enable = irq_enable;
609         at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
610                                  at86rf230_async_state_change_start,
611                                  irq_enable);
612 }
613
614 static void
615 at86rf230_sync_state_change_complete(void *context)
616 {
617         struct at86rf230_state_change *ctx = context;
618         struct at86rf230_local *lp = ctx->lp;
619
620         complete(&lp->state_complete);
621 }
622
623 /* This function do a sync framework above the async state change.
624  * Some callbacks of the IEEE 802.15.4 driver interface need to be
625  * handled synchronously.
626  */
627 static int
628 at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
629 {
630         unsigned long rc;
631
632         at86rf230_async_state_change(lp, &lp->state, state,
633                                      at86rf230_sync_state_change_complete,
634                                      false);
635
636         rc = wait_for_completion_timeout(&lp->state_complete,
637                                          msecs_to_jiffies(100));
638         if (!rc) {
639                 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
640                 return -ETIMEDOUT;
641         }
642
643         return 0;
644 }
645
646 static void
647 at86rf230_tx_complete(void *context)
648 {
649         struct at86rf230_state_change *ctx = context;
650         struct at86rf230_local *lp = ctx->lp;
651
652         enable_irq(ctx->irq);
653
654         ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
655 }
656
657 static void
658 at86rf230_tx_on(void *context)
659 {
660         struct at86rf230_state_change *ctx = context;
661         struct at86rf230_local *lp = ctx->lp;
662
663         at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
664                                      at86rf230_tx_complete, true);
665 }
666
667 static void
668 at86rf230_tx_trac_check(void *context)
669 {
670         struct at86rf230_state_change *ctx = context;
671         struct at86rf230_local *lp = ctx->lp;
672         const u8 *buf = ctx->buf;
673         const u8 trac = (buf[1] & 0xe0) >> 5;
674
675         /* If trac status is different than zero we need to do a state change
676          * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the
677          * transceiver.
678          */
679         if (trac)
680                 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
681                                              at86rf230_tx_on, true);
682         else
683                 at86rf230_tx_on(context);
684 }
685
686 static void
687 at86rf230_tx_trac_status(void *context)
688 {
689         struct at86rf230_state_change *ctx = context;
690         struct at86rf230_local *lp = ctx->lp;
691
692         at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
693                                  at86rf230_tx_trac_check, true);
694 }
695
696 static void
697 at86rf230_rx_read_frame_complete(void *context)
698 {
699         struct at86rf230_state_change *ctx = context;
700         struct at86rf230_local *lp = ctx->lp;
701         u8 rx_local_buf[AT86RF2XX_MAX_BUF];
702         const u8 *buf = ctx->buf;
703         struct sk_buff *skb;
704         u8 len, lqi;
705
706         len = buf[1];
707         if (!ieee802154_is_valid_psdu_len(len)) {
708                 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
709                 len = IEEE802154_MTU;
710         }
711         lqi = buf[2 + len];
712
713         memcpy(rx_local_buf, buf + 2, len);
714         ctx->trx.len = 2;
715         enable_irq(ctx->irq);
716
717         skb = dev_alloc_skb(IEEE802154_MTU);
718         if (!skb) {
719                 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
720                 return;
721         }
722
723         memcpy(skb_put(skb, len), rx_local_buf, len);
724         ieee802154_rx_irqsafe(lp->hw, skb, lqi);
725 }
726
727 static void
728 at86rf230_rx_read_frame(void *context)
729 {
730         struct at86rf230_state_change *ctx = context;
731         struct at86rf230_local *lp = ctx->lp;
732         u8 *buf = ctx->buf;
733         int rc;
734
735         buf[0] = CMD_FB;
736         ctx->trx.len = AT86RF2XX_MAX_BUF;
737         ctx->msg.complete = at86rf230_rx_read_frame_complete;
738         rc = spi_async(lp->spi, &ctx->msg);
739         if (rc) {
740                 ctx->trx.len = 2;
741                 enable_irq(ctx->irq);
742                 at86rf230_async_error(lp, ctx, rc);
743         }
744 }
745
746 static void
747 at86rf230_rx_trac_check(void *context)
748 {
749         /* Possible check on trac status here. This could be useful to make
750          * some stats why receive is failed. Not used at the moment, but it's
751          * maybe timing relevant. Datasheet doesn't say anything about this.
752          * The programming guide say do it so.
753          */
754
755         at86rf230_rx_read_frame(context);
756 }
757
758 static void
759 at86rf230_irq_trx_end(struct at86rf230_local *lp)
760 {
761         if (lp->is_tx) {
762                 lp->is_tx = 0;
763
764                 if (lp->tx_aret)
765                         at86rf230_async_state_change(lp, &lp->irq,
766                                                      STATE_FORCE_TX_ON,
767                                                      at86rf230_tx_trac_status,
768                                                      true);
769                 else
770                         at86rf230_async_state_change(lp, &lp->irq,
771                                                      STATE_RX_AACK_ON,
772                                                      at86rf230_tx_complete,
773                                                      true);
774         } else {
775                 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
776                                          at86rf230_rx_trac_check, true);
777         }
778 }
779
780 static void
781 at86rf230_irq_status(void *context)
782 {
783         struct at86rf230_state_change *ctx = context;
784         struct at86rf230_local *lp = ctx->lp;
785         const u8 *buf = ctx->buf;
786         const u8 irq = buf[1];
787
788         if (irq & IRQ_TRX_END) {
789                 at86rf230_irq_trx_end(lp);
790         } else {
791                 enable_irq(ctx->irq);
792                 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
793                         irq);
794         }
795 }
796
797 static irqreturn_t at86rf230_isr(int irq, void *data)
798 {
799         struct at86rf230_local *lp = data;
800         struct at86rf230_state_change *ctx = &lp->irq;
801         u8 *buf = ctx->buf;
802         int rc;
803
804         disable_irq_nosync(irq);
805
806         buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
807         ctx->msg.complete = at86rf230_irq_status;
808         rc = spi_async(lp->spi, &ctx->msg);
809         if (rc) {
810                 enable_irq(irq);
811                 at86rf230_async_error(lp, ctx, rc);
812                 return IRQ_NONE;
813         }
814
815         return IRQ_HANDLED;
816 }
817
818 static void
819 at86rf230_write_frame_complete(void *context)
820 {
821         struct at86rf230_state_change *ctx = context;
822         struct at86rf230_local *lp = ctx->lp;
823         u8 *buf = ctx->buf;
824         int rc;
825
826         ctx->trx.len = 2;
827
828         if (gpio_is_valid(lp->slp_tr)) {
829                 at86rf230_slp_tr_rising_edge(lp);
830         } else {
831                 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
832                 buf[1] = STATE_BUSY_TX;
833                 ctx->msg.complete = NULL;
834                 rc = spi_async(lp->spi, &ctx->msg);
835                 if (rc)
836                         at86rf230_async_error(lp, ctx, rc);
837         }
838 }
839
840 static void
841 at86rf230_write_frame(void *context)
842 {
843         struct at86rf230_state_change *ctx = context;
844         struct at86rf230_local *lp = ctx->lp;
845         struct sk_buff *skb = lp->tx_skb;
846         u8 *buf = ctx->buf;
847         int rc;
848
849         lp->is_tx = 1;
850
851         buf[0] = CMD_FB | CMD_WRITE;
852         buf[1] = skb->len + 2;
853         memcpy(buf + 2, skb->data, skb->len);
854         ctx->trx.len = skb->len + 2;
855         ctx->msg.complete = at86rf230_write_frame_complete;
856         rc = spi_async(lp->spi, &ctx->msg);
857         if (rc) {
858                 ctx->trx.len = 2;
859                 at86rf230_async_error(lp, ctx, rc);
860         }
861 }
862
863 static void
864 at86rf230_xmit_tx_on(void *context)
865 {
866         struct at86rf230_state_change *ctx = context;
867         struct at86rf230_local *lp = ctx->lp;
868
869         at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
870                                      at86rf230_write_frame, false);
871 }
872
873 static void
874 at86rf230_xmit_start(void *context)
875 {
876         struct at86rf230_state_change *ctx = context;
877         struct at86rf230_local *lp = ctx->lp;
878
879         /* In ARET mode we need to go into STATE_TX_ARET_ON after we
880          * are in STATE_TX_ON. The pfad differs here, so we change
881          * the complete handler.
882          */
883         if (lp->tx_aret) {
884                 if (lp->is_tx_from_off) {
885                         lp->is_tx_from_off = false;
886                         at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
887                                                      at86rf230_write_frame,
888                                                      false);
889                 } else {
890                         at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
891                                                      at86rf230_xmit_tx_on,
892                                                      false);
893                 }
894         } else {
895                 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
896                                              at86rf230_write_frame, false);
897         }
898 }
899
900 static int
901 at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
902 {
903         struct at86rf230_local *lp = hw->priv;
904         struct at86rf230_state_change *ctx = &lp->tx;
905
906         lp->tx_skb = skb;
907         lp->tx_retry = 0;
908
909         /* After 5 minutes in PLL and the same frequency we run again the
910          * calibration loops which is recommended by at86rf2xx datasheets.
911          *
912          * The calibration is initiate by a state change from TRX_OFF
913          * to TX_ON, the lp->cal_timeout should be reinit by state_delay
914          * function then to start in the next 5 minutes.
915          */
916         if (time_is_before_jiffies(lp->cal_timeout)) {
917                 lp->is_tx_from_off = true;
918                 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
919                                              at86rf230_xmit_start, false);
920         } else {
921                 at86rf230_xmit_start(ctx);
922         }
923
924         return 0;
925 }
926
927 static int
928 at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
929 {
930         BUG_ON(!level);
931         *level = 0xbe;
932         return 0;
933 }
934
935 static int
936 at86rf230_start(struct ieee802154_hw *hw)
937 {
938         struct at86rf230_local *lp = hw->priv;
939
940         at86rf230_awake(lp);
941         enable_irq(lp->spi->irq);
942
943         return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
944 }
945
946 static void
947 at86rf230_stop(struct ieee802154_hw *hw)
948 {
949         struct at86rf230_local *lp = hw->priv;
950         u8 csma_seed[2];
951
952         at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
953
954         disable_irq(lp->spi->irq);
955
956         /* It's recommended to set random new csma_seeds before sleep state.
957          * Makes only sense in the stop callback, not doing this inside of
958          * at86rf230_sleep, this is also used when we don't transmit afterwards
959          * when calling start callback again.
960          */
961         get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
962         at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
963         at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
964
965         at86rf230_sleep(lp);
966 }
967
968 static int
969 at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
970 {
971         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
972 }
973
974 #define AT86RF2XX_MAX_ED_LEVELS 0xF
975 static const s32 at86rf23x_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
976         -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
977         -7100, -6900, -6700, -6500, -6300, -6100,
978 };
979
980 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
981         -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
982         -8000, -7800, -7600, -7400, -7200, -7000,
983 };
984
985 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
986         -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
987         -7800, -7600, -7400, -7200, -7000, -6800,
988 };
989
990 static inline int
991 at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
992 {
993         unsigned int cca_ed_thres;
994         int rc;
995
996         rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
997         if (rc < 0)
998                 return rc;
999
1000         switch (rssi_base_val) {
1001         case -98:
1002                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
1003                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
1004                 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
1005                 break;
1006         case -100:
1007                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1008                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1009                 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
1010                 break;
1011         default:
1012                 WARN_ON(1);
1013         }
1014
1015         return 0;
1016 }
1017
1018 static int
1019 at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
1020 {
1021         int rc;
1022
1023         if (channel == 0)
1024                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1025         else
1026                 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1027         if (rc < 0)
1028                 return rc;
1029
1030         if (page == 0) {
1031                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
1032                 lp->data->rssi_base_val = -100;
1033         } else {
1034                 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
1035                 lp->data->rssi_base_val = -98;
1036         }
1037         if (rc < 0)
1038                 return rc;
1039
1040         rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
1041         if (rc < 0)
1042                 return rc;
1043
1044         /* This sets the symbol_duration according frequency on the 212.
1045          * TODO move this handling while set channel and page in cfg802154.
1046          * We can do that, this timings are according 802.15.4 standard.
1047          * If we do that in cfg802154, this is a more generic calculation.
1048          *
1049          * This should also protected from ifs_timer. Means cancel timer and
1050          * init with a new value. For now, this is okay.
1051          */
1052         if (channel == 0) {
1053                 if (page == 0) {
1054                         /* SUB:0 and BPSK:0 -> BPSK-20 */
1055                         lp->hw->phy->symbol_duration = 50;
1056                 } else {
1057                         /* SUB:1 and BPSK:0 -> BPSK-40 */
1058                         lp->hw->phy->symbol_duration = 25;
1059                 }
1060         } else {
1061                 if (page == 0)
1062                         /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
1063                         lp->hw->phy->symbol_duration = 40;
1064                 else
1065                         /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
1066                         lp->hw->phy->symbol_duration = 16;
1067         }
1068
1069         lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1070                                    lp->hw->phy->symbol_duration;
1071         lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1072                                    lp->hw->phy->symbol_duration;
1073
1074         return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1075 }
1076
1077 static int
1078 at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
1079 {
1080         struct at86rf230_local *lp = hw->priv;
1081         int rc;
1082
1083         rc = lp->data->set_channel(lp, page, channel);
1084         /* Wait for PLL */
1085         usleep_range(lp->data->t_channel_switch,
1086                      lp->data->t_channel_switch + 10);
1087
1088         lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
1089         return rc;
1090 }
1091
1092 static int
1093 at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
1094                            struct ieee802154_hw_addr_filt *filt,
1095                            unsigned long changed)
1096 {
1097         struct at86rf230_local *lp = hw->priv;
1098
1099         if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
1100                 u16 addr = le16_to_cpu(filt->short_addr);
1101
1102                 dev_vdbg(&lp->spi->dev,
1103                          "at86rf230_set_hw_addr_filt called for saddr\n");
1104                 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1105                 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
1106         }
1107
1108         if (changed & IEEE802154_AFILT_PANID_CHANGED) {
1109                 u16 pan = le16_to_cpu(filt->pan_id);
1110
1111                 dev_vdbg(&lp->spi->dev,
1112                          "at86rf230_set_hw_addr_filt called for pan id\n");
1113                 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1114                 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
1115         }
1116
1117         if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
1118                 u8 i, addr[8];
1119
1120                 memcpy(addr, &filt->ieee_addr, 8);
1121                 dev_vdbg(&lp->spi->dev,
1122                          "at86rf230_set_hw_addr_filt called for IEEE addr\n");
1123                 for (i = 0; i < 8; i++)
1124                         __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
1125         }
1126
1127         if (changed & IEEE802154_AFILT_PANC_CHANGED) {
1128                 dev_vdbg(&lp->spi->dev,
1129                          "at86rf230_set_hw_addr_filt called for panc change\n");
1130                 if (filt->pan_coord)
1131                         at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1132                 else
1133                         at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1134         }
1135
1136         return 0;
1137 }
1138
1139 #define AT86RF23X_MAX_TX_POWERS 0xF
1140 static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1141         400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1142         -800, -1200, -1700,
1143 };
1144
1145 static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1146         300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1147         -900, -1200, -1700,
1148 };
1149
1150 #define AT86RF212_MAX_TX_POWERS 0x1F
1151 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1152         500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1153         -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1154         -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1155 };
1156
1157 static int
1158 at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1159 {
1160         u32 i;
1161
1162         for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1163                 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1164                         return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1165         }
1166
1167         return -EINVAL;
1168 }
1169
1170 static int
1171 at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1172 {
1173         u32 i;
1174
1175         for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1176                 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1177                         return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1178         }
1179
1180         return -EINVAL;
1181 }
1182
1183 static int
1184 at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
1185 {
1186         struct at86rf230_local *lp = hw->priv;
1187
1188         return lp->data->set_txpower(lp, mbm);
1189 }
1190
1191 static int
1192 at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
1193 {
1194         struct at86rf230_local *lp = hw->priv;
1195
1196         return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1197 }
1198
1199 static int
1200 at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1201                        const struct wpan_phy_cca *cca)
1202 {
1203         struct at86rf230_local *lp = hw->priv;
1204         u8 val;
1205
1206         /* mapping 802.15.4 to driver spec */
1207         switch (cca->mode) {
1208         case NL802154_CCA_ENERGY:
1209                 val = 1;
1210                 break;
1211         case NL802154_CCA_CARRIER:
1212                 val = 2;
1213                 break;
1214         case NL802154_CCA_ENERGY_CARRIER:
1215                 switch (cca->opt) {
1216                 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1217                         val = 3;
1218                         break;
1219                 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1220                         val = 0;
1221                         break;
1222                 default:
1223                         return -EINVAL;
1224                 }
1225                 break;
1226         default:
1227                 return -EINVAL;
1228         }
1229
1230         return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
1231 }
1232
1233
1234 static int
1235 at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
1236 {
1237         struct at86rf230_local *lp = hw->priv;
1238         u32 i;
1239
1240         for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1241                 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1242                         return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1243         }
1244
1245         return -EINVAL;
1246 }
1247
1248 static int
1249 at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
1250                           u8 retries)
1251 {
1252         struct at86rf230_local *lp = hw->priv;
1253         int rc;
1254
1255         rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1256         if (rc)
1257                 return rc;
1258
1259         rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1260         if (rc)
1261                 return rc;
1262
1263         return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
1264 }
1265
1266 static int
1267 at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
1268 {
1269         struct at86rf230_local *lp = hw->priv;
1270         int rc = 0;
1271
1272         lp->tx_aret = retries >= 0;
1273         lp->max_frame_retries = retries;
1274
1275         if (retries >= 0)
1276                 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1277
1278         return rc;
1279 }
1280
1281 static int
1282 at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1283 {
1284         struct at86rf230_local *lp = hw->priv;
1285         int rc;
1286
1287         if (on) {
1288                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1289                 if (rc < 0)
1290                         return rc;
1291
1292                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1293                 if (rc < 0)
1294                         return rc;
1295         } else {
1296                 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1297                 if (rc < 0)
1298                         return rc;
1299
1300                 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1301                 if (rc < 0)
1302                         return rc;
1303         }
1304
1305         return 0;
1306 }
1307
1308 static const struct ieee802154_ops at86rf230_ops = {
1309         .owner = THIS_MODULE,
1310         .xmit_async = at86rf230_xmit,
1311         .ed = at86rf230_ed,
1312         .set_channel = at86rf230_channel,
1313         .start = at86rf230_start,
1314         .stop = at86rf230_stop,
1315         .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
1316         .set_txpower = at86rf230_set_txpower,
1317         .set_lbt = at86rf230_set_lbt,
1318         .set_cca_mode = at86rf230_set_cca_mode,
1319         .set_cca_ed_level = at86rf230_set_cca_ed_level,
1320         .set_csma_params = at86rf230_set_csma_params,
1321         .set_frame_retries = at86rf230_set_frame_retries,
1322         .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
1323 };
1324
1325 static struct at86rf2xx_chip_data at86rf233_data = {
1326         .t_sleep_cycle = 330,
1327         .t_channel_switch = 11,
1328         .t_reset_to_off = 26,
1329         .t_off_to_aack = 80,
1330         .t_off_to_tx_on = 80,
1331         .t_off_to_sleep = 35,
1332         .t_sleep_to_off = 210,
1333         .t_frame = 4096,
1334         .t_p_ack = 545,
1335         .rssi_base_val = -91,
1336         .set_channel = at86rf23x_set_channel,
1337         .set_txpower = at86rf23x_set_txpower,
1338 };
1339
1340 static struct at86rf2xx_chip_data at86rf231_data = {
1341         .t_sleep_cycle = 330,
1342         .t_channel_switch = 24,
1343         .t_reset_to_off = 37,
1344         .t_off_to_aack = 110,
1345         .t_off_to_tx_on = 110,
1346         .t_off_to_sleep = 35,
1347         .t_sleep_to_off = 380,
1348         .t_frame = 4096,
1349         .t_p_ack = 545,
1350         .rssi_base_val = -91,
1351         .set_channel = at86rf23x_set_channel,
1352         .set_txpower = at86rf23x_set_txpower,
1353 };
1354
1355 static struct at86rf2xx_chip_data at86rf212_data = {
1356         .t_sleep_cycle = 330,
1357         .t_channel_switch = 11,
1358         .t_reset_to_off = 26,
1359         .t_off_to_aack = 200,
1360         .t_off_to_tx_on = 200,
1361         .t_off_to_sleep = 35,
1362         .t_sleep_to_off = 380,
1363         .t_frame = 4096,
1364         .t_p_ack = 545,
1365         .rssi_base_val = -100,
1366         .set_channel = at86rf212_set_channel,
1367         .set_txpower = at86rf212_set_txpower,
1368 };
1369
1370 static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
1371 {
1372         int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
1373         unsigned int dvdd;
1374         u8 csma_seed[2];
1375
1376         rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
1377         if (rc)
1378                 return rc;
1379
1380         irq_type = irq_get_trigger_type(lp->spi->irq);
1381         if (irq_type == IRQ_TYPE_EDGE_RISING ||
1382             irq_type == IRQ_TYPE_EDGE_FALLING)
1383                 dev_warn(&lp->spi->dev,
1384                          "Using edge triggered irq's are not recommended!\n");
1385         if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1386             irq_type == IRQ_TYPE_LEVEL_LOW)
1387                 irq_pol = IRQ_ACTIVE_LOW;
1388
1389         rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
1390         if (rc)
1391                 return rc;
1392
1393         rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1394         if (rc)
1395                 return rc;
1396
1397         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
1398         if (rc)
1399                 return rc;
1400
1401         /* reset values differs in at86rf231 and at86rf233 */
1402         rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1403         if (rc)
1404                 return rc;
1405
1406         get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1407         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1408         if (rc)
1409                 return rc;
1410         rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1411         if (rc)
1412                 return rc;
1413
1414         /* CLKM changes are applied immediately */
1415         rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1416         if (rc)
1417                 return rc;
1418
1419         /* Turn CLKM Off */
1420         rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1421         if (rc)
1422                 return rc;
1423         /* Wait the next SLEEP cycle */
1424         usleep_range(lp->data->t_sleep_cycle,
1425                      lp->data->t_sleep_cycle + 100);
1426
1427         /* xtal_trim value is calculated by:
1428          * CL = 0.5 * (CX + CTRIM + CPAR)
1429          *
1430          * whereas:
1431          * CL = capacitor of used crystal
1432          * CX = connected capacitors at xtal pins
1433          * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1434          *        but this is different on each board setup. You need to fine
1435          *        tuning this value via CTRIM.
1436          * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1437          *         0 pF upto 4.5 pF.
1438          *
1439          * Examples:
1440          * atben transceiver:
1441          *
1442          * CL = 8 pF
1443          * CX = 12 pF
1444          * CPAR = 3 pF (We assume the magic constant from datasheet)
1445          * CTRIM = 0.9 pF
1446          *
1447          * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1448          *
1449          * xtal_trim = 0x3
1450          *
1451          * openlabs transceiver:
1452          *
1453          * CL = 16 pF
1454          * CX = 22 pF
1455          * CPAR = 3 pF (We assume the magic constant from datasheet)
1456          * CTRIM = 4.5 pF
1457          *
1458          * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1459          *
1460          * xtal_trim = 0xf
1461          */
1462         rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1463         if (rc)
1464                 return rc;
1465
1466         rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
1467         if (rc)
1468                 return rc;
1469         if (!dvdd) {
1470                 dev_err(&lp->spi->dev, "DVDD error\n");
1471                 return -EINVAL;
1472         }
1473
1474         /* Force setting slotted operation bit to 0. Sometimes the atben
1475          * sets this bit and I don't know why. We set this always force
1476          * to zero while probing.
1477          */
1478         return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
1479 }
1480
1481 static int
1482 at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1483                     u8 *xtal_trim)
1484 {
1485         struct at86rf230_platform_data *pdata = spi->dev.platform_data;
1486         int ret;
1487
1488         if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1489                 if (!pdata)
1490                         return -ENOENT;
1491
1492                 *rstn = pdata->rstn;
1493                 *slp_tr = pdata->slp_tr;
1494                 *xtal_trim = pdata->xtal_trim;
1495                 return 0;
1496         }
1497
1498         *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1499         *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1500         ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1501         if (ret < 0 && ret != -EINVAL)
1502                 return ret;
1503
1504         return 0;
1505 }
1506
1507 static int
1508 at86rf230_detect_device(struct at86rf230_local *lp)
1509 {
1510         unsigned int part, version, val;
1511         u16 man_id = 0;
1512         const char *chip;
1513         int rc;
1514
1515         rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1516         if (rc)
1517                 return rc;
1518         man_id |= val;
1519
1520         rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1521         if (rc)
1522                 return rc;
1523         man_id |= (val << 8);
1524
1525         rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1526         if (rc)
1527                 return rc;
1528
1529         rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
1530         if (rc)
1531                 return rc;
1532
1533         if (man_id != 0x001f) {
1534                 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1535                         man_id >> 8, man_id & 0xFF);
1536                 return -EINVAL;
1537         }
1538
1539         lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
1540                         IEEE802154_HW_CSMA_PARAMS |
1541                         IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1542                         IEEE802154_HW_PROMISCUOUS;
1543
1544         lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1545                              WPAN_PHY_FLAG_CCA_ED_LEVEL |
1546                              WPAN_PHY_FLAG_CCA_MODE;
1547
1548         lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1549                 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1550         lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1551                 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1552
1553         lp->hw->phy->supported.cca_ed_levels = at86rf23x_ed_levels;
1554         lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf23x_ed_levels);
1555
1556         lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1557
1558         switch (part) {
1559         case 2:
1560                 chip = "at86rf230";
1561                 rc = -ENOTSUPP;
1562                 goto not_supp;
1563         case 3:
1564                 chip = "at86rf231";
1565                 lp->data = &at86rf231_data;
1566                 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1567                 lp->hw->phy->current_channel = 11;
1568                 lp->hw->phy->symbol_duration = 16;
1569                 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1570                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
1571                 break;
1572         case 7:
1573                 chip = "at86rf212";
1574                 lp->data = &at86rf212_data;
1575                 lp->hw->flags |= IEEE802154_HW_LBT;
1576                 lp->hw->phy->supported.channels[0] = 0x00007FF;
1577                 lp->hw->phy->supported.channels[2] = 0x00007FF;
1578                 lp->hw->phy->current_channel = 5;
1579                 lp->hw->phy->symbol_duration = 25;
1580                 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
1581                 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1582                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
1583                 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1584                 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1585                 break;
1586         case 11:
1587                 chip = "at86rf233";
1588                 lp->data = &at86rf233_data;
1589                 lp->hw->phy->supported.channels[0] = 0x7FFF800;
1590                 lp->hw->phy->current_channel = 13;
1591                 lp->hw->phy->symbol_duration = 16;
1592                 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1593                 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
1594                 break;
1595         default:
1596                 chip = "unknown";
1597                 rc = -ENOTSUPP;
1598                 goto not_supp;
1599         }
1600
1601         lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
1602         lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
1603
1604 not_supp:
1605         dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1606
1607         return rc;
1608 }
1609
1610 static void
1611 at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1612 {
1613         lp->state.lp = lp;
1614         lp->state.irq = lp->spi->irq;
1615         spi_message_init(&lp->state.msg);
1616         lp->state.msg.context = &lp->state;
1617         lp->state.trx.len = 2;
1618         lp->state.trx.tx_buf = lp->state.buf;
1619         lp->state.trx.rx_buf = lp->state.buf;
1620         spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1621         hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1622         lp->state.timer.function = at86rf230_async_state_timer;
1623
1624         lp->irq.lp = lp;
1625         lp->irq.irq = lp->spi->irq;
1626         spi_message_init(&lp->irq.msg);
1627         lp->irq.msg.context = &lp->irq;
1628         lp->irq.trx.len = 2;
1629         lp->irq.trx.tx_buf = lp->irq.buf;
1630         lp->irq.trx.rx_buf = lp->irq.buf;
1631         spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1632         hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1633         lp->irq.timer.function = at86rf230_async_state_timer;
1634
1635         lp->tx.lp = lp;
1636         lp->tx.irq = lp->spi->irq;
1637         spi_message_init(&lp->tx.msg);
1638         lp->tx.msg.context = &lp->tx;
1639         lp->tx.trx.len = 2;
1640         lp->tx.trx.tx_buf = lp->tx.buf;
1641         lp->tx.trx.rx_buf = lp->tx.buf;
1642         spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1643         hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1644         lp->tx.timer.function = at86rf230_async_state_timer;
1645 }
1646
1647 static int at86rf230_probe(struct spi_device *spi)
1648 {
1649         struct ieee802154_hw *hw;
1650         struct at86rf230_local *lp;
1651         unsigned int status;
1652         int rc, irq_type, rstn, slp_tr;
1653         u8 xtal_trim = 0;
1654
1655         if (!spi->irq) {
1656                 dev_err(&spi->dev, "no IRQ specified\n");
1657                 return -EINVAL;
1658         }
1659
1660         rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
1661         if (rc < 0) {
1662                 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1663                 return rc;
1664         }
1665
1666         if (gpio_is_valid(rstn)) {
1667                 rc = devm_gpio_request_one(&spi->dev, rstn,
1668                                            GPIOF_OUT_INIT_HIGH, "rstn");
1669                 if (rc)
1670                         return rc;
1671         }
1672
1673         if (gpio_is_valid(slp_tr)) {
1674                 rc = devm_gpio_request_one(&spi->dev, slp_tr,
1675                                            GPIOF_OUT_INIT_LOW, "slp_tr");
1676                 if (rc)
1677                         return rc;
1678         }
1679
1680         /* Reset */
1681         if (gpio_is_valid(rstn)) {
1682                 udelay(1);
1683                 gpio_set_value(rstn, 0);
1684                 udelay(1);
1685                 gpio_set_value(rstn, 1);
1686                 usleep_range(120, 240);
1687         }
1688
1689         hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1690         if (!hw)
1691                 return -ENOMEM;
1692
1693         lp = hw->priv;
1694         lp->hw = hw;
1695         lp->spi = spi;
1696         lp->slp_tr = slp_tr;
1697         hw->parent = &spi->dev;
1698         ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
1699
1700         lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1701         if (IS_ERR(lp->regmap)) {
1702                 rc = PTR_ERR(lp->regmap);
1703                 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1704                         rc);
1705                 goto free_dev;
1706         }
1707
1708         at86rf230_setup_spi_messages(lp);
1709
1710         rc = at86rf230_detect_device(lp);
1711         if (rc < 0)
1712                 goto free_dev;
1713
1714         init_completion(&lp->state_complete);
1715
1716         spi_set_drvdata(spi, lp);
1717
1718         rc = at86rf230_hw_init(lp, xtal_trim);
1719         if (rc)
1720                 goto free_dev;
1721
1722         /* Read irq status register to reset irq line */
1723         rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
1724         if (rc)
1725                 goto free_dev;
1726
1727         irq_type = irq_get_trigger_type(spi->irq);
1728         if (!irq_type)
1729                 irq_type = IRQF_TRIGGER_HIGH;
1730
1731         rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1732                               IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
1733         if (rc)
1734                 goto free_dev;
1735
1736         /* disable_irq by default and wait for starting hardware */
1737         disable_irq(spi->irq);
1738
1739         /* going into sleep by default */
1740         at86rf230_sleep(lp);
1741
1742         rc = ieee802154_register_hw(lp->hw);
1743         if (rc)
1744                 goto free_dev;
1745
1746         return rc;
1747
1748 free_dev:
1749         ieee802154_free_hw(lp->hw);
1750
1751         return rc;
1752 }
1753
1754 static int at86rf230_remove(struct spi_device *spi)
1755 {
1756         struct at86rf230_local *lp = spi_get_drvdata(spi);
1757
1758         /* mask all at86rf230 irq's */
1759         at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
1760         ieee802154_unregister_hw(lp->hw);
1761         ieee802154_free_hw(lp->hw);
1762         dev_dbg(&spi->dev, "unregistered at86rf230\n");
1763
1764         return 0;
1765 }
1766
1767 static const struct of_device_id at86rf230_of_match[] = {
1768         { .compatible = "atmel,at86rf230", },
1769         { .compatible = "atmel,at86rf231", },
1770         { .compatible = "atmel,at86rf233", },
1771         { .compatible = "atmel,at86rf212", },
1772         { },
1773 };
1774 MODULE_DEVICE_TABLE(of, at86rf230_of_match);
1775
1776 static const struct spi_device_id at86rf230_device_id[] = {
1777         { .name = "at86rf230", },
1778         { .name = "at86rf231", },
1779         { .name = "at86rf233", },
1780         { .name = "at86rf212", },
1781         { },
1782 };
1783 MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1784
1785 static struct spi_driver at86rf230_driver = {
1786         .id_table = at86rf230_device_id,
1787         .driver = {
1788                 .of_match_table = of_match_ptr(at86rf230_of_match),
1789                 .name   = "at86rf230",
1790                 .owner  = THIS_MODULE,
1791         },
1792         .probe      = at86rf230_probe,
1793         .remove     = at86rf230_remove,
1794 };
1795
1796 module_spi_driver(at86rf230_driver);
1797
1798 MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1799 MODULE_LICENSE("GPL v2");