blob: 2f25a5ed82473b38190c9d873c0651fbdf755760 [file] [log] [blame]
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001/*
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 *
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000015 * Written by:
16 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Alexander Aring01ebd602014-07-03 00:20:55 +020018 * Alexander Aring <aar@pengutronix.de>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000019 */
20#include <linux/kernel.h>
21#include <linux/module.h>
Alexander Aringeb3b435e2015-03-09 13:56:10 +010022#include <linux/hrtimer.h>
Alexander Aringdce481e2015-03-09 13:56:11 +010023#include <linux/jiffies.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000024#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020025#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000026#include <linux/gpio.h>
27#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000028#include <linux/spi/spi.h>
29#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020030#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000031#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010032#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020033#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000034
35#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020036#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000037
Alexander Aring7490b002015-05-17 21:44:57 +020038#include "at86rf230.h"
39
Alexander Aringa53d1f72014-07-03 00:20:46 +020040struct at86rf230_local;
41/* at86rf2xx chip depend data.
42 * All timings are in us.
43 */
44struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020045 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020046 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020047 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020048 u16 t_off_to_aack;
49 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020050 u16 t_frame;
51 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020052 int rssi_base_val;
53
Alexander Aringe37d2ec2014-10-28 18:21:19 +010054 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aring6f4da3f2015-05-17 21:44:49 +020055 int (*set_txpower)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020056};
57
Alexander Aringba6d2232015-03-01 21:55:28 +010058#define AT86RF2XX_MAX_BUF (127 + 3)
59/* tx retries to access the TX_ON state
60 * if it's above then force change will be started.
61 *
62 * We assume the max_frame_retries (7) value of 802.15.4 here.
63 */
64#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aringdce481e2015-03-09 13:56:11 +010065/* We use the recommended 5 minutes timeout to recalibrate */
66#define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
Alexander Aring1d15d6b2014-07-03 00:20:48 +020067
68struct at86rf230_state_change {
69 struct at86rf230_local *lp;
Alexander Aringcca990c2015-03-01 21:55:31 +010070 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020071
Alexander Aringeb3b435e2015-03-09 13:56:10 +010072 struct hrtimer timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020073 struct spi_message msg;
74 struct spi_transfer trx;
75 u8 buf[AT86RF2XX_MAX_BUF];
76
77 void (*complete)(void *context);
78 u8 from_state;
79 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020080
81 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020082};
83
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000084struct at86rf230_local {
85 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000086
Alexander Aring5a504392014-10-25 17:16:34 +020087 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020088 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020089 struct regmap *regmap;
Alexander Aringd2c8bf52015-04-30 17:45:03 +020090 int slp_tr;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000091
Alexander Aring2e0571c2014-07-03 00:20:51 +020092 struct completion state_complete;
93 struct at86rf230_state_change state;
94
Alexander Aring1d15d6b2014-07-03 00:20:48 +020095 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020096
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010097 bool tx_aret;
Alexander Aringdce481e2015-03-09 13:56:11 +010098 unsigned long cal_timeout;
Alexander Aring850f43a2014-10-07 10:38:27 +020099 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200100 bool is_tx;
Alexander Aring85009202015-04-30 17:45:02 +0200101 bool is_tx_from_off;
Alexander Aringba6d2232015-03-01 21:55:28 +0100102 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200103 struct sk_buff *tx_skb;
104 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000105};
106
Alexander Aringf76014f772014-07-03 00:20:44 +0200107#define AT86RF2XX_NUMREGS 0x3F
108
Alexander Aring97fed792014-10-07 10:38:32 +0200109static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200110at86rf230_async_state_change(struct at86rf230_local *lp,
111 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200112 const u8 state, void (*complete)(void *context),
113 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200114
Alexander Aringf76014f772014-07-03 00:20:44 +0200115static inline int
116__at86rf230_write(struct at86rf230_local *lp,
117 unsigned int addr, unsigned int data)
118{
119 return regmap_write(lp->regmap, addr, data);
120}
121
122static inline int
123__at86rf230_read(struct at86rf230_local *lp,
124 unsigned int addr, unsigned int *data)
125{
126 return regmap_read(lp->regmap, addr, data);
127}
128
129static inline int
130at86rf230_read_subreg(struct at86rf230_local *lp,
131 unsigned int addr, unsigned int mask,
132 unsigned int shift, unsigned int *data)
133{
134 int rc;
135
136 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100137 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200138 *data = (*data & mask) >> shift;
139
140 return rc;
141}
142
143static inline int
144at86rf230_write_subreg(struct at86rf230_local *lp,
145 unsigned int addr, unsigned int mask,
146 unsigned int shift, unsigned int data)
147{
148 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
149}
150
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200151static inline void
152at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
153{
154 gpio_set_value(lp->slp_tr, 1);
155 udelay(1);
156 gpio_set_value(lp->slp_tr, 0);
157}
158
Alexander Aringf76014f772014-07-03 00:20:44 +0200159static bool
160at86rf230_reg_writeable(struct device *dev, unsigned int reg)
161{
162 switch (reg) {
163 case RG_TRX_STATE:
164 case RG_TRX_CTRL_0:
165 case RG_TRX_CTRL_1:
166 case RG_PHY_TX_PWR:
167 case RG_PHY_ED_LEVEL:
168 case RG_PHY_CC_CCA:
169 case RG_CCA_THRES:
170 case RG_RX_CTRL:
171 case RG_SFD_VALUE:
172 case RG_TRX_CTRL_2:
173 case RG_ANT_DIV:
174 case RG_IRQ_MASK:
175 case RG_VREG_CTRL:
176 case RG_BATMON:
177 case RG_XOSC_CTRL:
178 case RG_RX_SYN:
179 case RG_XAH_CTRL_1:
180 case RG_FTN_CTRL:
181 case RG_PLL_CF:
182 case RG_PLL_DCU:
183 case RG_SHORT_ADDR_0:
184 case RG_SHORT_ADDR_1:
185 case RG_PAN_ID_0:
186 case RG_PAN_ID_1:
187 case RG_IEEE_ADDR_0:
188 case RG_IEEE_ADDR_1:
189 case RG_IEEE_ADDR_2:
190 case RG_IEEE_ADDR_3:
191 case RG_IEEE_ADDR_4:
192 case RG_IEEE_ADDR_5:
193 case RG_IEEE_ADDR_6:
194 case RG_IEEE_ADDR_7:
195 case RG_XAH_CTRL_0:
196 case RG_CSMA_SEED_0:
197 case RG_CSMA_SEED_1:
198 case RG_CSMA_BE:
199 return true;
200 default:
201 return false;
202 }
203}
204
205static bool
206at86rf230_reg_readable(struct device *dev, unsigned int reg)
207{
208 bool rc;
209
210 /* all writeable are also readable */
211 rc = at86rf230_reg_writeable(dev, reg);
212 if (rc)
213 return rc;
214
215 /* readonly regs */
216 switch (reg) {
217 case RG_TRX_STATUS:
218 case RG_PHY_RSSI:
219 case RG_IRQ_STATUS:
220 case RG_PART_NUM:
221 case RG_VERSION_NUM:
222 case RG_MAN_ID_1:
223 case RG_MAN_ID_0:
224 return true;
225 default:
226 return false;
227 }
228}
229
230static bool
231at86rf230_reg_volatile(struct device *dev, unsigned int reg)
232{
233 /* can be changed during runtime */
234 switch (reg) {
235 case RG_TRX_STATUS:
236 case RG_TRX_STATE:
237 case RG_PHY_RSSI:
238 case RG_PHY_ED_LEVEL:
239 case RG_IRQ_STATUS:
240 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100241 case RG_PLL_CF:
242 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200243 return true;
244 default:
245 return false;
246 }
247}
248
249static bool
250at86rf230_reg_precious(struct device *dev, unsigned int reg)
251{
252 /* don't clear irq line on read */
253 switch (reg) {
254 case RG_IRQ_STATUS:
255 return true;
256 default:
257 return false;
258 }
259}
260
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100261static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200262 .reg_bits = 8,
263 .val_bits = 8,
264 .write_flag_mask = CMD_REG | CMD_WRITE,
265 .read_flag_mask = CMD_REG,
266 .cache_type = REGCACHE_RBTREE,
267 .max_register = AT86RF2XX_NUMREGS,
268 .writeable_reg = at86rf230_reg_writeable,
269 .readable_reg = at86rf230_reg_readable,
270 .volatile_reg = at86rf230_reg_volatile,
271 .precious_reg = at86rf230_reg_precious,
272};
273
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200274static void
275at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000276{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200277 struct at86rf230_state_change *ctx = context;
278 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000279
Alexander Aringa7a484b2015-03-26 12:46:30 +0100280 lp->is_tx = 0;
Alexander Aring97fed792014-10-07 10:38:32 +0200281 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100282 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200283}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000284
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100285static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200286at86rf230_async_error(struct at86rf230_local *lp,
287 struct at86rf230_state_change *ctx, int rc)
288{
289 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000290
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200291 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200292 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200293}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000294
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200295/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200296static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200297at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
298 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200299 void (*complete)(void *context),
300 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200301{
Alexander Aring97fed792014-10-07 10:38:32 +0200302 int rc;
303
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200304 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000305
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200306 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200307 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200308 ctx->irq_enable = irq_enable;
309 rc = spi_async(lp->spi, &ctx->msg);
310 if (rc) {
311 if (irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100312 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200313
314 at86rf230_async_error(lp, ctx, rc);
315 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200316}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000317
Alexander Aringdce481e2015-03-09 13:56:11 +0100318static inline u8 at86rf230_state_to_force(u8 state)
319{
320 if (state == STATE_TX_ON)
321 return STATE_FORCE_TX_ON;
322 else
323 return STATE_FORCE_TRX_OFF;
324}
325
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200326static void
327at86rf230_async_state_assert(void *context)
328{
329 struct at86rf230_state_change *ctx = context;
330 struct at86rf230_local *lp = ctx->lp;
331 const u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200332 const u8 trx_state = buf[1] & TRX_STATE_MASK;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000333
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200334 /* Assert state change */
335 if (trx_state != ctx->to_state) {
336 /* Special handling if transceiver state is in
337 * STATE_BUSY_RX_AACK and a SHR was detected.
338 */
339 if (trx_state == STATE_BUSY_RX_AACK) {
340 /* Undocumented race condition. If we send a state
341 * change to STATE_RX_AACK_ON the transceiver could
342 * change his state automatically to STATE_BUSY_RX_AACK
343 * if a SHR was detected. This is not an error, but we
344 * can't assert this.
345 */
346 if (ctx->to_state == STATE_RX_AACK_ON)
347 goto done;
348
349 /* If we change to STATE_TX_ON without forcing and
350 * transceiver state is STATE_BUSY_RX_AACK, we wait
351 * 'tFrame + tPAck' receiving time. In this time the
352 * PDU should be received. If the transceiver is still
353 * in STATE_BUSY_RX_AACK, we run a force state change
354 * to STATE_TX_ON. This is a timeout handling, if the
355 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100356 *
357 * Additional we do several retries to try to get into
358 * TX_ON state without forcing. If the retries are
359 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
360 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200361 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100362 if (ctx->to_state == STATE_TX_ON ||
363 ctx->to_state == STATE_TRX_OFF) {
364 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100365
366 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringdce481e2015-03-09 13:56:11 +0100367 state = at86rf230_state_to_force(state);
Alexander Aringba6d2232015-03-01 21:55:28 +0100368 lp->tx_retry++;
369
370 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200371 ctx->complete,
372 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200373 return;
374 }
375 }
376
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200377 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
378 ctx->from_state, ctx->to_state, trx_state);
379 }
380
381done:
382 if (ctx->complete)
383 ctx->complete(context);
384}
385
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100386static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
387{
388 struct at86rf230_state_change *ctx =
389 container_of(timer, struct at86rf230_state_change, timer);
390 struct at86rf230_local *lp = ctx->lp;
391
392 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
393 at86rf230_async_state_assert,
394 ctx->irq_enable);
395
396 return HRTIMER_NORESTART;
397}
398
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200399/* Do state change timing delay. */
400static void
401at86rf230_async_state_delay(void *context)
402{
403 struct at86rf230_state_change *ctx = context;
404 struct at86rf230_local *lp = ctx->lp;
405 struct at86rf2xx_chip_data *c = lp->data;
406 bool force = false;
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100407 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200408
409 /* The force state changes are will show as normal states in the
410 * state status subregister. We change the to_state to the
411 * corresponding one and remember if it was a force change, this
412 * differs if we do a state change from STATE_BUSY_RX_AACK.
413 */
414 switch (ctx->to_state) {
415 case STATE_FORCE_TX_ON:
416 ctx->to_state = STATE_TX_ON;
417 force = true;
418 break;
419 case STATE_FORCE_TRX_OFF:
420 ctx->to_state = STATE_TRX_OFF;
421 force = true;
422 break;
423 default:
424 break;
425 }
426
427 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200428 case STATE_TRX_OFF:
429 switch (ctx->to_state) {
430 case STATE_RX_AACK_ON:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100431 tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
Alexander Aring2ad33242015-04-30 17:44:59 +0200432 /* state change from TRX_OFF to RX_AACK_ON to do a
433 * calibration, we need to reset the timeout for the
434 * next one.
435 */
436 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200437 goto change;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200438 case STATE_TX_ARET_ON:
Alexander Aring2e0571c2014-07-03 00:20:51 +0200439 case STATE_TX_ON:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100440 tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
Alexander Aring3b951ca2015-04-30 17:45:00 +0200441 /* state change from TRX_OFF to TX_ON or ARET_ON to do
442 * a calibration, we need to reset the timeout for the
Alexander Aringdce481e2015-03-09 13:56:11 +0100443 * next one.
444 */
445 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200446 goto change;
447 default:
448 break;
449 }
450 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200451 case STATE_BUSY_RX_AACK:
452 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100453 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200454 case STATE_TX_ON:
455 /* Wait for worst case receiving time if we
456 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100457 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200458 */
459 if (!force) {
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100460 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
461 NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200462 goto change;
463 }
464 break;
465 default:
466 break;
467 }
468 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200469 /* Default value, means RESET state */
470 case STATE_P_ON:
471 switch (ctx->to_state) {
472 case STATE_TRX_OFF:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100473 tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
Alexander Aring09e536c2014-07-03 00:20:52 +0200474 goto change;
475 default:
476 break;
477 }
478 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200479 default:
480 break;
481 }
482
483 /* Default delay is 1us in the most cases */
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100484 tim = ktime_set(0, NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200485
486change:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100487 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200488}
489
490static void
491at86rf230_async_state_change_start(void *context)
492{
493 struct at86rf230_state_change *ctx = context;
494 struct at86rf230_local *lp = ctx->lp;
495 u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200496 const u8 trx_state = buf[1] & TRX_STATE_MASK;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200497 int rc;
498
499 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
500 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
501 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200502 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
503 at86rf230_async_state_change_start,
504 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200505 return;
506 }
507
508 /* Check if we already are in the state which we change in */
509 if (trx_state == ctx->to_state) {
510 if (ctx->complete)
511 ctx->complete(context);
512 return;
513 }
514
515 /* Set current state to the context of state change */
516 ctx->from_state = trx_state;
517
518 /* Going into the next step for a state change which do a timing
519 * relevant delay.
520 */
521 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
522 buf[1] = ctx->to_state;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200523 ctx->msg.complete = at86rf230_async_state_delay;
524 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200525 if (rc) {
526 if (ctx->irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100527 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200528
Alexander Aring4fef7d32014-12-15 10:25:55 +0100529 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200530 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000531}
532
Alexander Aring97fed792014-10-07 10:38:32 +0200533static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200534at86rf230_async_state_change(struct at86rf230_local *lp,
535 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200536 const u8 state, void (*complete)(void *context),
537 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000538{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200539 /* Initialization for the state change context */
540 ctx->to_state = state;
541 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200542 ctx->irq_enable = irq_enable;
543 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
544 at86rf230_async_state_change_start,
545 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200546}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000547
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200548static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200549at86rf230_sync_state_change_complete(void *context)
550{
551 struct at86rf230_state_change *ctx = context;
552 struct at86rf230_local *lp = ctx->lp;
553
554 complete(&lp->state_complete);
555}
556
557/* This function do a sync framework above the async state change.
558 * Some callbacks of the IEEE 802.15.4 driver interface need to be
559 * handled synchronously.
560 */
561static int
562at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
563{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100564 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200565
Alexander Aring97fed792014-10-07 10:38:32 +0200566 at86rf230_async_state_change(lp, &lp->state, state,
567 at86rf230_sync_state_change_complete,
568 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200569
570 rc = wait_for_completion_timeout(&lp->state_complete,
571 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200572 if (!rc) {
573 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200574 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200575 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200576
577 return 0;
578}
579
580static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200581at86rf230_tx_complete(void *context)
582{
583 struct at86rf230_state_change *ctx = context;
584 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000585
Alexander Aringcca990c2015-03-01 21:55:31 +0100586 enable_irq(ctx->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100587
Alexander Aringef5428a2015-03-01 21:55:29 +0100588 ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200589}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000590
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200591static void
592at86rf230_tx_on(void *context)
593{
594 struct at86rf230_state_change *ctx = context;
595 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000596
Alexander Aring31fa7432015-03-01 21:55:32 +0100597 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200598 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200599}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000600
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200601static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200602at86rf230_tx_trac_check(void *context)
603{
604 struct at86rf230_state_change *ctx = context;
605 struct at86rf230_local *lp = ctx->lp;
606 const u8 *buf = ctx->buf;
607 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000608
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200609 /* If trac status is different than zero we need to do a state change
Alexander Aring2f8cdd92015-04-30 17:45:01 +0200610 * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the
611 * transceiver.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200612 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100613 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200614 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring2f8cdd92015-04-30 17:45:01 +0200615 at86rf230_tx_on, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100616 else
617 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200618}
619
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200620static void
621at86rf230_tx_trac_status(void *context)
622{
623 struct at86rf230_state_change *ctx = context;
624 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200625
Alexander Aring97fed792014-10-07 10:38:32 +0200626 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
627 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200628}
629
630static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100631at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200632{
Alexander Aring74de4c82015-03-01 21:55:30 +0100633 struct at86rf230_state_change *ctx = context;
634 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200635 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
Alexander Aring31fa7432015-03-01 21:55:32 +0100636 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100637 struct sk_buff *skb;
638 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200639
Alexander Aring74de4c82015-03-01 21:55:30 +0100640 len = buf[1];
641 if (!ieee802154_is_valid_psdu_len(len)) {
642 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
643 len = IEEE802154_MTU;
644 }
645 lqi = buf[2 + len];
646
647 memcpy(rx_local_buf, buf + 2, len);
Alexander Aring263be332015-03-01 21:55:33 +0100648 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100649 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200650
Alexander Aring61a22812014-10-27 17:13:29 +0100651 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200652 if (!skb) {
653 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
654 return;
655 }
656
657 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100658 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200659}
660
661static void
Alexander Aringcca990c2015-03-01 21:55:31 +0100662at86rf230_rx_read_frame(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200663{
Alexander Aringcca990c2015-03-01 21:55:31 +0100664 struct at86rf230_state_change *ctx = context;
665 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100666 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200667 int rc;
668
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200669 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100670 ctx->trx.len = AT86RF2XX_MAX_BUF;
671 ctx->msg.complete = at86rf230_rx_read_frame_complete;
672 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200673 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100674 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100675 enable_irq(ctx->irq);
Alexander Aring31fa7432015-03-01 21:55:32 +0100676 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200677 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200678}
679
680static void
681at86rf230_rx_trac_check(void *context)
682{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200683 /* Possible check on trac status here. This could be useful to make
684 * some stats why receive is failed. Not used at the moment, but it's
685 * maybe timing relevant. Datasheet doesn't say anything about this.
686 * The programming guide say do it so.
687 */
688
Alexander Aringcca990c2015-03-01 21:55:31 +0100689 at86rf230_rx_read_frame(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200690}
691
Alexander Aring97fed792014-10-07 10:38:32 +0200692static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200693at86rf230_irq_trx_end(struct at86rf230_local *lp)
694{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200695 if (lp->is_tx) {
696 lp->is_tx = 0;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200697
698 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200699 at86rf230_async_state_change(lp, &lp->irq,
700 STATE_FORCE_TX_ON,
701 at86rf230_tx_trac_status,
702 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200703 else
Alexander Aring97fed792014-10-07 10:38:32 +0200704 at86rf230_async_state_change(lp, &lp->irq,
705 STATE_RX_AACK_ON,
706 at86rf230_tx_complete,
707 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200708 } else {
Alexander Aring97fed792014-10-07 10:38:32 +0200709 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
710 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200711 }
712}
713
714static void
715at86rf230_irq_status(void *context)
716{
717 struct at86rf230_state_change *ctx = context;
718 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100719 const u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200720 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200721
722 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200723 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200724 } else {
Alexander Aringcca990c2015-03-01 21:55:31 +0100725 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200726 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
727 irq);
728 }
729}
730
731static irqreturn_t at86rf230_isr(int irq, void *data)
732{
733 struct at86rf230_local *lp = data;
734 struct at86rf230_state_change *ctx = &lp->irq;
735 u8 *buf = ctx->buf;
736 int rc;
737
Alexander Aring90566362014-10-07 10:38:29 +0200738 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200739
740 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200741 ctx->msg.complete = at86rf230_irq_status;
742 rc = spi_async(lp->spi, &ctx->msg);
743 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200744 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200745 at86rf230_async_error(lp, ctx, rc);
746 return IRQ_NONE;
747 }
748
749 return IRQ_HANDLED;
750}
751
752static void
753at86rf230_write_frame_complete(void *context)
754{
755 struct at86rf230_state_change *ctx = context;
756 struct at86rf230_local *lp = ctx->lp;
757 u8 *buf = ctx->buf;
758 int rc;
759
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200760 ctx->trx.len = 2;
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200761
762 if (gpio_is_valid(lp->slp_tr)) {
763 at86rf230_slp_tr_rising_edge(lp);
764 } else {
765 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
766 buf[1] = STATE_BUSY_TX;
767 ctx->msg.complete = NULL;
768 rc = spi_async(lp->spi, &ctx->msg);
769 if (rc)
770 at86rf230_async_error(lp, ctx, rc);
771 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200772}
773
774static void
775at86rf230_write_frame(void *context)
776{
777 struct at86rf230_state_change *ctx = context;
778 struct at86rf230_local *lp = ctx->lp;
779 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100780 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200781 int rc;
782
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200783 lp->is_tx = 1;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200784
785 buf[0] = CMD_FB | CMD_WRITE;
786 buf[1] = skb->len + 2;
787 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100788 ctx->trx.len = skb->len + 2;
789 ctx->msg.complete = at86rf230_write_frame_complete;
790 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100791 if (rc) {
792 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200793 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100794 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200795}
796
797static void
798at86rf230_xmit_tx_on(void *context)
799{
800 struct at86rf230_state_change *ctx = context;
801 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200802
Alexander Aring97fed792014-10-07 10:38:32 +0200803 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
804 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200805}
806
Alexander Aringdce481e2015-03-09 13:56:11 +0100807static void
808at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200809{
Alexander Aringdce481e2015-03-09 13:56:11 +0100810 struct at86rf230_state_change *ctx = context;
811 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200812
813 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
814 * are in STATE_TX_ON. The pfad differs here, so we change
815 * the complete handler.
816 */
Alexander Aring85009202015-04-30 17:45:02 +0200817 if (lp->tx_aret) {
818 if (lp->is_tx_from_off) {
819 lp->is_tx_from_off = false;
820 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
Alexander Aringa2c390a2015-05-17 21:44:55 +0200821 at86rf230_write_frame,
Alexander Aring85009202015-04-30 17:45:02 +0200822 false);
823 } else {
824 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
825 at86rf230_xmit_tx_on,
826 false);
827 }
828 } else {
Alexander Aringdce481e2015-03-09 13:56:11 +0100829 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
830 at86rf230_write_frame, false);
Alexander Aring85009202015-04-30 17:45:02 +0200831 }
Alexander Aringdce481e2015-03-09 13:56:11 +0100832}
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200833
Alexander Aringdce481e2015-03-09 13:56:11 +0100834static int
835at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
836{
837 struct at86rf230_local *lp = hw->priv;
838 struct at86rf230_state_change *ctx = &lp->tx;
839
840 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +0100841 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +0100842
843 /* After 5 minutes in PLL and the same frequency we run again the
844 * calibration loops which is recommended by at86rf2xx datasheets.
845 *
846 * The calibration is initiate by a state change from TRX_OFF
847 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
848 * function then to start in the next 5 minutes.
849 */
Alexander Aring85009202015-04-30 17:45:02 +0200850 if (time_is_before_jiffies(lp->cal_timeout)) {
851 lp->is_tx_from_off = true;
Alexander Aringdce481e2015-03-09 13:56:11 +0100852 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
853 at86rf230_xmit_start, false);
Alexander Aring85009202015-04-30 17:45:02 +0200854 } else {
Alexander Aringdce481e2015-03-09 13:56:11 +0100855 at86rf230_xmit_start(ctx);
Alexander Aring85009202015-04-30 17:45:02 +0200856 }
Alexander Aring97fed792014-10-07 10:38:32 +0200857
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200858 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000859}
860
861static int
Alexander Aring5a504392014-10-25 17:16:34 +0200862at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000863{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000864 BUG_ON(!level);
865 *level = 0xbe;
866 return 0;
867}
868
869static int
Alexander Aring5a504392014-10-25 17:16:34 +0200870at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000871{
Alexander Aring5a504392014-10-25 17:16:34 +0200872 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000873}
874
875static void
Alexander Aring5a504392014-10-25 17:16:34 +0200876at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000877{
Alexander Aring5a504392014-10-25 17:16:34 +0200878 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000879}
880
881static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100882at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100883{
884 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
885}
886
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200887#define AT86RF2XX_MAX_ED_LEVELS 0xF
888static const s32 at86rf23x_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
889 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
890 -7100, -6900, -6700, -6500, -6300, -6100,
891};
892
893static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
894 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
Alexander Aring179655f2015-05-20 09:27:03 +0200895 -8000, -7800, -7600, -7400, -7200, -7000,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200896};
897
898static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
899 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
Alexander Aring179655f2015-05-20 09:27:03 +0200900 -7800, -7600, -7400, -7200, -7000, -6800,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200901};
902
903static inline int
904at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
905{
906 unsigned int cca_ed_thres;
907 int rc;
908
909 rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
910 if (rc < 0)
911 return rc;
912
913 switch (rssi_base_val) {
914 case -98:
915 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
916 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
917 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
918 break;
919 case -100:
920 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
921 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
922 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
923 break;
924 default:
925 WARN_ON(1);
926 }
927
928 return 0;
929}
930
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100931static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100932at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100933{
934 int rc;
935
936 if (channel == 0)
937 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
938 else
939 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
940 if (rc < 0)
941 return rc;
942
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100943 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100944 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +0200945 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100946 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100947 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +0200948 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100949 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100950 if (rc < 0)
951 return rc;
952
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200953 rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
954 if (rc < 0)
955 return rc;
956
Alexander Aring24ccb9f2014-11-12 19:51:57 +0100957 /* This sets the symbol_duration according frequency on the 212.
958 * TODO move this handling while set channel and page in cfg802154.
959 * We can do that, this timings are according 802.15.4 standard.
960 * If we do that in cfg802154, this is a more generic calculation.
961 *
962 * This should also protected from ifs_timer. Means cancel timer and
963 * init with a new value. For now, this is okay.
964 */
965 if (channel == 0) {
966 if (page == 0) {
967 /* SUB:0 and BPSK:0 -> BPSK-20 */
968 lp->hw->phy->symbol_duration = 50;
969 } else {
970 /* SUB:1 and BPSK:0 -> BPSK-40 */
971 lp->hw->phy->symbol_duration = 25;
972 }
973 } else {
974 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +0100975 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +0100976 lp->hw->phy->symbol_duration = 40;
977 else
Alexander Aring2d6dde22014-11-17 08:20:44 +0100978 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +0100979 lp->hw->phy->symbol_duration = 16;
980 }
981
982 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
983 lp->hw->phy->symbol_duration;
984 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
985 lp->hw->phy->symbol_duration;
986
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100987 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
988}
989
990static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100991at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000992{
Alexander Aring5a504392014-10-25 17:16:34 +0200993 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000994 int rc;
995
Alexander Aringa53d1f72014-07-03 00:20:46 +0200996 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +0200997 /* Wait for PLL */
998 usleep_range(lp->data->t_channel_switch,
999 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001000
1001 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001002 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001003}
1004
1005static int
Alexander Aring5a504392014-10-25 17:16:34 +02001006at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001007 struct ieee802154_hw_addr_filt *filt,
1008 unsigned long changed)
1009{
Alexander Aring5a504392014-10-25 17:16:34 +02001010 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001011
Alexander Aring57205c12014-10-25 05:25:09 +02001012 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001013 u16 addr = le16_to_cpu(filt->short_addr);
1014
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001015 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001016 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001017 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1018 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001019 }
1020
Alexander Aring57205c12014-10-25 05:25:09 +02001021 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001022 u16 pan = le16_to_cpu(filt->pan_id);
1023
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001024 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001025 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001026 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1027 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001028 }
1029
Alexander Aring57205c12014-10-25 05:25:09 +02001030 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001031 u8 i, addr[8];
1032
1033 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001034 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001035 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001036 for (i = 0; i < 8; i++)
1037 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001038 }
1039
Alexander Aring57205c12014-10-25 05:25:09 +02001040 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001041 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001042 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001043 if (filt->pan_coord)
1044 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1045 else
1046 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1047 }
1048
1049 return 0;
1050}
1051
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001052#define AT86RF23X_MAX_TX_POWERS 0xF
1053static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1054 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1055 -800, -1200, -1700,
1056};
1057
1058static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1059 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1060 -900, -1200, -1700,
1061};
1062
1063#define AT86RF212_MAX_TX_POWERS 0x1F
1064static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1065 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1066 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1067 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1068};
1069
1070static int
1071at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1072{
1073 u32 i;
1074
1075 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1076 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1077 return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1078 }
1079
1080 return -EINVAL;
1081}
1082
1083static int
1084at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1085{
1086 u32 i;
1087
1088 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1089 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1090 return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1091 }
1092
1093 return -EINVAL;
1094}
1095
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001096static int
Alexander Aringe2eb1732015-05-17 21:44:40 +02001097at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001098{
Alexander Aring5a504392014-10-25 17:16:34 +02001099 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001100
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001101 return lp->data->set_txpower(lp, mbm);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001102}
1103
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001104static int
Alexander Aring5a504392014-10-25 17:16:34 +02001105at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001106{
Alexander Aring5a504392014-10-25 17:16:34 +02001107 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001108
1109 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1110}
1111
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001112static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001113at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1114 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001115{
Alexander Aring5a504392014-10-25 17:16:34 +02001116 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001117 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001118
Alexander Aring7fe9a382014-12-10 15:33:12 +01001119 /* mapping 802.15.4 to driver spec */
1120 switch (cca->mode) {
1121 case NL802154_CCA_ENERGY:
1122 val = 1;
1123 break;
1124 case NL802154_CCA_CARRIER:
1125 val = 2;
1126 break;
1127 case NL802154_CCA_ENERGY_CARRIER:
1128 switch (cca->opt) {
1129 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1130 val = 3;
1131 break;
1132 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1133 val = 0;
1134 break;
1135 default:
1136 return -EINVAL;
1137 }
1138 break;
1139 default:
1140 return -EINVAL;
1141 }
1142
1143 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001144}
1145
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001146
1147static int
Alexander Aring32b23552015-05-17 21:44:41 +02001148at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001149{
Alexander Aring5a504392014-10-25 17:16:34 +02001150 struct at86rf230_local *lp = hw->priv;
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001151 u32 i;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001152
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001153 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1154 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1155 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1156 }
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001157
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001158 return -EINVAL;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001159}
1160
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001161static int
Alexander Aring5a504392014-10-25 17:16:34 +02001162at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001163 u8 retries)
1164{
Alexander Aring5a504392014-10-25 17:16:34 +02001165 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001166 int rc;
1167
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001168 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1169 if (rc)
1170 return rc;
1171
1172 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1173 if (rc)
1174 return rc;
1175
Alexander Aring39d7f322014-04-05 13:49:26 +02001176 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001177}
1178
1179static int
Alexander Aring5a504392014-10-25 17:16:34 +02001180at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001181{
Alexander Aring5a504392014-10-25 17:16:34 +02001182 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001183 int rc = 0;
1184
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001185 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001186 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001187
1188 if (retries >= 0)
1189 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1190
1191 return rc;
1192}
1193
Alexander Aring92f45f542014-10-29 21:34:33 +01001194static int
1195at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1196{
1197 struct at86rf230_local *lp = hw->priv;
1198 int rc;
1199
1200 if (on) {
1201 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1202 if (rc < 0)
1203 return rc;
1204
1205 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1206 if (rc < 0)
1207 return rc;
1208 } else {
1209 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1210 if (rc < 0)
1211 return rc;
1212
1213 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1214 if (rc < 0)
1215 return rc;
1216 }
1217
1218 return 0;
1219}
1220
Alexander Aring16301862014-10-28 18:21:18 +01001221static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001222 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001223 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001224 .ed = at86rf230_ed,
1225 .set_channel = at86rf230_channel,
1226 .start = at86rf230_start,
1227 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001228 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001229 .set_txpower = at86rf230_set_txpower,
1230 .set_lbt = at86rf230_set_lbt,
1231 .set_cca_mode = at86rf230_set_cca_mode,
1232 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1233 .set_csma_params = at86rf230_set_csma_params,
1234 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f542014-10-29 21:34:33 +01001235 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001236};
1237
Alexander Aringa53d1f72014-07-03 00:20:46 +02001238static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001239 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001240 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001241 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001242 .t_off_to_aack = 80,
1243 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001244 .t_frame = 4096,
1245 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001246 .rssi_base_val = -91,
1247 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001248 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001249};
1250
1251static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001252 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001253 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001254 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001255 .t_off_to_aack = 110,
1256 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001257 .t_frame = 4096,
1258 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001259 .rssi_base_val = -91,
1260 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001261 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001262};
1263
1264static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001265 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001266 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001267 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001268 .t_off_to_aack = 200,
1269 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001270 .t_frame = 4096,
1271 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001272 .rssi_base_val = -100,
1273 .set_channel = at86rf212_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001274 .set_txpower = at86rf212_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001275};
1276
Alexander Aringccdaeb22015-02-27 09:58:26 +01001277static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001278{
Alexander Aring1db05582014-07-03 00:20:50 +02001279 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001280 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001281 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001282
Alexander Aring09e536c2014-07-03 00:20:52 +02001283 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001284 if (rc)
1285 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001286
Alexander Aring4af619a2014-04-24 19:09:05 +02001287 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001288 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1289 irq_type == IRQ_TYPE_EDGE_FALLING)
1290 dev_warn(&lp->spi->dev,
1291 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001292 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1293 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001294 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001295
Alexander Aring18c65042014-04-24 19:09:18 +02001296 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001297 if (rc)
1298 return rc;
1299
Alexander Aring6bd2b132014-07-03 00:20:49 +02001300 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1301 if (rc)
1302 return rc;
1303
Sascha Herrmann057dad62013-04-14 22:33:29 +00001304 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001305 if (rc)
1306 return rc;
1307
Alexander Aringbe64f072015-02-27 09:58:28 +01001308 /* reset values differs in at86rf231 and at86rf233 */
1309 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1310 if (rc)
1311 return rc;
1312
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001313 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1314 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1315 if (rc)
1316 return rc;
1317 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1318 if (rc)
1319 return rc;
1320
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001321 /* CLKM changes are applied immediately */
1322 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1323 if (rc)
1324 return rc;
1325
1326 /* Turn CLKM Off */
1327 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1328 if (rc)
1329 return rc;
1330 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001331 usleep_range(lp->data->t_sleep_cycle,
1332 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001333
Alexander Aringccdaeb22015-02-27 09:58:26 +01001334 /* xtal_trim value is calculated by:
1335 * CL = 0.5 * (CX + CTRIM + CPAR)
1336 *
1337 * whereas:
1338 * CL = capacitor of used crystal
1339 * CX = connected capacitors at xtal pins
1340 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1341 * but this is different on each board setup. You need to fine
1342 * tuning this value via CTRIM.
1343 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1344 * 0 pF upto 4.5 pF.
1345 *
1346 * Examples:
1347 * atben transceiver:
1348 *
1349 * CL = 8 pF
1350 * CX = 12 pF
1351 * CPAR = 3 pF (We assume the magic constant from datasheet)
1352 * CTRIM = 0.9 pF
1353 *
1354 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1355 *
1356 * xtal_trim = 0x3
1357 *
1358 * openlabs transceiver:
1359 *
1360 * CL = 16 pF
1361 * CX = 22 pF
1362 * CPAR = 3 pF (We assume the magic constant from datasheet)
1363 * CTRIM = 4.5 pF
1364 *
1365 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1366 *
1367 * xtal_trim = 0xf
1368 */
1369 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1370 if (rc)
1371 return rc;
1372
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001373 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001374 if (rc)
1375 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001376 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001377 dev_err(&lp->spi->dev, "DVDD error\n");
1378 return -EINVAL;
1379 }
1380
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001381 /* Force setting slotted operation bit to 0. Sometimes the atben
1382 * sets this bit and I don't know why. We set this always force
1383 * to zero while probing.
1384 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001385 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001386}
1387
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001388static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001389at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1390 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001391{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001392 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001393 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001394
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001395 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1396 if (!pdata)
1397 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001398
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001399 *rstn = pdata->rstn;
1400 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001401 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001402 return 0;
1403 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001404
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001405 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1406 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001407 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1408 if (ret < 0 && ret != -EINVAL)
1409 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001410
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001411 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001412}
1413
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001414static int
1415at86rf230_detect_device(struct at86rf230_local *lp)
1416{
1417 unsigned int part, version, val;
1418 u16 man_id = 0;
1419 const char *chip;
1420 int rc;
1421
1422 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1423 if (rc)
1424 return rc;
1425 man_id |= val;
1426
1427 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1428 if (rc)
1429 return rc;
1430 man_id |= (val << 8);
1431
1432 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1433 if (rc)
1434 return rc;
1435
Andrey Yurovsky75989682014-12-17 13:14:42 -08001436 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001437 if (rc)
1438 return rc;
1439
1440 if (man_id != 0x001f) {
1441 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1442 man_id >> 8, man_id & 0xFF);
1443 return -EINVAL;
1444 }
1445
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001446 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringedea8f72015-05-17 21:44:46 +02001447 IEEE802154_HW_CSMA_PARAMS |
1448 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1449 IEEE802154_HW_PROMISCUOUS;
1450
1451 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1452 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1453 WPAN_PHY_FLAG_CCA_MODE;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001454
Alexander Aring8377d222015-05-17 21:44:48 +02001455 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1456 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1457 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1458 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1459
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001460 lp->hw->phy->supported.cca_ed_levels = at86rf23x_ed_levels;
1461 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf23x_ed_levels);
1462
Alexander Aringb48a7c12014-12-10 15:33:14 +01001463 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1464
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001465 switch (part) {
1466 case 2:
1467 chip = "at86rf230";
1468 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001469 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001470 case 3:
1471 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001472 lp->data = &at86rf231_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001473 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001474 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001475 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001476 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1477 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001478 break;
1479 case 7:
1480 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001481 lp->data = &at86rf212_data;
1482 lp->hw->flags |= IEEE802154_HW_LBT;
Alexander Aring72f655e2015-05-17 21:44:42 +02001483 lp->hw->phy->supported.channels[0] = 0x00007FF;
1484 lp->hw->phy->supported.channels[2] = 0x00007FF;
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001485 lp->hw->phy->current_channel = 5;
1486 lp->hw->phy->symbol_duration = 25;
Alexander Aring8377d222015-05-17 21:44:48 +02001487 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001488 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1489 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001490 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1491 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001492 break;
1493 case 11:
1494 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001495 lp->data = &at86rf233_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001496 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001497 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001498 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001499 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1500 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001501 break;
1502 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001503 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001504 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001505 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001506 }
1507
Alexander Aringcc643492015-05-17 21:44:51 +02001508 lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
Alexander Aring392f4e62015-05-17 21:44:52 +02001509 lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
Alexander Aringcc643492015-05-17 21:44:51 +02001510
1511not_supp:
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001512 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1513
1514 return rc;
1515}
1516
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001517static void
1518at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1519{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001520 lp->state.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001521 lp->state.irq = lp->spi->irq;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001522 spi_message_init(&lp->state.msg);
1523 lp->state.msg.context = &lp->state;
Alexander Aring263be332015-03-01 21:55:33 +01001524 lp->state.trx.len = 2;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001525 lp->state.trx.tx_buf = lp->state.buf;
1526 lp->state.trx.rx_buf = lp->state.buf;
1527 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001528 hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1529 lp->state.timer.function = at86rf230_async_state_timer;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001530
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001531 lp->irq.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001532 lp->irq.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001533 spi_message_init(&lp->irq.msg);
1534 lp->irq.msg.context = &lp->irq;
Alexander Aring263be332015-03-01 21:55:33 +01001535 lp->irq.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001536 lp->irq.trx.tx_buf = lp->irq.buf;
1537 lp->irq.trx.rx_buf = lp->irq.buf;
1538 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001539 hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1540 lp->irq.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001541
1542 lp->tx.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001543 lp->tx.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001544 spi_message_init(&lp->tx.msg);
1545 lp->tx.msg.context = &lp->tx;
Alexander Aring263be332015-03-01 21:55:33 +01001546 lp->tx.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001547 lp->tx.trx.tx_buf = lp->tx.buf;
1548 lp->tx.trx.rx_buf = lp->tx.buf;
1549 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001550 hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1551 lp->tx.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001552}
1553
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001554static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001555{
Alexander Aring5a504392014-10-25 17:16:34 +02001556 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001557 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001558 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001559 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001560 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001561
1562 if (!spi->irq) {
1563 dev_err(&spi->dev, "no IRQ specified\n");
1564 return -EINVAL;
1565 }
1566
Alexander Aringccdaeb22015-02-27 09:58:26 +01001567 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001568 if (rc < 0) {
1569 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1570 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001571 }
1572
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001573 if (gpio_is_valid(rstn)) {
1574 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001575 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001576 if (rc)
1577 return rc;
1578 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001579
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001580 if (gpio_is_valid(slp_tr)) {
1581 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001582 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001583 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001584 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001585 }
1586
1587 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001588 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001589 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001590 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001591 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001592 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001593 usleep_range(120, 240);
1594 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001595
Alexander Aring5a504392014-10-25 17:16:34 +02001596 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1597 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001598 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001599
Alexander Aring5a504392014-10-25 17:16:34 +02001600 lp = hw->priv;
1601 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001602 lp->spi = spi;
Alexander Aringd2c8bf52015-04-30 17:45:03 +02001603 lp->slp_tr = slp_tr;
Alexander Aring5a504392014-10-25 17:16:34 +02001604 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001605 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001606 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001607
Alexander Aringf76014f772014-07-03 00:20:44 +02001608 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1609 if (IS_ERR(lp->regmap)) {
1610 rc = PTR_ERR(lp->regmap);
1611 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1612 rc);
1613 goto free_dev;
1614 }
1615
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001616 at86rf230_setup_spi_messages(lp);
1617
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001618 rc = at86rf230_detect_device(lp);
1619 if (rc < 0)
1620 goto free_dev;
1621
Alexander Aring2e0571c2014-07-03 00:20:51 +02001622 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001623
1624 spi_set_drvdata(spi, lp);
1625
Alexander Aringccdaeb22015-02-27 09:58:26 +01001626 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001627 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001628 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001629
Alexander Aring19626942014-04-24 19:09:15 +02001630 /* Read irq status register to reset irq line */
1631 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001632 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001633 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001634
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001635 irq_type = irq_get_trigger_type(spi->irq);
1636 if (!irq_type)
1637 irq_type = IRQF_TRIGGER_RISING;
1638
1639 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1640 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001641 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001642 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001643
Alexander Aring5a504392014-10-25 17:16:34 +02001644 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001645 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001646 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001647
1648 return rc;
1649
Alexander Aring640985e2014-07-03 00:20:43 +02001650free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001651 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001652
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001653 return rc;
1654}
1655
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001656static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001657{
1658 struct at86rf230_local *lp = spi_get_drvdata(spi);
1659
Alexander Aring17e84a92014-03-31 03:26:51 +02001660 /* mask all at86rf230 irq's */
1661 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001662 ieee802154_unregister_hw(lp->hw);
1663 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001664 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001665
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001666 return 0;
1667}
1668
Alexander Aring1086b4f2014-04-24 19:09:11 +02001669static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001670 { .compatible = "atmel,at86rf230", },
1671 { .compatible = "atmel,at86rf231", },
1672 { .compatible = "atmel,at86rf233", },
1673 { .compatible = "atmel,at86rf212", },
1674 { },
1675};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001676MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001677
Alexander Aring90b15522014-04-24 19:09:12 +02001678static const struct spi_device_id at86rf230_device_id[] = {
1679 { .name = "at86rf230", },
1680 { .name = "at86rf231", },
1681 { .name = "at86rf233", },
1682 { .name = "at86rf212", },
1683 { },
1684};
1685MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1686
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001687static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001688 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001689 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001690 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001691 .name = "at86rf230",
1692 .owner = THIS_MODULE,
1693 },
1694 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001695 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001696};
1697
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001698module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001699
1700MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1701MODULE_LICENSE("GPL v2");