blob: 7d67f41387f5577959839f902128b1777b4b51ee [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00002/*
3 * AT86RF230/RF231 driver
4 *
5 * Copyright (C) 2009-2012 Siemens AG
6 *
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00007 * Written by:
8 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
Alexander Aring01ebd602014-07-03 00:20:55 +020010 * Alexander Aring <aar@pengutronix.de>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000011 */
12#include <linux/kernel.h>
13#include <linux/module.h>
Alexander Aringeb3b435e2015-03-09 13:56:10 +010014#include <linux/hrtimer.h>
Alexander Aringdce481e2015-03-09 13:56:11 +010015#include <linux/jiffies.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000016#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020017#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000018#include <linux/gpio.h>
19#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000020#include <linux/spi/spi.h>
21#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020022#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000023#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010024#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020025#include <linux/ieee802154.h>
Alexander Aring493bc902015-08-27 21:49:19 +020026#include <linux/debugfs.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000027
28#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020029#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000030
Alexander Aring7490b002015-05-17 21:44:57 +020031#include "at86rf230.h"
32
Alexander Aringa53d1f72014-07-03 00:20:46 +020033struct at86rf230_local;
34/* at86rf2xx chip depend data.
35 * All timings are in us.
36 */
37struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020038 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020039 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020040 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020041 u16 t_off_to_aack;
42 u16 t_off_to_tx_on;
Alexander Aringe6f7ed92015-06-13 22:15:54 +020043 u16 t_off_to_sleep;
44 u16 t_sleep_to_off;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020045 u16 t_frame;
46 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020047 int rssi_base_val;
48
Alexander Aringe37d2ec2014-10-28 18:21:19 +010049 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aring6f4da3f2015-05-17 21:44:49 +020050 int (*set_txpower)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020051};
52
Alexander Aringba6d2232015-03-01 21:55:28 +010053#define AT86RF2XX_MAX_BUF (127 + 3)
54/* tx retries to access the TX_ON state
55 * if it's above then force change will be started.
56 *
57 * We assume the max_frame_retries (7) value of 802.15.4 here.
58 */
59#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aringdce481e2015-03-09 13:56:11 +010060/* We use the recommended 5 minutes timeout to recalibrate */
61#define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
Alexander Aring1d15d6b2014-07-03 00:20:48 +020062
63struct at86rf230_state_change {
64 struct at86rf230_local *lp;
Alexander Aringcca990c2015-03-01 21:55:31 +010065 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020066
Alexander Aringeb3b435e2015-03-09 13:56:10 +010067 struct hrtimer timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020068 struct spi_message msg;
69 struct spi_transfer trx;
70 u8 buf[AT86RF2XX_MAX_BUF];
71
72 void (*complete)(void *context);
73 u8 from_state;
74 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020075
Alexander Aring57c1bc72015-09-21 09:37:54 +020076 bool free;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020077};
78
Alexander Aring493bc902015-08-27 21:49:19 +020079struct at86rf230_trac {
80 u64 success;
81 u64 success_data_pending;
82 u64 success_wait_for_ack;
83 u64 channel_access_failure;
84 u64 no_ack;
85 u64 invalid;
86};
87
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000088struct at86rf230_local {
89 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000090
Alexander Aring5a504392014-10-25 17:16:34 +020091 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020092 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020093 struct regmap *regmap;
Alexander Aringd2c8bf52015-04-30 17:45:03 +020094 int slp_tr;
Alexander Aringcbe62342015-06-16 11:07:42 +020095 bool sleep;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000096
Alexander Aring2e0571c2014-07-03 00:20:51 +020097 struct completion state_complete;
98 struct at86rf230_state_change state;
99
Alexander Aringdce481e2015-03-09 13:56:11 +0100100 unsigned long cal_timeout;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200101 bool is_tx;
Alexander Aring85009202015-04-30 17:45:02 +0200102 bool is_tx_from_off;
Alexander Aringba6d2232015-03-01 21:55:28 +0100103 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200104 struct sk_buff *tx_skb;
105 struct at86rf230_state_change tx;
Alexander Aring493bc902015-08-27 21:49:19 +0200106
107 struct at86rf230_trac trac;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000108};
109
Alexander Aringf76014f772014-07-03 00:20:44 +0200110#define AT86RF2XX_NUMREGS 0x3F
111
Alexander Aring97fed792014-10-07 10:38:32 +0200112static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200113at86rf230_async_state_change(struct at86rf230_local *lp,
114 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200115 const u8 state, void (*complete)(void *context));
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200116
Alexander Aringcbe62342015-06-16 11:07:42 +0200117static inline void
118at86rf230_sleep(struct at86rf230_local *lp)
119{
120 if (gpio_is_valid(lp->slp_tr)) {
121 gpio_set_value(lp->slp_tr, 1);
122 usleep_range(lp->data->t_off_to_sleep,
123 lp->data->t_off_to_sleep + 10);
124 lp->sleep = true;
125 }
126}
127
128static inline void
129at86rf230_awake(struct at86rf230_local *lp)
130{
131 if (gpio_is_valid(lp->slp_tr)) {
132 gpio_set_value(lp->slp_tr, 0);
133 usleep_range(lp->data->t_sleep_to_off,
134 lp->data->t_sleep_to_off + 100);
135 lp->sleep = false;
136 }
137}
138
Alexander Aringf76014f772014-07-03 00:20:44 +0200139static inline int
140__at86rf230_write(struct at86rf230_local *lp,
141 unsigned int addr, unsigned int data)
142{
Alexander Aringcbe62342015-06-16 11:07:42 +0200143 bool sleep = lp->sleep;
144 int ret;
145
146 /* awake for register setting if sleep */
147 if (sleep)
148 at86rf230_awake(lp);
149
150 ret = regmap_write(lp->regmap, addr, data);
151
152 /* sleep again if was sleeping */
153 if (sleep)
154 at86rf230_sleep(lp);
155
156 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200157}
158
159static inline int
160__at86rf230_read(struct at86rf230_local *lp,
161 unsigned int addr, unsigned int *data)
162{
Alexander Aringcbe62342015-06-16 11:07:42 +0200163 bool sleep = lp->sleep;
164 int ret;
165
166 /* awake for register setting if sleep */
167 if (sleep)
168 at86rf230_awake(lp);
169
170 ret = regmap_read(lp->regmap, addr, data);
171
172 /* sleep again if was sleeping */
173 if (sleep)
174 at86rf230_sleep(lp);
175
176 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200177}
178
179static inline int
180at86rf230_read_subreg(struct at86rf230_local *lp,
181 unsigned int addr, unsigned int mask,
182 unsigned int shift, unsigned int *data)
183{
184 int rc;
185
186 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100187 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200188 *data = (*data & mask) >> shift;
189
190 return rc;
191}
192
193static inline int
194at86rf230_write_subreg(struct at86rf230_local *lp,
195 unsigned int addr, unsigned int mask,
196 unsigned int shift, unsigned int data)
197{
Alexander Aringcbe62342015-06-16 11:07:42 +0200198 bool sleep = lp->sleep;
199 int ret;
200
201 /* awake for register setting if sleep */
202 if (sleep)
203 at86rf230_awake(lp);
204
205 ret = regmap_update_bits(lp->regmap, addr, mask, data << shift);
206
207 /* sleep again if was sleeping */
208 if (sleep)
209 at86rf230_sleep(lp);
210
211 return ret;
Alexander Aringf76014f772014-07-03 00:20:44 +0200212}
213
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200214static inline void
215at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
216{
217 gpio_set_value(lp->slp_tr, 1);
218 udelay(1);
219 gpio_set_value(lp->slp_tr, 0);
220}
221
Alexander Aringf76014f772014-07-03 00:20:44 +0200222static bool
223at86rf230_reg_writeable(struct device *dev, unsigned int reg)
224{
225 switch (reg) {
226 case RG_TRX_STATE:
227 case RG_TRX_CTRL_0:
228 case RG_TRX_CTRL_1:
229 case RG_PHY_TX_PWR:
230 case RG_PHY_ED_LEVEL:
231 case RG_PHY_CC_CCA:
232 case RG_CCA_THRES:
233 case RG_RX_CTRL:
234 case RG_SFD_VALUE:
235 case RG_TRX_CTRL_2:
236 case RG_ANT_DIV:
237 case RG_IRQ_MASK:
238 case RG_VREG_CTRL:
239 case RG_BATMON:
240 case RG_XOSC_CTRL:
241 case RG_RX_SYN:
242 case RG_XAH_CTRL_1:
243 case RG_FTN_CTRL:
244 case RG_PLL_CF:
245 case RG_PLL_DCU:
246 case RG_SHORT_ADDR_0:
247 case RG_SHORT_ADDR_1:
248 case RG_PAN_ID_0:
249 case RG_PAN_ID_1:
250 case RG_IEEE_ADDR_0:
251 case RG_IEEE_ADDR_1:
252 case RG_IEEE_ADDR_2:
253 case RG_IEEE_ADDR_3:
254 case RG_IEEE_ADDR_4:
255 case RG_IEEE_ADDR_5:
256 case RG_IEEE_ADDR_6:
257 case RG_IEEE_ADDR_7:
258 case RG_XAH_CTRL_0:
259 case RG_CSMA_SEED_0:
260 case RG_CSMA_SEED_1:
261 case RG_CSMA_BE:
262 return true;
263 default:
264 return false;
265 }
266}
267
268static bool
269at86rf230_reg_readable(struct device *dev, unsigned int reg)
270{
271 bool rc;
272
273 /* all writeable are also readable */
274 rc = at86rf230_reg_writeable(dev, reg);
275 if (rc)
276 return rc;
277
278 /* readonly regs */
279 switch (reg) {
280 case RG_TRX_STATUS:
281 case RG_PHY_RSSI:
282 case RG_IRQ_STATUS:
283 case RG_PART_NUM:
284 case RG_VERSION_NUM:
285 case RG_MAN_ID_1:
286 case RG_MAN_ID_0:
287 return true;
288 default:
289 return false;
290 }
291}
292
293static bool
294at86rf230_reg_volatile(struct device *dev, unsigned int reg)
295{
296 /* can be changed during runtime */
297 switch (reg) {
298 case RG_TRX_STATUS:
299 case RG_TRX_STATE:
300 case RG_PHY_RSSI:
301 case RG_PHY_ED_LEVEL:
302 case RG_IRQ_STATUS:
303 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100304 case RG_PLL_CF:
305 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200306 return true;
307 default:
308 return false;
309 }
310}
311
312static bool
313at86rf230_reg_precious(struct device *dev, unsigned int reg)
314{
315 /* don't clear irq line on read */
316 switch (reg) {
317 case RG_IRQ_STATUS:
318 return true;
319 default:
320 return false;
321 }
322}
323
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100324static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200325 .reg_bits = 8,
326 .val_bits = 8,
327 .write_flag_mask = CMD_REG | CMD_WRITE,
328 .read_flag_mask = CMD_REG,
329 .cache_type = REGCACHE_RBTREE,
330 .max_register = AT86RF2XX_NUMREGS,
331 .writeable_reg = at86rf230_reg_writeable,
332 .readable_reg = at86rf230_reg_readable,
333 .volatile_reg = at86rf230_reg_volatile,
334 .precious_reg = at86rf230_reg_precious,
335};
336
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200337static void
Alexander Aringc231c5a2016-02-19 09:59:12 +0100338at86rf230_async_error_recover_complete(void *context)
339{
340 struct at86rf230_state_change *ctx = context;
341 struct at86rf230_local *lp = ctx->lp;
342
343 if (ctx->free)
344 kfree(ctx);
345
346 ieee802154_wake_queue(lp->hw);
347}
348
349static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200350at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000351{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200352 struct at86rf230_state_change *ctx = context;
353 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000354
Alexander Aringa7a484b2015-03-26 12:46:30 +0100355 lp->is_tx = 0;
Alexander Aringc231c5a2016-02-19 09:59:12 +0100356 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
357 at86rf230_async_error_recover_complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200358}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000359
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100360static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200361at86rf230_async_error(struct at86rf230_local *lp,
362 struct at86rf230_state_change *ctx, int rc)
363{
364 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000365
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200366 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200367 at86rf230_async_error_recover);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200368}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000369
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200370/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200371static void
Alexander Aring57c1bc72015-09-21 09:37:54 +0200372at86rf230_async_read_reg(struct at86rf230_local *lp, u8 reg,
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200373 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200374 void (*complete)(void *context))
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200375{
Alexander Aring97fed792014-10-07 10:38:32 +0200376 int rc;
377
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200378 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000379
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200380 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200381 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200382 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200383 if (rc)
Alexander Aring97fed792014-10-07 10:38:32 +0200384 at86rf230_async_error(lp, ctx, rc);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200385}
386
387static void
388at86rf230_async_write_reg(struct at86rf230_local *lp, u8 reg, u8 val,
389 struct at86rf230_state_change *ctx,
390 void (*complete)(void *context))
391{
392 int rc;
393
394 ctx->buf[0] = (reg & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
395 ctx->buf[1] = val;
396 ctx->msg.complete = complete;
397 rc = spi_async(lp->spi, &ctx->msg);
398 if (rc)
399 at86rf230_async_error(lp, ctx, rc);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200400}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000401
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200402static void
403at86rf230_async_state_assert(void *context)
404{
405 struct at86rf230_state_change *ctx = context;
406 struct at86rf230_local *lp = ctx->lp;
407 const u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200408 const u8 trx_state = buf[1] & TRX_STATE_MASK;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000409
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200410 /* Assert state change */
411 if (trx_state != ctx->to_state) {
412 /* Special handling if transceiver state is in
413 * STATE_BUSY_RX_AACK and a SHR was detected.
414 */
415 if (trx_state == STATE_BUSY_RX_AACK) {
416 /* Undocumented race condition. If we send a state
417 * change to STATE_RX_AACK_ON the transceiver could
418 * change his state automatically to STATE_BUSY_RX_AACK
419 * if a SHR was detected. This is not an error, but we
420 * can't assert this.
421 */
422 if (ctx->to_state == STATE_RX_AACK_ON)
423 goto done;
424
425 /* If we change to STATE_TX_ON without forcing and
426 * transceiver state is STATE_BUSY_RX_AACK, we wait
427 * 'tFrame + tPAck' receiving time. In this time the
428 * PDU should be received. If the transceiver is still
429 * in STATE_BUSY_RX_AACK, we run a force state change
430 * to STATE_TX_ON. This is a timeout handling, if the
431 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100432 *
433 * Additional we do several retries to try to get into
434 * TX_ON state without forcing. If the retries are
435 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
436 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200437 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100438 if (ctx->to_state == STATE_TX_ON ||
439 ctx->to_state == STATE_TRX_OFF) {
440 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100441
442 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringed4a26b2015-08-27 21:49:18 +0200443 state = STATE_FORCE_TRX_OFF;
Alexander Aringba6d2232015-03-01 21:55:28 +0100444 lp->tx_retry++;
445
446 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200447 ctx->complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200448 return;
449 }
450 }
451
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200452 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
453 ctx->from_state, ctx->to_state, trx_state);
454 }
455
456done:
457 if (ctx->complete)
458 ctx->complete(context);
459}
460
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100461static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
462{
463 struct at86rf230_state_change *ctx =
464 container_of(timer, struct at86rf230_state_change, timer);
465 struct at86rf230_local *lp = ctx->lp;
466
467 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200468 at86rf230_async_state_assert);
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100469
470 return HRTIMER_NORESTART;
471}
472
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200473/* Do state change timing delay. */
474static void
475at86rf230_async_state_delay(void *context)
476{
477 struct at86rf230_state_change *ctx = context;
478 struct at86rf230_local *lp = ctx->lp;
479 struct at86rf2xx_chip_data *c = lp->data;
480 bool force = false;
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100481 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200482
483 /* The force state changes are will show as normal states in the
484 * state status subregister. We change the to_state to the
485 * corresponding one and remember if it was a force change, this
486 * differs if we do a state change from STATE_BUSY_RX_AACK.
487 */
488 switch (ctx->to_state) {
489 case STATE_FORCE_TX_ON:
490 ctx->to_state = STATE_TX_ON;
491 force = true;
492 break;
493 case STATE_FORCE_TRX_OFF:
494 ctx->to_state = STATE_TRX_OFF;
495 force = true;
496 break;
497 default:
498 break;
499 }
500
501 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200502 case STATE_TRX_OFF:
503 switch (ctx->to_state) {
504 case STATE_RX_AACK_ON:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100505 tim = c->t_off_to_aack * NSEC_PER_USEC;
Alexander Aring2ad33242015-04-30 17:44:59 +0200506 /* state change from TRX_OFF to RX_AACK_ON to do a
507 * calibration, we need to reset the timeout for the
508 * next one.
509 */
510 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200511 goto change;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200512 case STATE_TX_ARET_ON:
Alexander Aring2e0571c2014-07-03 00:20:51 +0200513 case STATE_TX_ON:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100514 tim = c->t_off_to_tx_on * NSEC_PER_USEC;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200515 /* state change from TRX_OFF to TX_ON or ARET_ON to do
516 * a calibration, we need to reset the timeout for the
Alexander Aringdce481e2015-03-09 13:56:11 +0100517 * next one.
518 */
519 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200520 goto change;
521 default:
522 break;
523 }
524 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200525 case STATE_BUSY_RX_AACK:
526 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100527 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200528 case STATE_TX_ON:
529 /* Wait for worst case receiving time if we
530 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100531 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200532 */
533 if (!force) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100534 tim = (c->t_frame + c->t_p_ack) * NSEC_PER_USEC;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200535 goto change;
536 }
537 break;
538 default:
539 break;
540 }
541 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200542 /* Default value, means RESET state */
543 case STATE_P_ON:
544 switch (ctx->to_state) {
545 case STATE_TRX_OFF:
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100546 tim = c->t_reset_to_off * NSEC_PER_USEC;
Alexander Aring09e536c2014-07-03 00:20:52 +0200547 goto change;
548 default:
549 break;
550 }
551 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200552 default:
553 break;
554 }
555
556 /* Default delay is 1us in the most cases */
Alexander Aring8b44f0d2015-07-30 12:13:29 +0200557 udelay(1);
558 at86rf230_async_state_timer(&ctx->timer);
559 return;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200560
561change:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100562 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200563}
564
565static void
566at86rf230_async_state_change_start(void *context)
567{
568 struct at86rf230_state_change *ctx = context;
569 struct at86rf230_local *lp = ctx->lp;
570 u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200571 const u8 trx_state = buf[1] & TRX_STATE_MASK;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200572
573 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
574 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
575 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200576 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200577 at86rf230_async_state_change_start);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200578 return;
579 }
580
581 /* Check if we already are in the state which we change in */
582 if (trx_state == ctx->to_state) {
583 if (ctx->complete)
584 ctx->complete(context);
585 return;
586 }
587
588 /* Set current state to the context of state change */
589 ctx->from_state = trx_state;
590
591 /* Going into the next step for a state change which do a timing
592 * relevant delay.
593 */
Alexander Aring57c1bc72015-09-21 09:37:54 +0200594 at86rf230_async_write_reg(lp, RG_TRX_STATE, ctx->to_state, ctx,
595 at86rf230_async_state_delay);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000596}
597
Alexander Aring97fed792014-10-07 10:38:32 +0200598static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200599at86rf230_async_state_change(struct at86rf230_local *lp,
600 struct at86rf230_state_change *ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200601 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000602{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200603 /* Initialization for the state change context */
604 ctx->to_state = state;
605 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200606 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200607 at86rf230_async_state_change_start);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200608}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000609
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200610static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200611at86rf230_sync_state_change_complete(void *context)
612{
613 struct at86rf230_state_change *ctx = context;
614 struct at86rf230_local *lp = ctx->lp;
615
616 complete(&lp->state_complete);
617}
618
619/* This function do a sync framework above the async state change.
620 * Some callbacks of the IEEE 802.15.4 driver interface need to be
621 * handled synchronously.
622 */
623static int
624at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
625{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100626 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200627
Alexander Aring97fed792014-10-07 10:38:32 +0200628 at86rf230_async_state_change(lp, &lp->state, state,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200629 at86rf230_sync_state_change_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200630
631 rc = wait_for_completion_timeout(&lp->state_complete,
632 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200633 if (!rc) {
634 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200635 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200636 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200637
638 return 0;
639}
640
641static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200642at86rf230_tx_complete(void *context)
643{
644 struct at86rf230_state_change *ctx = context;
645 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000646
Alexander Aringfc0719e2015-08-10 21:15:57 +0200647 ieee802154_xmit_complete(lp->hw, lp->tx_skb, false);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200648 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200649}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000650
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200651static void
652at86rf230_tx_on(void *context)
653{
654 struct at86rf230_state_change *ctx = context;
655 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000656
Alexander Aring31fa7432015-03-01 21:55:32 +0100657 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200658 at86rf230_tx_complete);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200659}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000660
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200661static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200662at86rf230_tx_trac_check(void *context)
663{
664 struct at86rf230_state_change *ctx = context;
665 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000666
Alexander Aring493bc902015-08-27 21:49:19 +0200667 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
668 u8 trac = TRAC_MASK(ctx->buf[1]);
669
670 switch (trac) {
671 case TRAC_SUCCESS:
672 lp->trac.success++;
673 break;
674 case TRAC_SUCCESS_DATA_PENDING:
675 lp->trac.success_data_pending++;
676 break;
677 case TRAC_CHANNEL_ACCESS_FAILURE:
678 lp->trac.channel_access_failure++;
679 break;
680 case TRAC_NO_ACK:
681 lp->trac.no_ack++;
682 break;
683 case TRAC_INVALID:
684 lp->trac.invalid++;
685 break;
686 default:
687 WARN_ONCE(1, "received tx trac status %d\n", trac);
688 break;
689 }
690 }
691
Alexander Aring57c1bc72015-09-21 09:37:54 +0200692 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, at86rf230_tx_on);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200693}
694
695static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100696at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200697{
Alexander Aring74de4c82015-03-01 21:55:30 +0100698 struct at86rf230_state_change *ctx = context;
699 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100700 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100701 struct sk_buff *skb;
702 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200703
Alexander Aring74de4c82015-03-01 21:55:30 +0100704 len = buf[1];
705 if (!ieee802154_is_valid_psdu_len(len)) {
706 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
707 len = IEEE802154_MTU;
708 }
709 lqi = buf[2 + len];
710
Alexander Aring61a22812014-10-27 17:13:29 +0100711 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200712 if (!skb) {
713 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
Alexander Aring57c1bc72015-09-21 09:37:54 +0200714 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200715 return;
716 }
717
Johannes Berg59ae1d12017-06-16 14:29:20 +0200718 skb_put_data(skb, buf + 2, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100719 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200720 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200721}
722
723static void
Alexander Aring493bc902015-08-27 21:49:19 +0200724at86rf230_rx_trac_check(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200725{
Alexander Aringcca990c2015-03-01 21:55:31 +0100726 struct at86rf230_state_change *ctx = context;
727 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100728 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200729 int rc;
730
Alexander Aring493bc902015-08-27 21:49:19 +0200731 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS)) {
732 u8 trac = TRAC_MASK(buf[1]);
733
734 switch (trac) {
735 case TRAC_SUCCESS:
736 lp->trac.success++;
737 break;
738 case TRAC_SUCCESS_WAIT_FOR_ACK:
739 lp->trac.success_wait_for_ack++;
740 break;
741 case TRAC_INVALID:
742 lp->trac.invalid++;
743 break;
744 default:
745 WARN_ONCE(1, "received rx trac status %d\n", trac);
746 break;
747 }
748 }
749
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200750 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100751 ctx->trx.len = AT86RF2XX_MAX_BUF;
752 ctx->msg.complete = at86rf230_rx_read_frame_complete;
753 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200754 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100755 ctx->trx.len = 2;
Alexander Aring31fa7432015-03-01 21:55:32 +0100756 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200757 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200758}
759
760static void
Alexander Aring57c1bc72015-09-21 09:37:54 +0200761at86rf230_irq_trx_end(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200762{
Alexander Aring57c1bc72015-09-21 09:37:54 +0200763 struct at86rf230_state_change *ctx = context;
764 struct at86rf230_local *lp = ctx->lp;
765
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200766 if (lp->is_tx) {
767 lp->is_tx = 0;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200768 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
769 at86rf230_tx_trac_check);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200770 } else {
Alexander Aring57c1bc72015-09-21 09:37:54 +0200771 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
772 at86rf230_rx_trac_check);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200773 }
774}
775
776static void
777at86rf230_irq_status(void *context)
778{
779 struct at86rf230_state_change *ctx = context;
780 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100781 const u8 *buf = ctx->buf;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200782 u8 irq = buf[1];
783
784 enable_irq(lp->spi->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200785
786 if (irq & IRQ_TRX_END) {
Alexander Aring57c1bc72015-09-21 09:37:54 +0200787 at86rf230_irq_trx_end(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200788 } else {
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200789 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
790 irq);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200791 kfree(ctx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200792 }
793}
794
Alexander Aring57c1bc72015-09-21 09:37:54 +0200795static void
796at86rf230_setup_spi_messages(struct at86rf230_local *lp,
797 struct at86rf230_state_change *state)
798{
799 state->lp = lp;
800 state->irq = lp->spi->irq;
801 spi_message_init(&state->msg);
802 state->msg.context = state;
803 state->trx.len = 2;
804 state->trx.tx_buf = state->buf;
805 state->trx.rx_buf = state->buf;
806 spi_message_add_tail(&state->trx, &state->msg);
807 hrtimer_init(&state->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
808 state->timer.function = at86rf230_async_state_timer;
809}
810
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200811static irqreturn_t at86rf230_isr(int irq, void *data)
812{
813 struct at86rf230_local *lp = data;
Alexander Aring57c1bc72015-09-21 09:37:54 +0200814 struct at86rf230_state_change *ctx;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200815 int rc;
816
Alexander Aring90566362014-10-07 10:38:29 +0200817 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200818
Alexander Aring57c1bc72015-09-21 09:37:54 +0200819 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
820 if (!ctx) {
821 enable_irq(irq);
822 return IRQ_NONE;
823 }
824
825 at86rf230_setup_spi_messages(lp, ctx);
826 /* tell on error handling to free ctx */
827 ctx->free = true;
828
829 ctx->buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200830 ctx->msg.complete = at86rf230_irq_status;
831 rc = spi_async(lp->spi, &ctx->msg);
832 if (rc) {
833 at86rf230_async_error(lp, ctx, rc);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200834 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200835 return IRQ_NONE;
836 }
837
838 return IRQ_HANDLED;
839}
840
841static void
842at86rf230_write_frame_complete(void *context)
843{
844 struct at86rf230_state_change *ctx = context;
845 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200846
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200847 ctx->trx.len = 2;
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200848
Alexander Aring57c1bc72015-09-21 09:37:54 +0200849 if (gpio_is_valid(lp->slp_tr))
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200850 at86rf230_slp_tr_rising_edge(lp);
Alexander Aring57c1bc72015-09-21 09:37:54 +0200851 else
852 at86rf230_async_write_reg(lp, RG_TRX_STATE, STATE_BUSY_TX, ctx,
853 NULL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200854}
855
856static void
857at86rf230_write_frame(void *context)
858{
859 struct at86rf230_state_change *ctx = context;
860 struct at86rf230_local *lp = ctx->lp;
861 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100862 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200863 int rc;
864
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200865 lp->is_tx = 1;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200866
867 buf[0] = CMD_FB | CMD_WRITE;
868 buf[1] = skb->len + 2;
869 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100870 ctx->trx.len = skb->len + 2;
871 ctx->msg.complete = at86rf230_write_frame_complete;
872 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100873 if (rc) {
874 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200875 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100876 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200877}
878
879static void
880at86rf230_xmit_tx_on(void *context)
881{
882 struct at86rf230_state_change *ctx = context;
883 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200884
Alexander Aring97fed792014-10-07 10:38:32 +0200885 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200886 at86rf230_write_frame);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200887}
888
Alexander Aringdce481e2015-03-09 13:56:11 +0100889static void
890at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200891{
Alexander Aringdce481e2015-03-09 13:56:11 +0100892 struct at86rf230_state_change *ctx = context;
893 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200894
Alexander Aringfc0719e2015-08-10 21:15:57 +0200895 /* check if we change from off state */
Alexander Aringd981b5b2016-02-19 09:59:13 +0100896 if (lp->is_tx_from_off)
Alexander Aringfc0719e2015-08-10 21:15:57 +0200897 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200898 at86rf230_write_frame);
Alexander Aringd981b5b2016-02-19 09:59:13 +0100899 else
Alexander Aringdce481e2015-03-09 13:56:11 +0100900 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200901 at86rf230_xmit_tx_on);
Alexander Aringdce481e2015-03-09 13:56:11 +0100902}
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200903
Alexander Aringdce481e2015-03-09 13:56:11 +0100904static int
905at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
906{
907 struct at86rf230_local *lp = hw->priv;
908 struct at86rf230_state_change *ctx = &lp->tx;
909
910 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +0100911 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +0100912
913 /* After 5 minutes in PLL and the same frequency we run again the
914 * calibration loops which is recommended by at86rf2xx datasheets.
915 *
916 * The calibration is initiate by a state change from TRX_OFF
917 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
918 * function then to start in the next 5 minutes.
919 */
Alexander Aring85009202015-04-30 17:45:02 +0200920 if (time_is_before_jiffies(lp->cal_timeout)) {
921 lp->is_tx_from_off = true;
Alexander Aringdce481e2015-03-09 13:56:11 +0100922 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
Alexander Aring57c1bc72015-09-21 09:37:54 +0200923 at86rf230_xmit_start);
Alexander Aring85009202015-04-30 17:45:02 +0200924 } else {
Alexander Aringd981b5b2016-02-19 09:59:13 +0100925 lp->is_tx_from_off = false;
Alexander Aringdce481e2015-03-09 13:56:11 +0100926 at86rf230_xmit_start(ctx);
Alexander Aring85009202015-04-30 17:45:02 +0200927 }
Alexander Aring97fed792014-10-07 10:38:32 +0200928
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200929 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000930}
931
932static int
Alexander Aring5a504392014-10-25 17:16:34 +0200933at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000934{
Stefan Schmidt20f33042017-09-22 14:13:53 +0200935 WARN_ON(!level);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000936 *level = 0xbe;
937 return 0;
938}
939
940static int
Alexander Aring5a504392014-10-25 17:16:34 +0200941at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000942{
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200943 struct at86rf230_local *lp = hw->priv;
944
Alexander Aring493bc902015-08-27 21:49:19 +0200945 /* reset trac stats on start */
946 if (IS_ENABLED(CONFIG_IEEE802154_AT86RF230_DEBUGFS))
947 memset(&lp->trac, 0, sizeof(struct at86rf230_trac));
948
Alexander Aringcbe62342015-06-16 11:07:42 +0200949 at86rf230_awake(lp);
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200950 enable_irq(lp->spi->irq);
951
Alexander Aring30811fa2015-06-16 11:07:44 +0200952 return at86rf230_sync_state_change(lp, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000953}
954
955static void
Alexander Aring5a504392014-10-25 17:16:34 +0200956at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000957{
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200958 struct at86rf230_local *lp = hw->priv;
Alexander Aring74ed9d92015-06-16 11:07:43 +0200959 u8 csma_seed[2];
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200960
Alexander Aring30811fa2015-06-16 11:07:44 +0200961 at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Alexander Aringe6f7ed92015-06-13 22:15:54 +0200962
963 disable_irq(lp->spi->irq);
Alexander Aring74ed9d92015-06-16 11:07:43 +0200964
965 /* It's recommended to set random new csma_seeds before sleep state.
966 * Makes only sense in the stop callback, not doing this inside of
967 * at86rf230_sleep, this is also used when we don't transmit afterwards
968 * when calling start callback again.
969 */
970 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
971 at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
972 at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
973
Alexander Aringcbe62342015-06-16 11:07:42 +0200974 at86rf230_sleep(lp);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000975}
976
977static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100978at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100979{
980 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
981}
982
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200983#define AT86RF2XX_MAX_ED_LEVELS 0xF
Alexander Aring4d7101d2016-09-06 09:50:04 +0200984static const s32 at86rf233_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
985 -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, -7800, -7600,
986 -7400, -7200, -7000, -6800, -6600, -6400,
987};
988
989static const s32 at86rf231_ed_levels[AT86RF2XX_MAX_ED_LEVELS + 1] = {
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200990 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
991 -7100, -6900, -6700, -6500, -6300, -6100,
992};
993
994static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
995 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
Alexander Aring179655f2015-05-20 09:27:03 +0200996 -8000, -7800, -7600, -7400, -7200, -7000,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +0200997};
998
999static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
1000 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
Alexander Aring179655f2015-05-20 09:27:03 +02001001 -7800, -7600, -7400, -7200, -7000, -6800,
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001002};
1003
1004static inline int
1005at86rf212_update_cca_ed_level(struct at86rf230_local *lp, int rssi_base_val)
1006{
1007 unsigned int cca_ed_thres;
1008 int rc;
1009
1010 rc = at86rf230_read_subreg(lp, SR_CCA_ED_THRES, &cca_ed_thres);
1011 if (rc < 0)
1012 return rc;
1013
1014 switch (rssi_base_val) {
1015 case -98:
1016 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
1017 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
1018 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
1019 break;
1020 case -100:
1021 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1022 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
1023 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
1024 break;
1025 default:
1026 WARN_ON(1);
1027 }
1028
1029 return 0;
1030}
1031
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001032static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001033at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001034{
1035 int rc;
1036
1037 if (channel == 0)
1038 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1039 else
1040 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1041 if (rc < 0)
1042 return rc;
1043
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001044 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001045 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001046 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001047 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001048 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001049 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001050 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001051 if (rc < 0)
1052 return rc;
1053
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001054 rc = at86rf212_update_cca_ed_level(lp, lp->data->rssi_base_val);
1055 if (rc < 0)
1056 return rc;
1057
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001058 /* This sets the symbol_duration according frequency on the 212.
1059 * TODO move this handling while set channel and page in cfg802154.
1060 * We can do that, this timings are according 802.15.4 standard.
1061 * If we do that in cfg802154, this is a more generic calculation.
1062 *
1063 * This should also protected from ifs_timer. Means cancel timer and
1064 * init with a new value. For now, this is okay.
1065 */
1066 if (channel == 0) {
1067 if (page == 0) {
1068 /* SUB:0 and BPSK:0 -> BPSK-20 */
1069 lp->hw->phy->symbol_duration = 50;
1070 } else {
1071 /* SUB:1 and BPSK:0 -> BPSK-40 */
1072 lp->hw->phy->symbol_duration = 25;
1073 }
1074 } else {
1075 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001076 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001077 lp->hw->phy->symbol_duration = 40;
1078 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001079 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001080 lp->hw->phy->symbol_duration = 16;
1081 }
1082
1083 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1084 lp->hw->phy->symbol_duration;
1085 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1086 lp->hw->phy->symbol_duration;
1087
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001088 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1089}
1090
1091static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001092at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001093{
Alexander Aring5a504392014-10-25 17:16:34 +02001094 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001095 int rc;
1096
Alexander Aringa53d1f72014-07-03 00:20:46 +02001097 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001098 /* Wait for PLL */
1099 usleep_range(lp->data->t_channel_switch,
1100 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001101
1102 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001103 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001104}
1105
1106static int
Alexander Aring5a504392014-10-25 17:16:34 +02001107at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001108 struct ieee802154_hw_addr_filt *filt,
1109 unsigned long changed)
1110{
Alexander Aring5a504392014-10-25 17:16:34 +02001111 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001112
Alexander Aring57205c12014-10-25 05:25:09 +02001113 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001114 u16 addr = le16_to_cpu(filt->short_addr);
1115
Stefan Schmidt8a813882017-09-22 14:13:54 +02001116 dev_vdbg(&lp->spi->dev, "%s called for saddr\n", __func__);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001117 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1118 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001119 }
1120
Alexander Aring57205c12014-10-25 05:25:09 +02001121 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001122 u16 pan = le16_to_cpu(filt->pan_id);
1123
Stefan Schmidt8a813882017-09-22 14:13:54 +02001124 dev_vdbg(&lp->spi->dev, "%s called for pan id\n", __func__);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001125 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1126 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001127 }
1128
Alexander Aring57205c12014-10-25 05:25:09 +02001129 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001130 u8 i, addr[8];
1131
1132 memcpy(addr, &filt->ieee_addr, 8);
Stefan Schmidt8a813882017-09-22 14:13:54 +02001133 dev_vdbg(&lp->spi->dev, "%s called for IEEE addr\n", __func__);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001134 for (i = 0; i < 8; i++)
1135 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001136 }
1137
Alexander Aring57205c12014-10-25 05:25:09 +02001138 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
Stefan Schmidt8a813882017-09-22 14:13:54 +02001139 dev_vdbg(&lp->spi->dev, "%s called for panc change\n", __func__);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001140 if (filt->pan_coord)
1141 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1142 else
1143 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1144 }
1145
1146 return 0;
1147}
1148
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001149#define AT86RF23X_MAX_TX_POWERS 0xF
1150static const s32 at86rf233_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1151 400, 370, 340, 300, 250, 200, 100, 0, -100, -200, -300, -400, -600,
1152 -800, -1200, -1700,
1153};
1154
1155static const s32 at86rf231_powers[AT86RF23X_MAX_TX_POWERS + 1] = {
1156 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
1157 -900, -1200, -1700,
1158};
1159
1160#define AT86RF212_MAX_TX_POWERS 0x1F
1161static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
1162 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
1163 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
1164 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
1165};
1166
1167static int
1168at86rf23x_set_txpower(struct at86rf230_local *lp, s32 mbm)
1169{
1170 u32 i;
1171
1172 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1173 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1174 return at86rf230_write_subreg(lp, SR_TX_PWR_23X, i);
1175 }
1176
1177 return -EINVAL;
1178}
1179
1180static int
1181at86rf212_set_txpower(struct at86rf230_local *lp, s32 mbm)
1182{
1183 u32 i;
1184
1185 for (i = 0; i < lp->hw->phy->supported.tx_powers_size; i++) {
1186 if (lp->hw->phy->supported.tx_powers[i] == mbm)
1187 return at86rf230_write_subreg(lp, SR_TX_PWR_212, i);
1188 }
1189
1190 return -EINVAL;
1191}
1192
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001193static int
Alexander Aringe2eb1732015-05-17 21:44:40 +02001194at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001195{
Alexander Aring5a504392014-10-25 17:16:34 +02001196 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001197
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001198 return lp->data->set_txpower(lp, mbm);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001199}
1200
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001201static int
Alexander Aring5a504392014-10-25 17:16:34 +02001202at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001203{
Alexander Aring5a504392014-10-25 17:16:34 +02001204 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001205
1206 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1207}
1208
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001209static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001210at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1211 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001212{
Alexander Aring5a504392014-10-25 17:16:34 +02001213 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001214 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001215
Alexander Aring7fe9a382014-12-10 15:33:12 +01001216 /* mapping 802.15.4 to driver spec */
1217 switch (cca->mode) {
1218 case NL802154_CCA_ENERGY:
1219 val = 1;
1220 break;
1221 case NL802154_CCA_CARRIER:
1222 val = 2;
1223 break;
1224 case NL802154_CCA_ENERGY_CARRIER:
1225 switch (cca->opt) {
1226 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1227 val = 3;
1228 break;
1229 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1230 val = 0;
1231 break;
1232 default:
1233 return -EINVAL;
1234 }
1235 break;
1236 default:
1237 return -EINVAL;
1238 }
1239
1240 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001241}
1242
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001243static int
Alexander Aring32b23552015-05-17 21:44:41 +02001244at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001245{
Alexander Aring5a504392014-10-25 17:16:34 +02001246 struct at86rf230_local *lp = hw->priv;
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001247 u32 i;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001248
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001249 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
1250 if (hw->phy->supported.cca_ed_levels[i] == mbm)
1251 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, i);
1252 }
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001253
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001254 return -EINVAL;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001255}
1256
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001257static int
Alexander Aring5a504392014-10-25 17:16:34 +02001258at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001259 u8 retries)
1260{
Alexander Aring5a504392014-10-25 17:16:34 +02001261 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001262 int rc;
1263
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001264 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1265 if (rc)
1266 return rc;
1267
1268 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1269 if (rc)
1270 return rc;
1271
Alexander Aring39d7f322014-04-05 13:49:26 +02001272 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001273}
1274
1275static int
Alexander Aring5a504392014-10-25 17:16:34 +02001276at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001277{
Alexander Aring5a504392014-10-25 17:16:34 +02001278 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001279
Alexander Aringfc0719e2015-08-10 21:15:57 +02001280 return at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001281}
1282
Alexander Aring92f45f542014-10-29 21:34:33 +01001283static int
1284at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1285{
1286 struct at86rf230_local *lp = hw->priv;
1287 int rc;
1288
1289 if (on) {
1290 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1291 if (rc < 0)
1292 return rc;
1293
1294 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1295 if (rc < 0)
1296 return rc;
1297 } else {
1298 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1299 if (rc < 0)
1300 return rc;
1301
1302 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1303 if (rc < 0)
1304 return rc;
1305 }
1306
1307 return 0;
1308}
1309
Alexander Aring16301862014-10-28 18:21:18 +01001310static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001311 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001312 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001313 .ed = at86rf230_ed,
1314 .set_channel = at86rf230_channel,
1315 .start = at86rf230_start,
1316 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001317 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001318 .set_txpower = at86rf230_set_txpower,
1319 .set_lbt = at86rf230_set_lbt,
1320 .set_cca_mode = at86rf230_set_cca_mode,
1321 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1322 .set_csma_params = at86rf230_set_csma_params,
1323 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f542014-10-29 21:34:33 +01001324 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001325};
1326
Alexander Aringa53d1f72014-07-03 00:20:46 +02001327static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001328 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001329 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001330 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001331 .t_off_to_aack = 80,
1332 .t_off_to_tx_on = 80,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001333 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001334 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001335 .t_frame = 4096,
1336 .t_p_ack = 545,
Alexander Aring4d7101d2016-09-06 09:50:04 +02001337 .rssi_base_val = -94,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001338 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001339 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001340};
1341
1342static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001343 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001344 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001345 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001346 .t_off_to_aack = 110,
1347 .t_off_to_tx_on = 110,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001348 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001349 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001350 .t_frame = 4096,
1351 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001352 .rssi_base_val = -91,
1353 .set_channel = at86rf23x_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001354 .set_txpower = at86rf23x_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001355};
1356
1357static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001358 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001359 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001360 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001361 .t_off_to_aack = 200,
1362 .t_off_to_tx_on = 200,
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001363 .t_off_to_sleep = 35,
Alexander Aring60f5f5d2016-04-19 15:34:22 +02001364 .t_sleep_to_off = 1000,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001365 .t_frame = 4096,
1366 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001367 .rssi_base_val = -100,
1368 .set_channel = at86rf212_set_channel,
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001369 .set_txpower = at86rf212_set_txpower,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001370};
1371
Alexander Aringccdaeb22015-02-27 09:58:26 +01001372static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001373{
Alexander Aring1db05582014-07-03 00:20:50 +02001374 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001375 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001376 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001377
Alexander Aring09e536c2014-07-03 00:20:52 +02001378 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001379 if (rc)
1380 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001381
Alexander Aring4af619a2014-04-24 19:09:05 +02001382 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring702d2112015-02-27 09:58:29 +01001383 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1384 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001385 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001386
Alexander Aring18c65042014-04-24 19:09:18 +02001387 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001388 if (rc)
1389 return rc;
1390
Alexander Aring6bd2b132014-07-03 00:20:49 +02001391 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1392 if (rc)
1393 return rc;
1394
Sascha Herrmann057dad62013-04-14 22:33:29 +00001395 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001396 if (rc)
1397 return rc;
1398
Alexander Aringbe64f072015-02-27 09:58:28 +01001399 /* reset values differs in at86rf231 and at86rf233 */
1400 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1401 if (rc)
1402 return rc;
1403
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001404 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1405 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1406 if (rc)
1407 return rc;
1408 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1409 if (rc)
1410 return rc;
1411
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001412 /* CLKM changes are applied immediately */
1413 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1414 if (rc)
1415 return rc;
1416
1417 /* Turn CLKM Off */
1418 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1419 if (rc)
1420 return rc;
1421 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001422 usleep_range(lp->data->t_sleep_cycle,
1423 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001424
Alexander Aringccdaeb22015-02-27 09:58:26 +01001425 /* xtal_trim value is calculated by:
1426 * CL = 0.5 * (CX + CTRIM + CPAR)
1427 *
1428 * whereas:
1429 * CL = capacitor of used crystal
1430 * CX = connected capacitors at xtal pins
1431 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1432 * but this is different on each board setup. You need to fine
1433 * tuning this value via CTRIM.
1434 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1435 * 0 pF upto 4.5 pF.
1436 *
1437 * Examples:
1438 * atben transceiver:
1439 *
1440 * CL = 8 pF
1441 * CX = 12 pF
1442 * CPAR = 3 pF (We assume the magic constant from datasheet)
1443 * CTRIM = 0.9 pF
1444 *
1445 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1446 *
1447 * xtal_trim = 0x3
1448 *
1449 * openlabs transceiver:
1450 *
1451 * CL = 16 pF
1452 * CX = 22 pF
1453 * CPAR = 3 pF (We assume the magic constant from datasheet)
1454 * CTRIM = 4.5 pF
1455 *
1456 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1457 *
1458 * xtal_trim = 0xf
1459 */
1460 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1461 if (rc)
1462 return rc;
1463
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001464 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001465 if (rc)
1466 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001467 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001468 dev_err(&lp->spi->dev, "DVDD error\n");
1469 return -EINVAL;
1470 }
1471
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001472 /* Force setting slotted operation bit to 0. Sometimes the atben
1473 * sets this bit and I don't know why. We set this always force
1474 * to zero while probing.
1475 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001476 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001477}
1478
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001479static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001480at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1481 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001482{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001483 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001484 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001485
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001486 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1487 if (!pdata)
1488 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001489
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001490 *rstn = pdata->rstn;
1491 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001492 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001493 return 0;
1494 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001495
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001496 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1497 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001498 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1499 if (ret < 0 && ret != -EINVAL)
1500 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001501
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001502 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001503}
1504
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001505static int
1506at86rf230_detect_device(struct at86rf230_local *lp)
1507{
1508 unsigned int part, version, val;
1509 u16 man_id = 0;
1510 const char *chip;
1511 int rc;
1512
1513 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1514 if (rc)
1515 return rc;
1516 man_id |= val;
1517
1518 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1519 if (rc)
1520 return rc;
1521 man_id |= (val << 8);
1522
1523 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1524 if (rc)
1525 return rc;
1526
Andrey Yurovsky75989682014-12-17 13:14:42 -08001527 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001528 if (rc)
1529 return rc;
1530
1531 if (man_id != 0x001f) {
1532 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1533 man_id >> 8, man_id & 0xFF);
1534 return -EINVAL;
1535 }
1536
Alexander Aringf265be32015-06-06 17:30:48 +02001537 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM |
Alexander Aringedea8f72015-05-17 21:44:46 +02001538 IEEE802154_HW_CSMA_PARAMS |
1539 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1540 IEEE802154_HW_PROMISCUOUS;
1541
1542 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1543 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1544 WPAN_PHY_FLAG_CCA_MODE;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001545
Alexander Aring8377d222015-05-17 21:44:48 +02001546 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1547 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1548 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1549 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1550
Alexander Aringb48a7c12014-12-10 15:33:14 +01001551 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1552
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001553 switch (part) {
1554 case 2:
1555 chip = "at86rf230";
1556 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001557 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001558 case 3:
1559 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001560 lp->data = &at86rf231_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001561 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001562 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001563 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001564 lp->hw->phy->supported.tx_powers = at86rf231_powers;
1565 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf231_powers);
Alexander Aring4d7101d2016-09-06 09:50:04 +02001566 lp->hw->phy->supported.cca_ed_levels = at86rf231_ed_levels;
1567 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf231_ed_levels);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001568 break;
1569 case 7:
1570 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001571 lp->data = &at86rf212_data;
1572 lp->hw->flags |= IEEE802154_HW_LBT;
Alexander Aring72f655e2015-05-17 21:44:42 +02001573 lp->hw->phy->supported.channels[0] = 0x00007FF;
1574 lp->hw->phy->supported.channels[2] = 0x00007FF;
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001575 lp->hw->phy->current_channel = 5;
1576 lp->hw->phy->symbol_duration = 25;
Alexander Aring8377d222015-05-17 21:44:48 +02001577 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001578 lp->hw->phy->supported.tx_powers = at86rf212_powers;
1579 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
Alexander Aring2d9fe7a2015-05-17 21:44:50 +02001580 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
1581 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001582 break;
1583 case 11:
1584 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001585 lp->data = &at86rf233_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001586 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001587 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001588 lp->hw->phy->symbol_duration = 16;
Alexander Aring6f4da3f2015-05-17 21:44:49 +02001589 lp->hw->phy->supported.tx_powers = at86rf233_powers;
1590 lp->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf233_powers);
Alexander Aring4d7101d2016-09-06 09:50:04 +02001591 lp->hw->phy->supported.cca_ed_levels = at86rf233_ed_levels;
1592 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf233_ed_levels);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001593 break;
1594 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001595 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001596 rc = -ENOTSUPP;
Alexander Aringcc643492015-05-17 21:44:51 +02001597 goto not_supp;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001598 }
1599
Alexander Aringcc643492015-05-17 21:44:51 +02001600 lp->hw->phy->cca_ed_level = lp->hw->phy->supported.cca_ed_levels[7];
Alexander Aring392f4e62015-05-17 21:44:52 +02001601 lp->hw->phy->transmit_power = lp->hw->phy->supported.tx_powers[0];
Alexander Aringcc643492015-05-17 21:44:51 +02001602
1603not_supp:
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001604 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1605
1606 return rc;
1607}
1608
Alexander Aring493bc902015-08-27 21:49:19 +02001609#ifdef CONFIG_IEEE802154_AT86RF230_DEBUGFS
1610static struct dentry *at86rf230_debugfs_root;
1611
1612static int at86rf230_stats_show(struct seq_file *file, void *offset)
1613{
1614 struct at86rf230_local *lp = file->private;
Alexander Aring493bc902015-08-27 21:49:19 +02001615
Stephen Rothwell97170ea2015-09-21 20:41:44 -07001616 seq_printf(file, "SUCCESS:\t\t%8llu\n", lp->trac.success);
1617 seq_printf(file, "SUCCESS_DATA_PENDING:\t%8llu\n",
1618 lp->trac.success_data_pending);
1619 seq_printf(file, "SUCCESS_WAIT_FOR_ACK:\t%8llu\n",
1620 lp->trac.success_wait_for_ack);
1621 seq_printf(file, "CHANNEL_ACCESS_FAILURE:\t%8llu\n",
1622 lp->trac.channel_access_failure);
1623 seq_printf(file, "NO_ACK:\t\t\t%8llu\n", lp->trac.no_ack);
1624 seq_printf(file, "INVALID:\t\t%8llu\n", lp->trac.invalid);
1625 return 0;
Alexander Aring493bc902015-08-27 21:49:19 +02001626}
Yangtao Lif578e672018-12-10 10:46:03 -05001627DEFINE_SHOW_ATTRIBUTE(at86rf230_stats);
Alexander Aring493bc902015-08-27 21:49:19 +02001628
Greg Kroah-Hartman7e174a42019-08-10 12:17:32 +02001629static void at86rf230_debugfs_init(struct at86rf230_local *lp)
Alexander Aring493bc902015-08-27 21:49:19 +02001630{
1631 char debugfs_dir_name[DNAME_INLINE_LEN + 1] = "at86rf230-";
Alexander Aring493bc902015-08-27 21:49:19 +02001632
1633 strncat(debugfs_dir_name, dev_name(&lp->spi->dev), DNAME_INLINE_LEN);
1634
1635 at86rf230_debugfs_root = debugfs_create_dir(debugfs_dir_name, NULL);
Alexander Aring493bc902015-08-27 21:49:19 +02001636
Greg Kroah-Hartman7e174a42019-08-10 12:17:32 +02001637 debugfs_create_file("trac_stats", 0444, at86rf230_debugfs_root, lp,
1638 &at86rf230_stats_fops);
Alexander Aring493bc902015-08-27 21:49:19 +02001639}
1640
1641static void at86rf230_debugfs_remove(void)
1642{
1643 debugfs_remove_recursive(at86rf230_debugfs_root);
1644}
1645#else
Greg Kroah-Hartman7e174a42019-08-10 12:17:32 +02001646static void at86rf230_debugfs_init(struct at86rf230_local *lp) { }
Alexander Aring493bc902015-08-27 21:49:19 +02001647static void at86rf230_debugfs_remove(void) { }
1648#endif
1649
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001650static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001651{
Alexander Aring5a504392014-10-25 17:16:34 +02001652 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001653 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001654 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001655 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001656 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001657
1658 if (!spi->irq) {
1659 dev_err(&spi->dev, "no IRQ specified\n");
1660 return -EINVAL;
1661 }
1662
Alexander Aringccdaeb22015-02-27 09:58:26 +01001663 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001664 if (rc < 0) {
1665 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1666 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001667 }
1668
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001669 if (gpio_is_valid(rstn)) {
1670 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001671 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001672 if (rc)
1673 return rc;
1674 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001675
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001676 if (gpio_is_valid(slp_tr)) {
1677 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001678 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001679 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001680 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001681 }
1682
1683 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001684 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001685 udelay(1);
Andrey Smirnovf3016062016-12-18 15:25:33 -08001686 gpio_set_value_cansleep(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001687 udelay(1);
Andrey Smirnovf3016062016-12-18 15:25:33 -08001688 gpio_set_value_cansleep(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001689 usleep_range(120, 240);
1690 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001691
Alexander Aring5a504392014-10-25 17:16:34 +02001692 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1693 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001694 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001695
Alexander Aring5a504392014-10-25 17:16:34 +02001696 lp = hw->priv;
1697 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001698 lp->spi = spi;
Alexander Aringd2c8bf52015-04-30 17:45:03 +02001699 lp->slp_tr = slp_tr;
Alexander Aring5a504392014-10-25 17:16:34 +02001700 hw->parent = &spi->dev;
Alexander Aringf6f4e862014-11-05 20:51:26 +01001701 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001702
Alexander Aringf76014f772014-07-03 00:20:44 +02001703 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1704 if (IS_ERR(lp->regmap)) {
1705 rc = PTR_ERR(lp->regmap);
1706 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1707 rc);
1708 goto free_dev;
1709 }
1710
Alexander Aring57c1bc72015-09-21 09:37:54 +02001711 at86rf230_setup_spi_messages(lp, &lp->state);
1712 at86rf230_setup_spi_messages(lp, &lp->tx);
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001713
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001714 rc = at86rf230_detect_device(lp);
1715 if (rc < 0)
1716 goto free_dev;
1717
Alexander Aring2e0571c2014-07-03 00:20:51 +02001718 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001719
1720 spi_set_drvdata(spi, lp);
1721
Alexander Aringccdaeb22015-02-27 09:58:26 +01001722 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001723 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001724 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001725
Alexander Aring19626942014-04-24 19:09:15 +02001726 /* Read irq status register to reset irq line */
1727 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001728 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001729 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001730
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001731 irq_type = irq_get_trigger_type(spi->irq);
1732 if (!irq_type)
Alexander Aring9ff19e62015-06-13 22:15:53 +02001733 irq_type = IRQF_TRIGGER_HIGH;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001734
1735 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1736 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001737 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001738 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001739
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001740 /* disable_irq by default and wait for starting hardware */
1741 disable_irq(spi->irq);
1742
1743 /* going into sleep by default */
Alexander Aringcbe62342015-06-16 11:07:42 +02001744 at86rf230_sleep(lp);
Alexander Aringe6f7ed92015-06-13 22:15:54 +02001745
Greg Kroah-Hartman7e174a42019-08-10 12:17:32 +02001746 at86rf230_debugfs_init(lp);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001747
Alexander Aring493bc902015-08-27 21:49:19 +02001748 rc = ieee802154_register_hw(lp->hw);
1749 if (rc)
1750 goto free_debugfs;
1751
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001752 return rc;
1753
Alexander Aring493bc902015-08-27 21:49:19 +02001754free_debugfs:
1755 at86rf230_debugfs_remove();
Alexander Aring640985e2014-07-03 00:20:43 +02001756free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001757 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001758
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001759 return rc;
1760}
1761
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001762static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001763{
1764 struct at86rf230_local *lp = spi_get_drvdata(spi);
1765
Alexander Aring17e84a92014-03-31 03:26:51 +02001766 /* mask all at86rf230 irq's */
1767 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001768 ieee802154_unregister_hw(lp->hw);
1769 ieee802154_free_hw(lp->hw);
Alexander Aring493bc902015-08-27 21:49:19 +02001770 at86rf230_debugfs_remove();
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001771 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001772
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001773 return 0;
1774}
1775
Alexander Aring1086b4f2014-04-24 19:09:11 +02001776static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001777 { .compatible = "atmel,at86rf230", },
1778 { .compatible = "atmel,at86rf231", },
1779 { .compatible = "atmel,at86rf233", },
1780 { .compatible = "atmel,at86rf212", },
1781 { },
1782};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001783MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001784
Alexander Aring90b15522014-04-24 19:09:12 +02001785static const struct spi_device_id at86rf230_device_id[] = {
1786 { .name = "at86rf230", },
1787 { .name = "at86rf231", },
1788 { .name = "at86rf233", },
1789 { .name = "at86rf212", },
1790 { },
1791};
1792MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1793
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001794static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001795 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001796 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001797 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001798 .name = "at86rf230",
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001799 },
1800 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001801 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001802};
1803
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001804module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001805
1806MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1807MODULE_LICENSE("GPL v2");