blob: 548d9d026a85e2ff0e329f0f61454f555ab1a522 [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>
Alexander Aring493bc902015-08-27 21:49:19 +020034#include <linux/debugfs.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000035
36#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020037#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000038
Alexander Aring7490b002015-05-17 21:44:57 +020039#include "at86rf230.h"
40
Alexander Aringa53d1f72014-07-03 00:20:46 +020041struct at86rf230_local;
42/* at86rf2xx chip depend data.
43 * All timings are in us.
44 */
45struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020046 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020047 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020048 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020049 u16 t_off_to_aack;
50 u16 t_off_to_tx_on;
Alexander Aringe6f7ed92015-06-13 22:15:54 +020051 u16 t_off_to_sleep;
52 u16 t_sleep_to_off;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020053 u16 t_frame;
54 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020055 int rssi_base_val;
56
Alexander Aringe37d2ec2014-10-28 18:21:19 +010057 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aring6f4da3f2015-05-17 21:44:49 +020058 int (*set_txpower)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020059};
60
Alexander Aringba6d2232015-03-01 21:55:28 +010061#define AT86RF2XX_MAX_BUF (127 + 3)
62/* tx retries to access the TX_ON state
63 * if it's above then force change will be started.
64 *
65 * We assume the max_frame_retries (7) value of 802.15.4 here.
66 */
67#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aringdce481e2015-03-09 13:56:11 +010068/* We use the recommended 5 minutes timeout to recalibrate */
69#define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
Alexander Aring1d15d6b2014-07-03 00:20:48 +020070
71struct at86rf230_state_change {
72 struct at86rf230_local *lp;
Alexander Aringcca990c2015-03-01 21:55:31 +010073 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020074
Alexander Aringeb3b435e2015-03-09 13:56:10 +010075 struct hrtimer timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020076 struct spi_message msg;
77 struct spi_transfer trx;
78 u8 buf[AT86RF2XX_MAX_BUF];
79
80 void (*complete)(void *context);
81 u8 from_state;
82 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020083
Alexander Aring57c1bc72015-09-21 09:37:54 +020084 bool free;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020085};
86
Alexander Aring493bc902015-08-27 21:49:19 +020087struct at86rf230_trac {
88 u64 success;
89 u64 success_data_pending;
90 u64 success_wait_for_ack;
91 u64 channel_access_failure;
92 u64 no_ack;
93 u64 invalid;
94};
95
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000096struct at86rf230_local {
97 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000098
Alexander Aring5a504392014-10-25 17:16:34 +020099 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200100 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +0200101 struct regmap *regmap;
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200102 int slp_tr;
Alexander Aringcbe62342015-06-16 11:07:42 +0200103 bool sleep;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000104
Alexander Aring2e0571c2014-07-03 00:20:51 +0200105 struct completion state_complete;
106 struct at86rf230_state_change state;
107
Alexander Aringdce481e2015-03-09 13:56:11 +0100108 unsigned long cal_timeout;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200109 bool is_tx;
Alexander Aring85009202015-04-30 17:45:02 +0200110 bool is_tx_from_off;
Alexander Aringba6d2232015-03-01 21:55:28 +0100111 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200112 struct sk_buff *tx_skb;
113 struct at86rf230_state_change tx;
Alexander Aring493bc902015-08-27 21:49:19 +0200114
115 struct at86rf230_trac trac;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000116};
117
Alexander Aringf76014f772014-07-03 00:20:44 +0200118#define AT86RF2XX_NUMREGS 0x3F
119
Alexander Aring97fed792014-10-07 10:38:32 +0200120static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200121at86rf230_async_state_change(struct at86rf230_local *lp,
122 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200123 const u8 state, void (*complete)(void *context));
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200124
Alexander Aringcbe62342015-06-16 11:07:42 +0200125static inline void
126at86rf230_sleep(struct at86rf230_local *lp)
127{
128 if (gpio_is_valid(lp->slp_tr)) {
129 gpio_set_value(lp->slp_tr, 1);
130 usleep_range(lp->data->t_off_to_sleep,
131 lp->data->t_off_to_sleep + 10);
132 lp->sleep = true;
133 }
134}
135
136static inline void
137at86rf230_awake(struct at86rf230_local *lp)
138{
139 if (gpio_is_valid(lp->slp_tr)) {
140 gpio_set_value(lp->slp_tr, 0);
141 usleep_range(lp->data->t_sleep_to_off,
142 lp->data->t_sleep_to_off + 100);
143 lp->sleep = false;
144 }
145}
146
Alexander Aringf76014f772014-07-03 00:20:44 +0200147static inline int
148__at86rf230_write(struct at86rf230_local *lp,
149 unsigned int addr, unsigned int data)
150{
Alexander Aringcbe62342015-06-16 11:07:42 +0200151 bool sleep = lp->sleep;
152 int ret;
153
154 /* awake for register setting if sleep */
155 if (sleep)
156 at86rf230_awake(lp);
157
158 ret = regmap_write(lp->regmap, addr, data);
159
160 /* sleep again if was sleeping */
161 if (sleep)
162 at86rf230_sleep(lp);
163
164 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200165}
166
167static inline int
168__at86rf230_read(struct at86rf230_local *lp,
169 unsigned int addr, unsigned int *data)
170{
Alexander Aringcbe62342015-06-16 11:07:42 +0200171 bool sleep = lp->sleep;
172 int ret;
173
174 /* awake for register setting if sleep */
175 if (sleep)
176 at86rf230_awake(lp);
177
178 ret = regmap_read(lp->regmap, addr, data);
179
180 /* sleep again if was sleeping */
181 if (sleep)
182 at86rf230_sleep(lp);
183
184 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200185}
186
187static inline int
188at86rf230_read_subreg(struct at86rf230_local *lp,
189 unsigned int addr, unsigned int mask,
190 unsigned int shift, unsigned int *data)
191{
192 int rc;
193
194 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100195 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200196 *data = (*data & mask) >> shift;
197
198 return rc;
199}
200
201static inline int
202at86rf230_write_subreg(struct at86rf230_local *lp,
203 unsigned int addr, unsigned int mask,
204 unsigned int shift, unsigned int data)
205{
Alexander Aringcbe62342015-06-16 11:07:42 +0200206 bool sleep = lp->sleep;
207 int ret;
208
209 /* awake for register setting if sleep */
210 if (sleep)
211 at86rf230_awake(lp);
212
213 ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
214
215 /* sleep again if was sleeping */
216 if (sleep)
217 at86rf230_sleep(lp);
218
219 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200220}
221
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200222static inline void
223at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
224{
225 gpio_set_value(lp->slp_tr, 1);
226 udelay(1);
227 gpio_set_value(lp->slp_tr, 0);
228}
229
Alexander Aringf76014f772014-07-03 00:20:44 +0200230static bool
231at86rf230_reg_writeable(struct device *dev, unsigned int reg)
232{
233 switch (reg) {
234 case RG_TRX_STATE:
235 case RG_TRX_CTRL_0:
236 case RG_TRX_CTRL_1:
237 case RG_PHY_TX_PWR:
238 case RG_PHY_ED_LEVEL:
239 case RG_PHY_CC_CCA:
240 case RG_CCA_THRES:
241 case RG_RX_CTRL:
242 case RG_SFD_VALUE:
243 case RG_TRX_CTRL_2:
244 case RG_ANT_DIV:
245 case RG_IRQ_MASK:
246 case RG_VREG_CTRL:
247 case RG_BATMON:
248 case RG_XOSC_CTRL:
249 case RG_RX_SYN:
250 case RG_XAH_CTRL_1:
251 case RG_FTN_CTRL:
252 case RG_PLL_CF:
253 case RG_PLL_DCU:
254 case RG_SHORT_ADDR_0:
255 case RG_SHORT_ADDR_1:
256 case RG_PAN_ID_0:
257 case RG_PAN_ID_1:
258 case RG_IEEE_ADDR_0:
259 case RG_IEEE_ADDR_1:
260 case RG_IEEE_ADDR_2:
261 case RG_IEEE_ADDR_3:
262 case RG_IEEE_ADDR_4:
263 case RG_IEEE_ADDR_5:
264 case RG_IEEE_ADDR_6:
265 case RG_IEEE_ADDR_7:
266 case RG_XAH_CTRL_0:
267 case RG_CSMA_SEED_0:
268 case RG_CSMA_SEED_1:
269 case RG_CSMA_BE:
270 return true;
271 default:
272 return false;
273 }
274}
275
276static bool
277at86rf230_reg_readable(struct device *dev, unsigned int reg)
278{
279 bool rc;
280
281 /* all writeable are also readable */
282 rc = at86rf230_reg_writeable(dev, reg);
283 if (rc)
284 return rc;
285
286 /* readonly regs */
287 switch (reg) {
288 case RG_TRX_STATUS:
289 case RG_PHY_RSSI:
290 case RG_IRQ_STATUS:
291 case RG_PART_NUM:
292 case RG_VERSION_NUM:
293 case RG_MAN_ID_1:
294 case RG_MAN_ID_0:
295 return true;
296 default:
297 return false;
298 }
299}
300
301static bool
302at86rf230_reg_volatile(struct device *dev, unsigned int reg)
303{
304 /* can be changed during runtime */
305 switch (reg) {
306 case RG_TRX_STATUS:
307 case RG_TRX_STATE:
308 case RG_PHY_RSSI:
309 case RG_PHY_ED_LEVEL:
310 case RG_IRQ_STATUS:
311 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100312 case RG_PLL_CF:
313 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200314 return true;
315 default:
316 return false;
317 }
318}
319
320static bool
321at86rf230_reg_precious(struct device *dev, unsigned int reg)
322{
323 /* don't clear irq line on read */
324 switch (reg) {
325 case RG_IRQ_STATUS:
326 return true;
327 default:
328 return false;
329 }
330}
331
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100332static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200333 .reg_bits = 8,
334 .val_bits = 8,
335 .write_flag_mask = CMD_REG | CMD_WRITE,
336 .read_flag_mask = CMD_REG,
337 .cache_type = REGCACHE_RBTREE,
338 .max_register = AT86RF2XX_NUMREGS,
339 .writeable_reg = at86rf230_reg_writeable,
340 .readable_reg = at86rf230_reg_readable,
341 .volatile_reg = at86rf230_reg_volatile,
342 .precious_reg = at86rf230_reg_precious,
343};
344
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200345static void
Alexander Aringc231c5a2016-02-19 09:59:12 +0100346at86rf230_async_error_recover_complete(void *context)
347{
348 struct at86rf230_state_change *ctx = context;
349 struct at86rf230_local *lp = ctx->lp;
350
351 if (ctx->free)
352 kfree(ctx);
353
354 ieee802154_wake_queue(lp->hw);
355}
356
357static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200358at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000359{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200360 struct at86rf230_state_change *ctx = context;
361 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000362
Alexander Aringa7a484b2015-03-26 12:46:30 +0100363 lp->is_tx = 0;
Alexander Aringc231c5a2016-02-19 09:59:12 +0100364 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
365 at86rf230_async_error_recover_complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200366}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000367
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100368static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200369at86rf230_async_error(struct at86rf230_local *lp,
370 struct at86rf230_state_change *ctx, int rc)
371{
372 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000373
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200374 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200375 at86rf230_async_error_recover);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200376}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000377
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200378/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200379static void
Alexander Aring57c1bc72015-09-21 09:37:54 +0200380at86rf230_async_read_reg(struct at86rf230_local *lp, u8 reg,
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200381 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200382 void (*complete)(void *context))
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200383{
Alexander Aring97fed792014-10-07 10:38:32 +0200384 int rc;
385
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200386 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000387
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200388 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200389 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200390 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200391 if (rc)
Alexander Aring97fed792014-10-07 10:38:32 +0200392 at86rf230_async_error(lp, ctx, rc);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200393}
394
395static void
396at86rf230_async_write_reg(struct at86rf230_local *lp, u8 reg, u8 val,
397 struct at86rf230_state_change *ctx,
398 void (*complete)(void *context))
399{
400 int rc;
401
402 ctx->buf[0] = (reg & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
403 ctx->buf[1] = val;
404 ctx->msg.complete = complete;
405 rc = spi_async(lp->spi, &ctx->msg);
406 if (rc)
407 at86rf230_async_error(lp, ctx, rc);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200408}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000409
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200410static void
411at86rf230_async_state_assert(void *context)
412{
413 struct at86rf230_state_change *ctx = context;
414 struct at86rf230_local *lp = ctx->lp;
415 const u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200416 const u8 trx_state = buf[1] & TRX_STATE_MASK;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000417
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200418 /* Assert state change */
419 if (trx_state != ctx->to_state) {
420 /* Special handling if transceiver state is in
421 * STATE_BUSY_RX_AACK and a SHR was detected.
422 */
423 if (trx_state == STATE_BUSY_RX_AACK) {
424 /* Undocumented race condition. If we send a state
425 * change to STATE_RX_AACK_ON the transceiver could
426 * change his state automatically to STATE_BUSY_RX_AACK
427 * if a SHR was detected. This is not an error, but we
428 * can't assert this.
429 */
430 if (ctx->to_state == STATE_RX_AACK_ON)
431 goto done;
432
433 /* If we change to STATE_TX_ON without forcing and
434 * transceiver state is STATE_BUSY_RX_AACK, we wait
435 * 'tFrame + tPAck' receiving time. In this time the
436 * PDU should be received. If the transceiver is still
437 * in STATE_BUSY_RX_AACK, we run a force state change
438 * to STATE_TX_ON. This is a timeout handling, if the
439 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100440 *
441 * Additional we do several retries to try to get into
442 * TX_ON state without forcing. If the retries are
443 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
444 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200445 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100446 if (ctx->to_state == STATE_TX_ON ||
447 ctx->to_state == STATE_TRX_OFF) {
448 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100449
450 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringed4a26b2015-08-27 21:49:18 +0200451 state = STATE_FORCE_TRX_OFF;
Alexander Aringba6d2232015-03-01 21:55:28 +0100452 lp->tx_retry++;
453
454 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200455 ctx->complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200456 return;
457 }
458 }
459
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200460 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
461 ctx->from_state, ctx->to_state, trx_state);
462 }
463
464done:
465 if (ctx->complete)
466 ctx->complete(context);
467}
468
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100469static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
470{
471 struct at86rf230_state_change *ctx =
472 container_of(timer, struct at86rf230_state_change, timer);
473 struct at86rf230_local *lp = ctx->lp;
474
475 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200476 at86rf230_async_state_assert);
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100477
478 return HRTIMER_NORESTART;
479}
480
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200481/* Do state change timing delay. */
482static void
483at86rf230_async_state_delay(void *context)
484{
485 struct at86rf230_state_change *ctx = context;
486 struct at86rf230_local *lp = ctx->lp;
487 struct at86rf2xx_chip_data *c = lp->data;
488 bool force = false;
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100489 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200490
491 /* The force state changes are will show as normal states in the
492 * state status subregister. We change the to_state to the
493 * corresponding one and remember if it was a force change, this
494 * differs if we do a state change from STATE_BUSY_RX_AACK.
495 */
496 switch (ctx->to_state) {
497 case STATE_FORCE_TX_ON:
498 ctx->to_state = STATE_TX_ON;
499 force = true;
500 break;
501 case STATE_FORCE_TRX_OFF:
502 ctx->to_state = STATE_TRX_OFF;
503 force = true;
504 break;
505 default:
506 break;
507 }
508
509 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200510 case STATE_TRX_OFF:
511 switch (ctx->to_state) {
512 case STATE_RX_AACK_ON:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100513 tim = c->t_off_to_aack * NSEC_PER_USEC;
Alexander Aring2ad33242015-04-30 17:44:59 +0200514 /* state change from TRX_OFF to RX_AACK_ON to do a
515 * calibration, we need to reset the timeout for the
516 * next one.
517 */
518 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200519 goto change;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200520 case STATE_TX_ARET_ON:
Alexander Aring2e0571c2014-07-03 00:20:51 +0200521 case STATE_TX_ON:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100522 tim = c->t_off_to_tx_on * NSEC_PER_USEC;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200523 /* state change from TRX_OFF to TX_ON or ARET_ON to do
524 * a calibration, we need to reset the timeout for the
Alexander Aringdce481e2015-03-09 13:56:11 +0100525 * next one.
526 */
527 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200528 goto change;
529 default:
530 break;
531 }
532 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200533 case STATE_BUSY_RX_AACK:
534 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100535 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200536 case STATE_TX_ON:
537 /* Wait for worst case receiving time if we
538 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100539 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200540 */
541 if (!force) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100542 tim = (c->t_frame + c->t_p_ack) * NSEC_PER_USEC;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200543 goto change;
544 }
545 break;
546 default:
547 break;
548 }
549 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200550 /* Default value, means RESET state */
551 case STATE_P_ON:
552 switch (ctx->to_state) {
553 case STATE_TRX_OFF:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100554 tim = c->t_reset_to_off * NSEC_PER_USEC;
Alexander Aring09e536c2014-07-03 00:20:52 +0200555 goto change;
556 default:
557 break;
558 }
559 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200560 default:
561 break;
562 }
563
564 /* Default delay is 1us in the most cases */
Alexander Aring8b44f0d2015-07-30 12:13:29 +0200565 udelay(1);
566 at86rf230_async_state_timer(&ctx->timer);
567 return;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200568
569change:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100570 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200571}
572
573static void
574at86rf230_async_state_change_start(void *context)
575{
576 struct at86rf230_state_change *ctx = context;
577 struct at86rf230_local *lp = ctx->lp;
578 u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200579 const u8 trx_state = buf[1] & TRX_STATE_MASK;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200580
581 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
582 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
583 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200584 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200585 at86rf230_async_state_change_start);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200586 return;
587 }
588
589 /* Check if we already are in the state which we change in */
590 if (trx_state == ctx->to_state) {
591 if (ctx->complete)
592 ctx->complete(context);
593 return;
594 }
595
596 /* Set current state to the context of state change */
597 ctx->from_state = trx_state;
598
599 /* Going into the next step for a state change which do a timing
600 * relevant delay.
601 */
Alexander Aring57c1bc72015-09-21 09:37:54 +0200602 at86rf230_async_write_reg(lp, RG_TRX_STATE, ctx->to_state, ctx,
603 at86rf230_async_state_delay);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000604}
605
Alexander Aring97fed792014-10-07 10:38:32 +0200606static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200607at86rf230_async_state_change(struct at86rf230_local *lp,
608 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200609 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000610{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200611 /* Initialization for the state change context */
612 ctx->to_state = state;
613 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200614 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200615 at86rf230_async_state_change_start);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200616}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000617
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200618static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200619at86rf230_sync_state_change_complete(void *context)
620{
621 struct at86rf230_state_change *ctx = context;
622 struct at86rf230_local *lp = ctx->lp;
623
624 complete(&lp->state_complete);
625}
626
627/* This function do a sync framework above the async state change.
628 * Some callbacks of the IEEE 802.15.4 driver interface need to be
629 * handled synchronously.
630 */
631static int
632at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
633{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100634 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200635
Alexander Aring97fed792014-10-07 10:38:32 +0200636 at86rf230_async_state_change(lp, &lp->state, state,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200637 at86rf230_sync_state_change_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200638
639 rc = wait_for_completion_timeout(&lp->state_complete,
640 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200641 if (!rc) {
642 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200643 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200644 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200645
646 return 0;
647}
648
649static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200650at86rf230_tx_complete(void *context)
651{
652 struct at86rf230_state_change *ctx = context;
653 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000654
Alexander Aringfc0719e2015-08-10 21:15:57 +0200655 ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200656 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200657}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000658
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200659static void
660at86rf230_tx_on(void *context)
661{
662 struct at86rf230_state_change *ctx = context;
663 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000664
Alexander Aring31fa7432015-03-01 21:55:32 +0100665 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200666 at86rf230_tx_complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200667}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000668
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200669static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200670at86rf230_tx_trac_check(void *context)
671{
672 struct at86rf230_state_change *ctx = context;
673 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000674
Alexander Aring493bc902015-08-27 21:49:19 +0200675 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
676 u8 trac = TRAC_MASK(ctx->buf[1]);
677
678 switch (trac) {
679 case TRAC_SUCCESS:
680 lp->trac.success++;
681 break;
682 case TRAC_SUCCESS_DATA_PENDING:
683 lp->trac.success_data_pending++;
684 break;
685 case TRAC_CHANNEL_ACCESS_FAILURE:
686 lp->trac.channel_access_failure++;
687 break;
688 case TRAC_NO_ACK:
689 lp->trac.no_ack++;
690 break;
691 case TRAC_INVALID:
692 lp->trac.invalid++;
693 break;
694 default:
695 WARN_ONCE(1, "received tx trac status %d\n", trac);
696 break;
697 }
698 }
699
Alexander Aring57c1bc72015-09-21 09:37:54 +0200700 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, at86rf230_tx_on);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200701}
702
703static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100704at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200705{
Alexander Aring74de4c82015-03-01 21:55:30 +0100706 struct at86rf230_state_change *ctx = context;
707 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100708 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100709 struct sk_buff *skb;
710 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200711
Alexander Aring74de4c82015-03-01 21:55:30 +0100712 len = buf[1];
713 if (!ieee802154_is_valid_psdu_len(len)) {
714 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
715 len = IEEE802154_MTU;
716 }
717 lqi = buf[2 + len];
718
Alexander Aring61a22812014-10-27 17:13:29 +0100719 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200720 if (!skb) {
721 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
Alexander Aring57c1bc72015-09-21 09:37:54 +0200722 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200723 return;
724 }
725
Johannes Berg59ae1d12017-06-16 14:29:20 +0200726 skb_put_data(skb, buf + 2, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100727 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200728 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200729}
730
731static void
Alexander Aring493bc902015-08-27 21:49:19 +0200732at86rf230_rx_trac_check(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200733{
Alexander Aringcca990c2015-03-01 21:55:31 +0100734 struct at86rf230_state_change *ctx = context;
735 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100736 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200737 int rc;
738
Alexander Aring493bc902015-08-27 21:49:19 +0200739 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
740 u8 trac = TRAC_MASK(buf[1]);
741
742 switch (trac) {
743 case TRAC_SUCCESS:
744 lp->trac.success++;
745 break;
746 case TRAC_SUCCESS_WAIT_FOR_ACK:
747 lp->trac.success_wait_for_ack++;
748 break;
749 case TRAC_INVALID:
750 lp->trac.invalid++;
751 break;
752 default:
753 WARN_ONCE(1, "received rx trac status %d\n", trac);
754 break;
755 }
756 }
757
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200758 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100759 ctx->trx.len = AT86RF2XX_MAX_BUF;
760 ctx->msg.complete = at86rf230_rx_read_frame_complete;
761 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200762 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100763 ctx->trx.len = 2;
Alexander Aring31fa7432015-03-01 21:55:32 +0100764 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200765 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200766}
767
768static void
Alexander Aring57c1bc72015-09-21 09:37:54 +0200769at86rf230_irq_trx_end(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200770{
Alexander Aring57c1bc72015-09-21 09:37:54 +0200771 struct at86rf230_state_change *ctx = context;
772 struct at86rf230_local *lp = ctx->lp;
773
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200774 if (lp->is_tx) {
775 lp->is_tx = 0;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200776 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
777 at86rf230_tx_trac_check);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200778 } else {
Alexander Aring57c1bc72015-09-21 09:37:54 +0200779 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
780 at86rf230_rx_trac_check);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200781 }
782}
783
784static void
785at86rf230_irq_status(void *context)
786{
787 struct at86rf230_state_change *ctx = context;
788 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100789 const u8 *buf = ctx->buf;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200790 u8 irq = buf[1];
791
792 enable_irq(lp->spi->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200793
794 if (irq & IRQ_TRX_END) {
Alexander Aring57c1bc72015-09-21 09:37:54 +0200795 at86rf230_irq_trx_end(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200796 } else {
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200797 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
798 irq);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200799 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200800 }
801}
802
Alexander Aring57c1bc72015-09-21 09:37:54 +0200803static void
804at86rf230_setup_spi_messages(struct at86rf230_local *lp,
805 struct at86rf230_state_change *state)
806{
807 state->lp = lp;
808 state->irq = lp->spi->irq;
809 spi_message_init(&state->msg);
810 state->msg.context = state;
811 state->trx.len = 2;
812 state->trx.tx_buf = state->buf;
813 state->trx.rx_buf = state->buf;
814 spi_message_add_tail(&state->trx, &state->msg);
815 hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
816 state->timer.function = at86rf230_async_state_timer;
817}
818
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200819static irqreturn_t at86rf230_isr(int irq, void *data)
820{
821 struct at86rf230_local *lp = data;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200822 struct at86rf230_state_change *ctx;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200823 int rc;
824
Alexander Aring90566362014-10-07 10:38:29 +0200825 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200826
Alexander Aring57c1bc72015-09-21 09:37:54 +0200827 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
828 if (!ctx) {
829 enable_irq(irq);
830 return IRQ_NONE;
831 }
832
833 at86rf230_setup_spi_messages(lp, ctx);
834 /* tell on error handling to free ctx */
835 ctx->free = true;
836
837 ctx->buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200838 ctx->msg.complete = at86rf230_irq_status;
839 rc = spi_async(lp->spi, &ctx->msg);
840 if (rc) {
841 at86rf230_async_error(lp, ctx, rc);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200842 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200843 return IRQ_NONE;
844 }
845
846 return IRQ_HANDLED;
847}
848
849static void
850at86rf230_write_frame_complete(void *context)
851{
852 struct at86rf230_state_change *ctx = context;
853 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200854
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855 ctx->trx.len = 2;
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200856
Alexander Aring57c1bc72015-09-21 09:37:54 +0200857 if (gpio_is_valid(lp->slp_tr))
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200858 at86rf230_slp_tr_rising_edge(lp);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200859 else
860 at86rf230_async_write_reg(lp, RG_TRX_STATE, STATE_BUSY_TX, ctx,
861 NULL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200862}
863
864static void
865at86rf230_write_frame(void *context)
866{
867 struct at86rf230_state_change *ctx = context;
868 struct at86rf230_local *lp = ctx->lp;
869 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100870 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200871 int rc;
872
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200873 lp->is_tx = 1;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200874
875 buf[0] = CMD_FB | CMD_WRITE;
876 buf[1] = skb->len + 2;
877 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100878 ctx->trx.len = skb->len + 2;
879 ctx->msg.complete = at86rf230_write_frame_complete;
880 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100881 if (rc) {
882 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200883 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100884 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200885}
886
887static void
888at86rf230_xmit_tx_on(void *context)
889{
890 struct at86rf230_state_change *ctx = context;
891 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200892
Alexander Aring97fed792014-10-07 10:38:32 +0200893 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200894 at86rf230_write_frame);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200895}
896
Alexander Aringdce481e2015-03-09 13:56:11 +0100897static void
898at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200899{
Alexander Aringdce481e2015-03-09 13:56:11 +0100900 struct at86rf230_state_change *ctx = context;
901 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200902
Alexander Aringfc0719e2015-08-10 21:15:57 +0200903 /* check if we change from off state */
Alexander Aringd981b5b2016-02-19 09:59:13 +0100904 if (lp->is_tx_from_off)
Alexander Aringfc0719e2015-08-10 21:15:57 +0200905 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200906 at86rf230_write_frame);
Alexander Aringd981b5b2016-02-19 09:59:13 +0100907 else
Alexander Aringdce481e2015-03-09 13:56:11 +0100908 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200909 at86rf230_xmit_tx_on);
Alexander Aringdce481e2015-03-09 13:56:11 +0100910}
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200911
Alexander Aringdce481e2015-03-09 13:56:11 +0100912static int
913at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
914{
915 struct at86rf230_local *lp = hw->priv;
916 struct at86rf230_state_change *ctx = &lp->tx;
917
918 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +0100919 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +0100920
921 /* After 5 minutes in PLL and the same frequency we run again the
922 * calibration loops which is recommended by at86rf2xx datasheets.
923 *
924 * The calibration is initiate by a state change from TRX_OFF
925 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
926 * function then to start in the next 5 minutes.
927 */
Alexander Aring85009202015-04-30 17:45:02 +0200928 if (time_is_before_jiffies(lp->cal_timeout)) {
929 lp->is_tx_from_off = true;
Alexander Aringdce481e2015-03-09 13:56:11 +0100930 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200931 at86rf230_xmit_start);
Alexander Aring85009202015-04-30 17:45:02 +0200932 } else {
Alexander Aringd981b5b2016-02-19 09:59:13 +0100933 lp->is_tx_from_off = false;
Alexander Aringdce481e2015-03-09 13:56:11 +0100934 at86rf230_xmit_start(ctx);
Alexander Aring85009202015-04-30 17:45:02 +0200935 }
Alexander Aring97fed792014-10-07 10:38:32 +0200936
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200937 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000938}
939
940static int
Alexander Aring5a504392014-10-25 17:16:34 +0200941at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000942{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000943 BUG_ON(!level);
944 *level = 0xbe;
945 return 0;
946}
947
948static int
Alexander Aring5a504392014-10-25 17:16:34 +0200949at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000950{
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200951 struct at86rf230_local *lp = hw->priv;
952
Alexander Aring493bc902015-08-27 21:49:19 +0200953 /* reset trac stats on start */
954 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS))
955 memset(&lp->trac, 0, sizeof(struct at86rf230_trac));
956
Alexander Aringcbe62342015-06-16 11:07:42 +0200957 at86rf230_awake(lp);
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200958 enable_irq(lp->spi->irq);
959
Alexander Aring30811fa2015-06-16 11:07:44 +0200960 return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000961}
962
963static void
Alexander Aring5a504392014-10-25 17:16:34 +0200964at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000965{
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200966 struct at86rf230_local *lp = hw->priv;
Alexander Aring74ed9d92015-06-16 11:07:43 +0200967 u8 csma_seed[2];
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200968
Alexander Aring30811fa2015-06-16 11:07:44 +0200969 at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200970
971 disable_irq(lp->spi->irq);
Alexander Aring74ed9d92015-06-16 11:07:43 +0200972
973 /* It's recommended to set random new csma_seeds before sleep state.
974 * Makes only sense in the stop callback, not doing this inside of
975 * at86rf230_sleep, this is also used when we don't transmit afterwards
976 * when calling start callback again.
977 */
978 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
979 at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
980 at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
981
Alexander Aringcbe62342015-06-16 11:07:42 +0200982 at86rf230_sleep(lp);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000983}
984
985static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100986at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100987{
988 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
989}
990
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200991#define AT86RF2XX_MAX_ED_LEVELS 0xF
Alexander Aring4d7101d2016-09-06 09:50:04 +0200992static const s32 at86rf233_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
993 -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, -7800, -7600,
994 -7400, -7200, -7000, -6800, -6600, -6400,
995};
996
997static const s32 at86rf231_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200998 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
999 -7100, -6900, -6700, -6500, -6300, -6100,
1000};
1001
1002static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1003 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
Alexander Aring179655f2015-05-20 09:27:03 +02001004 -8000, -7800, -7600, -7400, -7200, -7000,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001005};
1006
1007static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1008 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
Alexander Aring179655f2015-05-20 09:27:03 +02001009 -7800, -7600, -7400, -7200, -7000, -6800,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001010};
1011
1012static inline int
1013at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
1014{
1015 unsigned int cca_ed_thres;
1016 int rc;
1017
1018 rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
1019 if (rc < 0)
1020 return rc;
1021
1022 switch (rssi_base_val) {
1023 case -98:
1024 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
1025 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
1026 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
1027 break;
1028 case -100:
1029 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1030 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1031 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
1032 break;
1033 default:
1034 WARN_ON(1);
1035 }
1036
1037 return 0;
1038}
1039
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001040static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001041at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001042{
1043 int rc;
1044
1045 if (channel == 0)
1046 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1047 else
1048 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1049 if (rc < 0)
1050 return rc;
1051
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001052 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001053 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001054 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001055 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001056 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001057 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001058 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001059 if (rc < 0)
1060 return rc;
1061
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001062 rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
1063 if (rc < 0)
1064 return rc;
1065
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001066 /* This sets the symbol_duration according frequency on the 212.
1067 * TODO move this handling while set channel and page in cfg802154.
1068 * We can do that, this timings are according 802.15.4 standard.
1069 * If we do that in cfg802154, this is a more generic calculation.
1070 *
1071 * This should also protected from ifs_timer. Means cancel timer and
1072 * init with a new value. For now, this is okay.
1073 */
1074 if (channel == 0) {
1075 if (page == 0) {
1076 /* SUB:0 and BPSK:0 -> BPSK-20 */
1077 lp->hw->phy->symbol_duration = 50;
1078 } else {
1079 /* SUB:1 and BPSK:0 -> BPSK-40 */
1080 lp->hw->phy->symbol_duration = 25;
1081 }
1082 } else {
1083 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001084 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001085 lp->hw->phy->symbol_duration = 40;
1086 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001087 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001088 lp->hw->phy->symbol_duration = 16;
1089 }
1090
1091 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1092 lp->hw->phy->symbol_duration;
1093 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1094 lp->hw->phy->symbol_duration;
1095
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001096 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1097}
1098
1099static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001100at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001101{
Alexander Aring5a504392014-10-25 17:16:34 +02001102 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001103 int rc;
1104
Alexander Aringa53d1f72014-07-03 00:20:46 +02001105 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001106 /* Wait for PLL */
1107 usleep_range(lp->data->t_channel_switch,
1108 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001109
1110 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001111 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001112}
1113
1114static int
Alexander Aring5a504392014-10-25 17:16:34 +02001115at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001116 struct ieee802154_hw_addr_filt *filt,
1117 unsigned long changed)
1118{
Alexander Aring5a504392014-10-25 17:16:34 +02001119 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001120
Alexander Aring57205c12014-10-25 05:25:09 +02001121 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001122 u16 addr = le16_to_cpu(filt->short_addr);
1123
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001124 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001125 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001126 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1127 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001128 }
1129
Alexander Aring57205c12014-10-25 05:25:09 +02001130 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001131 u16 pan = le16_to_cpu(filt->pan_id);
1132
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001133 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001134 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001135 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1136 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001137 }
1138
Alexander Aring57205c12014-10-25 05:25:09 +02001139 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001140 u8 i, addr[8];
1141
1142 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001143 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001144 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001145 for (i = 0; i < 8; i++)
1146 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001147 }
1148
Alexander Aring57205c12014-10-25 05:25:09 +02001149 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001150 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001151 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001152 if (filt->pan_coord)
1153 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1154 else
1155 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1156 }
1157
1158 return 0;
1159}
1160
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001161#define AT86RF23X_MAX_TX_POWERS 0xF
1162static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1163 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1164 -800, -1200, -1700,
1165};
1166
1167static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1168 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1169 -900, -1200, -1700,
1170};
1171
1172#define AT86RF212_MAX_TX_POWERS 0x1F
1173static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1174 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1175 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1176 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1177};
1178
1179static int
1180at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1181{
1182 u32 i;
1183
1184 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1185 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1186 return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1187 }
1188
1189 return -EINVAL;
1190}
1191
1192static int
1193at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1194{
1195 u32 i;
1196
1197 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1198 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1199 return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1200 }
1201
1202 return -EINVAL;
1203}
1204
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001205static int
Alexander Aringe2eb1732015-05-17 21:44:40 +02001206at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001207{
Alexander Aring5a504392014-10-25 17:16:34 +02001208 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001209
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001210 return lp->data->set_txpower(lp, mbm);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001211}
1212
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001213static int
Alexander Aring5a504392014-10-25 17:16:34 +02001214at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001215{
Alexander Aring5a504392014-10-25 17:16:34 +02001216 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001217
1218 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1219}
1220
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001221static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001222at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1223 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001224{
Alexander Aring5a504392014-10-25 17:16:34 +02001225 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001226 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001227
Alexander Aring7fe9a382014-12-10 15:33:12 +01001228 /* mapping 802.15.4 to driver spec */
1229 switch (cca->mode) {
1230 case NL802154_CCA_ENERGY:
1231 val = 1;
1232 break;
1233 case NL802154_CCA_CARRIER:
1234 val = 2;
1235 break;
1236 case NL802154_CCA_ENERGY_CARRIER:
1237 switch (cca->opt) {
1238 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1239 val = 3;
1240 break;
1241 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1242 val = 0;
1243 break;
1244 default:
1245 return -EINVAL;
1246 }
1247 break;
1248 default:
1249 return -EINVAL;
1250 }
1251
1252 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001253}
1254
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001255
1256static int
Alexander Aring32b23552015-05-17 21:44:41 +02001257at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001258{
Alexander Aring5a504392014-10-25 17:16:34 +02001259 struct at86rf230_local *lp = hw->priv;
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001260 u32 i;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001261
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001262 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1263 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1264 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1265 }
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001266
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001267 return -EINVAL;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001268}
1269
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001270static int
Alexander Aring5a504392014-10-25 17:16:34 +02001271at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001272 u8 retries)
1273{
Alexander Aring5a504392014-10-25 17:16:34 +02001274 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001275 int rc;
1276
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001277 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1278 if (rc)
1279 return rc;
1280
1281 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1282 if (rc)
1283 return rc;
1284
Alexander Aring39d7f322014-04-05 13:49:26 +02001285 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001286}
1287
1288static int
Alexander Aring5a504392014-10-25 17:16:34 +02001289at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001290{
Alexander Aring5a504392014-10-25 17:16:34 +02001291 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001292
Alexander Aringfc0719e2015-08-10 21:15:57 +02001293 return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001294}
1295
Alexander Aring92f45f542014-10-29 21:34:33 +01001296static int
1297at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1298{
1299 struct at86rf230_local *lp = hw->priv;
1300 int rc;
1301
1302 if (on) {
1303 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1304 if (rc < 0)
1305 return rc;
1306
1307 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1308 if (rc < 0)
1309 return rc;
1310 } else {
1311 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1312 if (rc < 0)
1313 return rc;
1314
1315 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1316 if (rc < 0)
1317 return rc;
1318 }
1319
1320 return 0;
1321}
1322
Alexander Aring16301862014-10-28 18:21:18 +01001323static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001324 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001325 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001326 .ed = at86rf230_ed,
1327 .set_channel = at86rf230_channel,
1328 .start = at86rf230_start,
1329 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001330 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001331 .set_txpower = at86rf230_set_txpower,
1332 .set_lbt = at86rf230_set_lbt,
1333 .set_cca_mode = at86rf230_set_cca_mode,
1334 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1335 .set_csma_params = at86rf230_set_csma_params,
1336 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f542014-10-29 21:34:33 +01001337 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001338};
1339
Alexander Aringa53d1f72014-07-03 00:20:46 +02001340static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001341 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001342 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001343 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001344 .t_off_to_aack = 80,
1345 .t_off_to_tx_on = 80,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001346 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001347 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001348 .t_frame = 4096,
1349 .t_p_ack = 545,
Alexander Aring4d7101d2016-09-06 09:50:04 +02001350 .rssi_base_val = -94,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001351 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001352 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001353};
1354
1355static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001356 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001357 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001358 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001359 .t_off_to_aack = 110,
1360 .t_off_to_tx_on = 110,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001361 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001362 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001363 .t_frame = 4096,
1364 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001365 .rssi_base_val = -91,
1366 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001367 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001368};
1369
1370static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001371 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001372 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001373 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001374 .t_off_to_aack = 200,
1375 .t_off_to_tx_on = 200,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001376 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001377 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001378 .t_frame = 4096,
1379 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001380 .rssi_base_val = -100,
1381 .set_channel = at86rf212_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001382 .set_txpower = at86rf212_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001383};
1384
Alexander Aringccdaeb22015-02-27 09:58:26 +01001385static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001386{
Alexander Aring1db05582014-07-03 00:20:50 +02001387 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001388 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001389 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001390
Alexander Aring09e536c2014-07-03 00:20:52 +02001391 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001392 if (rc)
1393 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001394
Alexander Aring4af619a2014-04-24 19:09:05 +02001395 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring702d2112015-02-27 09:58:29 +01001396 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1397 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001398 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001399
Alexander Aring18c65042014-04-24 19:09:18 +02001400 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001401 if (rc)
1402 return rc;
1403
Alexander Aring6bd2b132014-07-03 00:20:49 +02001404 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1405 if (rc)
1406 return rc;
1407
Sascha Herrmann057dad62013-04-14 22:33:29 +00001408 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001409 if (rc)
1410 return rc;
1411
Alexander Aringbe64f072015-02-27 09:58:28 +01001412 /* reset values differs in at86rf231 and at86rf233 */
1413 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1414 if (rc)
1415 return rc;
1416
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001417 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1418 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1419 if (rc)
1420 return rc;
1421 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1422 if (rc)
1423 return rc;
1424
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001425 /* CLKM changes are applied immediately */
1426 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1427 if (rc)
1428 return rc;
1429
1430 /* Turn CLKM Off */
1431 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1432 if (rc)
1433 return rc;
1434 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001435 usleep_range(lp->data->t_sleep_cycle,
1436 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001437
Alexander Aringccdaeb22015-02-27 09:58:26 +01001438 /* xtal_trim value is calculated by:
1439 * CL = 0.5 * (CX + CTRIM + CPAR)
1440 *
1441 * whereas:
1442 * CL = capacitor of used crystal
1443 * CX = connected capacitors at xtal pins
1444 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1445 * but this is different on each board setup. You need to fine
1446 * tuning this value via CTRIM.
1447 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1448 * 0 pF upto 4.5 pF.
1449 *
1450 * Examples:
1451 * atben transceiver:
1452 *
1453 * CL = 8 pF
1454 * CX = 12 pF
1455 * CPAR = 3 pF (We assume the magic constant from datasheet)
1456 * CTRIM = 0.9 pF
1457 *
1458 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1459 *
1460 * xtal_trim = 0x3
1461 *
1462 * openlabs transceiver:
1463 *
1464 * CL = 16 pF
1465 * CX = 22 pF
1466 * CPAR = 3 pF (We assume the magic constant from datasheet)
1467 * CTRIM = 4.5 pF
1468 *
1469 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1470 *
1471 * xtal_trim = 0xf
1472 */
1473 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1474 if (rc)
1475 return rc;
1476
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001477 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001478 if (rc)
1479 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001480 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001481 dev_err(&lp->spi->dev, "DVDD error\n");
1482 return -EINVAL;
1483 }
1484
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001485 /* Force setting slotted operation bit to 0. Sometimes the atben
1486 * sets this bit and I don't know why. We set this always force
1487 * to zero while probing.
1488 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001489 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001490}
1491
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001492static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001493at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1494 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001495{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001496 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001497 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001498
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001499 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1500 if (!pdata)
1501 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001502
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001503 *rstn = pdata->rstn;
1504 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001505 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001506 return 0;
1507 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001508
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001509 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1510 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001511 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1512 if (ret < 0 && ret != -EINVAL)
1513 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001514
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001515 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001516}
1517
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001518static int
1519at86rf230_detect_device(struct at86rf230_local *lp)
1520{
1521 unsigned int part, version, val;
1522 u16 man_id = 0;
1523 const char *chip;
1524 int rc;
1525
1526 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1527 if (rc)
1528 return rc;
1529 man_id |= val;
1530
1531 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1532 if (rc)
1533 return rc;
1534 man_id |= (val << 8);
1535
1536 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1537 if (rc)
1538 return rc;
1539
Andrey Yurovsky75989682014-12-17 13:14:42 -08001540 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001541 if (rc)
1542 return rc;
1543
1544 if (man_id != 0x001f) {
1545 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1546 man_id >> 8, man_id & 0xFF);
1547 return -EINVAL;
1548 }
1549
Alexander Aringf265be32015-06-06 17:30:48 +02001550 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
Alexander Aringedea8f72015-05-17 21:44:46 +02001551 IEEE802154_HW_CSMA_PARAMS |
1552 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1553 IEEE802154_HW_PROMISCUOUS;
1554
1555 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1556 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1557 WPAN_PHY_FLAG_CCA_MODE;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001558
Alexander Aring8377d222015-05-17 21:44:48 +02001559 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1560 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1561 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1562 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1563
Alexander Aringb48a7c12014-12-10 15:33:14 +01001564 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1565
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001566 switch (part) {
1567 case 2:
1568 chip = "at86rf230";
1569 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001570 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001571 case 3:
1572 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001573 lp->data = &at86rf231_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001574 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001575 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001576 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001577 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1578 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
Alexander Aring4d7101d2016-09-06 09:50:04 +02001579 lp->hw->phy->supported.cca_ed_levels = at86rf231_ed_levels;
1580 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf231_ed_levels);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001581 break;
1582 case 7:
1583 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001584 lp->data = &at86rf212_data;
1585 lp->hw->flags |= IEEE802154_HW_LBT;
Alexander Aring72f655e2015-05-17 21:44:42 +02001586 lp->hw->phy->supported.channels[0] = 0x00007FF;
1587 lp->hw->phy->supported.channels[2] = 0x00007FF;
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001588 lp->hw->phy->current_channel = 5;
1589 lp->hw->phy->symbol_duration = 25;
Alexander Aring8377d222015-05-17 21:44:48 +02001590 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001591 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1592 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001593 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1594 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001595 break;
1596 case 11:
1597 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001598 lp->data = &at86rf233_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001599 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001600 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001601 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001602 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1603 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
Alexander Aring4d7101d2016-09-06 09:50:04 +02001604 lp->hw->phy->supported.cca_ed_levels = at86rf233_ed_levels;
1605 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf233_ed_levels);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001606 break;
1607 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001608 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001609 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001610 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001611 }
1612
Alexander Aringcc643492015-05-17 21:44:51 +02001613 lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
Alexander Aring392f4e62015-05-17 21:44:52 +02001614 lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
Alexander Aringcc643492015-05-17 21:44:51 +02001615
1616not_supp:
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001617 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1618
1619 return rc;
1620}
1621
Alexander Aring493bc902015-08-27 21:49:19 +02001622#ifdef CONFIG_IEEE802154_AT86RF230_DEBUGFS
1623static struct dentry *at86rf230_debugfs_root;
1624
1625static int at86rf230_stats_show(struct seq_file *file, void *offset)
1626{
1627 struct at86rf230_local *lp = file->private;
Alexander Aring493bc902015-08-27 21:49:19 +02001628
Stephen Rothwell97170ea2015-09-21 20:41:44 -07001629 seq_printf(file, "SUCCESS:\t\t%8llu\n", lp->trac.success);
1630 seq_printf(file, "SUCCESS_DATA_PENDING:\t%8llu\n",
1631 lp->trac.success_data_pending);
1632 seq_printf(file, "SUCCESS_WAIT_FOR_ACK:\t%8llu\n",
1633 lp->trac.success_wait_for_ack);
1634 seq_printf(file, "CHANNEL_ACCESS_FAILURE:\t%8llu\n",
1635 lp->trac.channel_access_failure);
1636 seq_printf(file, "NO_ACK:\t\t\t%8llu\n", lp->trac.no_ack);
1637 seq_printf(file, "INVALID:\t\t%8llu\n", lp->trac.invalid);
1638 return 0;
Alexander Aring493bc902015-08-27 21:49:19 +02001639}
1640
1641static int at86rf230_stats_open(struct inode *inode, struct file *file)
1642{
1643 return single_open(file, at86rf230_stats_show, inode->i_private);
1644}
1645
1646static const struct file_operations at86rf230_stats_fops = {
1647 .open = at86rf230_stats_open,
1648 .read = seq_read,
1649 .llseek = seq_lseek,
1650 .release = single_release,
1651};
1652
1653static int at86rf230_debugfs_init(struct at86rf230_local *lp)
1654{
1655 char debugfs_dir_name[DNAME_INLINE_LEN + 1] = "at86rf230-";
1656 struct dentry *stats;
1657
1658 strncat(debugfs_dir_name, dev_name(&lp->spi->dev), DNAME_INLINE_LEN);
1659
1660 at86rf230_debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
1661 if (!at86rf230_debugfs_root)
1662 return -ENOMEM;
1663
1664 stats = debugfs_create_file("trac_stats", S_IRUGO,
1665 at86rf230_debugfs_root, lp,
1666 &at86rf230_stats_fops);
1667 if (!stats)
1668 return -ENOMEM;
1669
1670 return 0;
1671}
1672
1673static void at86rf230_debugfs_remove(void)
1674{
1675 debugfs_remove_recursive(at86rf230_debugfs_root);
1676}
1677#else
1678static int at86rf230_debugfs_init(struct at86rf230_local *lp) { return 0; }
1679static void at86rf230_debugfs_remove(void) { }
1680#endif
1681
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001682static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001683{
Alexander Aring5a504392014-10-25 17:16:34 +02001684 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001685 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001686 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001687 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001688 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001689
1690 if (!spi->irq) {
1691 dev_err(&spi->dev, "no IRQ specified\n");
1692 return -EINVAL;
1693 }
1694
Alexander Aringccdaeb22015-02-27 09:58:26 +01001695 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001696 if (rc < 0) {
1697 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1698 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001699 }
1700
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001701 if (gpio_is_valid(rstn)) {
1702 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001703 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001704 if (rc)
1705 return rc;
1706 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001707
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001708 if (gpio_is_valid(slp_tr)) {
1709 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001710 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001711 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001712 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001713 }
1714
1715 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001716 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001717 udelay(1);
Andrey Smirnovf3016062016-12-18 15:25:33 -08001718 gpio_set_value_cansleep(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001719 udelay(1);
Andrey Smirnovf3016062016-12-18 15:25:33 -08001720 gpio_set_value_cansleep(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001721 usleep_range(120, 240);
1722 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001723
Alexander Aring5a504392014-10-25 17:16:34 +02001724 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1725 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001726 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001727
Alexander Aring5a504392014-10-25 17:16:34 +02001728 lp = hw->priv;
1729 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001730 lp->spi = spi;
Alexander Aringd2c8bf52015-04-30 17:45:03 +02001731 lp->slp_tr = slp_tr;
Alexander Aring5a504392014-10-25 17:16:34 +02001732 hw->parent = &spi->dev;
Alexander Aringf6f4e862014-11-05 20:51:26 +01001733 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001734
Alexander Aringf76014f772014-07-03 00:20:44 +02001735 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1736 if (IS_ERR(lp->regmap)) {
1737 rc = PTR_ERR(lp->regmap);
1738 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1739 rc);
1740 goto free_dev;
1741 }
1742
Alexander Aring57c1bc72015-09-21 09:37:54 +02001743 at86rf230_setup_spi_messages(lp, &lp->state);
1744 at86rf230_setup_spi_messages(lp, &lp->tx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001745
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001746 rc = at86rf230_detect_device(lp);
1747 if (rc < 0)
1748 goto free_dev;
1749
Alexander Aring2e0571c2014-07-03 00:20:51 +02001750 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001751
1752 spi_set_drvdata(spi, lp);
1753
Alexander Aringccdaeb22015-02-27 09:58:26 +01001754 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001755 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001756 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001757
Alexander Aring19626942014-04-24 19:09:15 +02001758 /* Read irq status register to reset irq line */
1759 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001760 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001761 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001762
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001763 irq_type = irq_get_trigger_type(spi->irq);
1764 if (!irq_type)
Alexander Aring9ff19e62015-06-13 22:15:53 +02001765 irq_type = IRQF_TRIGGER_HIGH;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001766
1767 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1768 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001769 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001770 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001771
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001772 /* disable_irq by default and wait for starting hardware */
1773 disable_irq(spi->irq);
1774
1775 /* going into sleep by default */
Alexander Aringcbe62342015-06-16 11:07:42 +02001776 at86rf230_sleep(lp);
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001777
Alexander Aring493bc902015-08-27 21:49:19 +02001778 rc = at86rf230_debugfs_init(lp);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001779 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001780 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001781
Alexander Aring493bc902015-08-27 21:49:19 +02001782 rc = ieee802154_register_hw(lp->hw);
1783 if (rc)
1784 goto free_debugfs;
1785
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001786 return rc;
1787
Alexander Aring493bc902015-08-27 21:49:19 +02001788free_debugfs:
1789 at86rf230_debugfs_remove();
Alexander Aring640985e2014-07-03 00:20:43 +02001790free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001791 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001792
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001793 return rc;
1794}
1795
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001796static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001797{
1798 struct at86rf230_local *lp = spi_get_drvdata(spi);
1799
Alexander Aring17e84a92014-03-31 03:26:51 +02001800 /* mask all at86rf230 irq's */
1801 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001802 ieee802154_unregister_hw(lp->hw);
1803 ieee802154_free_hw(lp->hw);
Alexander Aring493bc902015-08-27 21:49:19 +02001804 at86rf230_debugfs_remove();
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001805 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001806
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001807 return 0;
1808}
1809
Alexander Aring1086b4f2014-04-24 19:09:11 +02001810static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001811 { .compatible = "atmel,at86rf230", },
1812 { .compatible = "atmel,at86rf231", },
1813 { .compatible = "atmel,at86rf233", },
1814 { .compatible = "atmel,at86rf212", },
1815 { },
1816};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001817MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001818
Alexander Aring90b15522014-04-24 19:09:12 +02001819static const struct spi_device_id at86rf230_device_id[] = {
1820 { .name = "at86rf230", },
1821 { .name = "at86rf231", },
1822 { .name = "at86rf233", },
1823 { .name = "at86rf212", },
1824 { },
1825};
1826MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1827
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001828static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001829 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001830 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001831 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001832 .name = "at86rf230",
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001833 },
1834 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001835 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001836};
1837
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001838module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001839
1840MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1841MODULE_LICENSE("GPL v2");