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