blob: 503fabddd431ef51895a0ed4af0e587ae7379ba3 [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>
22#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020023#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000024#include <linux/gpio.h>
25#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000026#include <linux/spinlock.h>
27#include <linux/spi/spi.h>
28#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020029#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000030#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010031#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020032#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000033
34#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020035#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000036
Alexander Aringa53d1f72014-07-03 00:20:46 +020037struct at86rf230_local;
38/* at86rf2xx chip depend data.
39 * All timings are in us.
40 */
41struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020042 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020043 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020044 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020045 u16 t_off_to_aack;
46 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020047 u16 t_frame;
48 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020049 int rssi_base_val;
50
Alexander Aringe37d2ec2014-10-28 18:21:19 +010051 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020052 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020053};
54
Alexander Aringba6d2232015-03-01 21:55:28 +010055#define AT86RF2XX_MAX_BUF (127 + 3)
56/* tx retries to access the TX_ON state
57 * if it's above then force change will be started.
58 *
59 * We assume the max_frame_retries (7) value of 802.15.4 here.
60 */
61#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aring1d15d6b2014-07-03 00:20:48 +020062
63struct at86rf230_state_change {
64 struct at86rf230_local *lp;
65
66 struct spi_message msg;
67 struct spi_transfer trx;
68 u8 buf[AT86RF2XX_MAX_BUF];
69
70 void (*complete)(void *context);
71 u8 from_state;
72 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020073
74 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020075};
76
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000077struct at86rf230_local {
78 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
Alexander Aring5a504392014-10-25 17:16:34 +020080 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020081 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020082 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000083
Alexander Aring2e0571c2014-07-03 00:20:51 +020084 struct completion state_complete;
85 struct at86rf230_state_change state;
86
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020088
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010089 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020090 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020091 bool is_tx;
92 /* spinlock for is_tx protection */
93 spinlock_t lock;
Alexander Aringba6d2232015-03-01 21:55:28 +010094 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020095 struct sk_buff *tx_skb;
96 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000097};
98
99#define RG_TRX_STATUS (0x01)
100#define SR_TRX_STATUS 0x01, 0x1f, 0
101#define SR_RESERVED_01_3 0x01, 0x20, 5
102#define SR_CCA_STATUS 0x01, 0x40, 6
103#define SR_CCA_DONE 0x01, 0x80, 7
104#define RG_TRX_STATE (0x02)
105#define SR_TRX_CMD 0x02, 0x1f, 0
106#define SR_TRAC_STATUS 0x02, 0xe0, 5
107#define RG_TRX_CTRL_0 (0x03)
108#define SR_CLKM_CTRL 0x03, 0x07, 0
109#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
110#define SR_PAD_IO_CLKM 0x03, 0x30, 4
111#define SR_PAD_IO 0x03, 0xc0, 6
112#define RG_TRX_CTRL_1 (0x04)
113#define SR_IRQ_POLARITY 0x04, 0x01, 0
114#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
115#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
116#define SR_RX_BL_CTRL 0x04, 0x10, 4
117#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
118#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
119#define SR_PA_EXT_EN 0x04, 0x80, 7
120#define RG_PHY_TX_PWR (0x05)
121#define SR_TX_PWR 0x05, 0x0f, 0
122#define SR_PA_LT 0x05, 0x30, 4
123#define SR_PA_BUF_LT 0x05, 0xc0, 6
124#define RG_PHY_RSSI (0x06)
125#define SR_RSSI 0x06, 0x1f, 0
126#define SR_RND_VALUE 0x06, 0x60, 5
127#define SR_RX_CRC_VALID 0x06, 0x80, 7
128#define RG_PHY_ED_LEVEL (0x07)
129#define SR_ED_LEVEL 0x07, 0xff, 0
130#define RG_PHY_CC_CCA (0x08)
131#define SR_CHANNEL 0x08, 0x1f, 0
132#define SR_CCA_MODE 0x08, 0x60, 5
133#define SR_CCA_REQUEST 0x08, 0x80, 7
134#define RG_CCA_THRES (0x09)
135#define SR_CCA_ED_THRES 0x09, 0x0f, 0
136#define SR_RESERVED_09_1 0x09, 0xf0, 4
137#define RG_RX_CTRL (0x0a)
138#define SR_PDT_THRES 0x0a, 0x0f, 0
139#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
140#define RG_SFD_VALUE (0x0b)
141#define SR_SFD_VALUE 0x0b, 0xff, 0
142#define RG_TRX_CTRL_2 (0x0c)
143#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100144#define SR_SUB_MODE 0x0c, 0x04, 2
145#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100146#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
147#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000148#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
149#define RG_ANT_DIV (0x0d)
150#define SR_ANT_CTRL 0x0d, 0x03, 0
151#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
152#define SR_ANT_DIV_EN 0x0d, 0x08, 3
153#define SR_RESERVED_0d_2 0x0d, 0x70, 4
154#define SR_ANT_SEL 0x0d, 0x80, 7
155#define RG_IRQ_MASK (0x0e)
156#define SR_IRQ_MASK 0x0e, 0xff, 0
157#define RG_IRQ_STATUS (0x0f)
158#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
159#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
160#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
161#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
162#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
163#define SR_IRQ_5_AMI 0x0f, 0x20, 5
164#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
165#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
166#define RG_VREG_CTRL (0x10)
167#define SR_RESERVED_10_6 0x10, 0x03, 0
168#define SR_DVDD_OK 0x10, 0x04, 2
169#define SR_DVREG_EXT 0x10, 0x08, 3
170#define SR_RESERVED_10_3 0x10, 0x30, 4
171#define SR_AVDD_OK 0x10, 0x40, 6
172#define SR_AVREG_EXT 0x10, 0x80, 7
173#define RG_BATMON (0x11)
174#define SR_BATMON_VTH 0x11, 0x0f, 0
175#define SR_BATMON_HR 0x11, 0x10, 4
176#define SR_BATMON_OK 0x11, 0x20, 5
177#define SR_RESERVED_11_1 0x11, 0xc0, 6
178#define RG_XOSC_CTRL (0x12)
179#define SR_XTAL_TRIM 0x12, 0x0f, 0
180#define SR_XTAL_MODE 0x12, 0xf0, 4
181#define RG_RX_SYN (0x15)
182#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
183#define SR_RESERVED_15_2 0x15, 0x70, 4
184#define SR_RX_PDT_DIS 0x15, 0x80, 7
185#define RG_XAH_CTRL_1 (0x17)
186#define SR_RESERVED_17_8 0x17, 0x01, 0
187#define SR_AACK_PROM_MODE 0x17, 0x02, 1
188#define SR_AACK_ACK_TIME 0x17, 0x04, 2
189#define SR_RESERVED_17_5 0x17, 0x08, 3
190#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
191#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100192#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000193#define SR_RESERVED_17_1 0x17, 0x80, 7
194#define RG_FTN_CTRL (0x18)
195#define SR_RESERVED_18_2 0x18, 0x7f, 0
196#define SR_FTN_START 0x18, 0x80, 7
197#define RG_PLL_CF (0x1a)
198#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
199#define SR_PLL_CF_START 0x1a, 0x80, 7
200#define RG_PLL_DCU (0x1b)
201#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
202#define SR_RESERVED_1b_2 0x1b, 0x40, 6
203#define SR_PLL_DCU_START 0x1b, 0x80, 7
204#define RG_PART_NUM (0x1c)
205#define SR_PART_NUM 0x1c, 0xff, 0
206#define RG_VERSION_NUM (0x1d)
207#define SR_VERSION_NUM 0x1d, 0xff, 0
208#define RG_MAN_ID_0 (0x1e)
209#define SR_MAN_ID_0 0x1e, 0xff, 0
210#define RG_MAN_ID_1 (0x1f)
211#define SR_MAN_ID_1 0x1f, 0xff, 0
212#define RG_SHORT_ADDR_0 (0x20)
213#define SR_SHORT_ADDR_0 0x20, 0xff, 0
214#define RG_SHORT_ADDR_1 (0x21)
215#define SR_SHORT_ADDR_1 0x21, 0xff, 0
216#define RG_PAN_ID_0 (0x22)
217#define SR_PAN_ID_0 0x22, 0xff, 0
218#define RG_PAN_ID_1 (0x23)
219#define SR_PAN_ID_1 0x23, 0xff, 0
220#define RG_IEEE_ADDR_0 (0x24)
221#define SR_IEEE_ADDR_0 0x24, 0xff, 0
222#define RG_IEEE_ADDR_1 (0x25)
223#define SR_IEEE_ADDR_1 0x25, 0xff, 0
224#define RG_IEEE_ADDR_2 (0x26)
225#define SR_IEEE_ADDR_2 0x26, 0xff, 0
226#define RG_IEEE_ADDR_3 (0x27)
227#define SR_IEEE_ADDR_3 0x27, 0xff, 0
228#define RG_IEEE_ADDR_4 (0x28)
229#define SR_IEEE_ADDR_4 0x28, 0xff, 0
230#define RG_IEEE_ADDR_5 (0x29)
231#define SR_IEEE_ADDR_5 0x29, 0xff, 0
232#define RG_IEEE_ADDR_6 (0x2a)
233#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
234#define RG_IEEE_ADDR_7 (0x2b)
235#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
236#define RG_XAH_CTRL_0 (0x2c)
237#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
238#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
239#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
240#define RG_CSMA_SEED_0 (0x2d)
241#define SR_CSMA_SEED_0 0x2d, 0xff, 0
242#define RG_CSMA_SEED_1 (0x2e)
243#define SR_CSMA_SEED_1 0x2e, 0x07, 0
244#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
245#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
246#define SR_AACK_SET_PD 0x2e, 0x20, 5
247#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
248#define RG_CSMA_BE (0x2f)
249#define SR_MIN_BE 0x2f, 0x0f, 0
250#define SR_MAX_BE 0x2f, 0xf0, 4
251
252#define CMD_REG 0x80
253#define CMD_REG_MASK 0x3f
254#define CMD_WRITE 0x40
255#define CMD_FB 0x20
256
257#define IRQ_BAT_LOW (1 << 7)
258#define IRQ_TRX_UR (1 << 6)
259#define IRQ_AMI (1 << 5)
260#define IRQ_CCA_ED (1 << 4)
261#define IRQ_TRX_END (1 << 3)
262#define IRQ_RX_START (1 << 2)
263#define IRQ_PLL_UNL (1 << 1)
264#define IRQ_PLL_LOCK (1 << 0)
265
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000266#define IRQ_ACTIVE_HIGH 0
267#define IRQ_ACTIVE_LOW 1
268
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000269#define STATE_P_ON 0x00 /* BUSY */
270#define STATE_BUSY_RX 0x01
271#define STATE_BUSY_TX 0x02
272#define STATE_FORCE_TRX_OFF 0x03
273#define STATE_FORCE_TX_ON 0x04 /* IDLE */
274/* 0x05 */ /* INVALID_PARAMETER */
275#define STATE_RX_ON 0x06
276/* 0x07 */ /* SUCCESS */
277#define STATE_TRX_OFF 0x08
278#define STATE_TX_ON 0x09
279/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
280#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500281#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000282#define STATE_BUSY_RX_AACK 0x11
283#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000284#define STATE_RX_AACK_ON 0x16
285#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000286#define STATE_RX_ON_NOCLK 0x1C
287#define STATE_RX_AACK_ON_NOCLK 0x1D
288#define STATE_BUSY_RX_AACK_NOCLK 0x1E
289#define STATE_TRANSITION_IN_PROGRESS 0x1F
290
Alexander Aringf76014f772014-07-03 00:20:44 +0200291#define AT86RF2XX_NUMREGS 0x3F
292
Alexander Aring97fed792014-10-07 10:38:32 +0200293static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200294at86rf230_async_state_change(struct at86rf230_local *lp,
295 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200296 const u8 state, void (*complete)(void *context),
297 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200298
Alexander Aringf76014f772014-07-03 00:20:44 +0200299static inline int
300__at86rf230_write(struct at86rf230_local *lp,
301 unsigned int addr, unsigned int data)
302{
303 return regmap_write(lp->regmap, addr, data);
304}
305
306static inline int
307__at86rf230_read(struct at86rf230_local *lp,
308 unsigned int addr, unsigned int *data)
309{
310 return regmap_read(lp->regmap, addr, data);
311}
312
313static inline int
314at86rf230_read_subreg(struct at86rf230_local *lp,
315 unsigned int addr, unsigned int mask,
316 unsigned int shift, unsigned int *data)
317{
318 int rc;
319
320 rc = __at86rf230_read(lp, addr, data);
321 if (rc > 0)
322 *data = (*data & mask) >> shift;
323
324 return rc;
325}
326
327static inline int
328at86rf230_write_subreg(struct at86rf230_local *lp,
329 unsigned int addr, unsigned int mask,
330 unsigned int shift, unsigned int data)
331{
332 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
333}
334
335static bool
336at86rf230_reg_writeable(struct device *dev, unsigned int reg)
337{
338 switch (reg) {
339 case RG_TRX_STATE:
340 case RG_TRX_CTRL_0:
341 case RG_TRX_CTRL_1:
342 case RG_PHY_TX_PWR:
343 case RG_PHY_ED_LEVEL:
344 case RG_PHY_CC_CCA:
345 case RG_CCA_THRES:
346 case RG_RX_CTRL:
347 case RG_SFD_VALUE:
348 case RG_TRX_CTRL_2:
349 case RG_ANT_DIV:
350 case RG_IRQ_MASK:
351 case RG_VREG_CTRL:
352 case RG_BATMON:
353 case RG_XOSC_CTRL:
354 case RG_RX_SYN:
355 case RG_XAH_CTRL_1:
356 case RG_FTN_CTRL:
357 case RG_PLL_CF:
358 case RG_PLL_DCU:
359 case RG_SHORT_ADDR_0:
360 case RG_SHORT_ADDR_1:
361 case RG_PAN_ID_0:
362 case RG_PAN_ID_1:
363 case RG_IEEE_ADDR_0:
364 case RG_IEEE_ADDR_1:
365 case RG_IEEE_ADDR_2:
366 case RG_IEEE_ADDR_3:
367 case RG_IEEE_ADDR_4:
368 case RG_IEEE_ADDR_5:
369 case RG_IEEE_ADDR_6:
370 case RG_IEEE_ADDR_7:
371 case RG_XAH_CTRL_0:
372 case RG_CSMA_SEED_0:
373 case RG_CSMA_SEED_1:
374 case RG_CSMA_BE:
375 return true;
376 default:
377 return false;
378 }
379}
380
381static bool
382at86rf230_reg_readable(struct device *dev, unsigned int reg)
383{
384 bool rc;
385
386 /* all writeable are also readable */
387 rc = at86rf230_reg_writeable(dev, reg);
388 if (rc)
389 return rc;
390
391 /* readonly regs */
392 switch (reg) {
393 case RG_TRX_STATUS:
394 case RG_PHY_RSSI:
395 case RG_IRQ_STATUS:
396 case RG_PART_NUM:
397 case RG_VERSION_NUM:
398 case RG_MAN_ID_1:
399 case RG_MAN_ID_0:
400 return true;
401 default:
402 return false;
403 }
404}
405
406static bool
407at86rf230_reg_volatile(struct device *dev, unsigned int reg)
408{
409 /* can be changed during runtime */
410 switch (reg) {
411 case RG_TRX_STATUS:
412 case RG_TRX_STATE:
413 case RG_PHY_RSSI:
414 case RG_PHY_ED_LEVEL:
415 case RG_IRQ_STATUS:
416 case RG_VREG_CTRL:
417 return true;
418 default:
419 return false;
420 }
421}
422
423static bool
424at86rf230_reg_precious(struct device *dev, unsigned int reg)
425{
426 /* don't clear irq line on read */
427 switch (reg) {
428 case RG_IRQ_STATUS:
429 return true;
430 default:
431 return false;
432 }
433}
434
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100435static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200436 .reg_bits = 8,
437 .val_bits = 8,
438 .write_flag_mask = CMD_REG | CMD_WRITE,
439 .read_flag_mask = CMD_REG,
440 .cache_type = REGCACHE_RBTREE,
441 .max_register = AT86RF2XX_NUMREGS,
442 .writeable_reg = at86rf230_reg_writeable,
443 .readable_reg = at86rf230_reg_readable,
444 .volatile_reg = at86rf230_reg_volatile,
445 .precious_reg = at86rf230_reg_precious,
446};
447
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200448static void
449at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000450{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200451 struct at86rf230_state_change *ctx = context;
452 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000453
Alexander Aring97fed792014-10-07 10:38:32 +0200454 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100455 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200456}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000457
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100458static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200459at86rf230_async_error(struct at86rf230_local *lp,
460 struct at86rf230_state_change *ctx, int rc)
461{
462 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000463
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200464 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200465 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200466}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000467
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200468/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200469static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200470at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
471 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200472 void (*complete)(void *context),
473 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200474{
Alexander Aring97fed792014-10-07 10:38:32 +0200475 int rc;
476
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200477 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000478
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200479 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
480 ctx->trx.len = 2;
481 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200482 ctx->irq_enable = irq_enable;
483 rc = spi_async(lp->spi, &ctx->msg);
484 if (rc) {
485 if (irq_enable)
486 enable_irq(lp->spi->irq);
487
488 at86rf230_async_error(lp, ctx, rc);
489 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200490}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000491
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200492static void
493at86rf230_async_state_assert(void *context)
494{
495 struct at86rf230_state_change *ctx = context;
496 struct at86rf230_local *lp = ctx->lp;
497 const u8 *buf = ctx->buf;
498 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000499
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200500 /* Assert state change */
501 if (trx_state != ctx->to_state) {
502 /* Special handling if transceiver state is in
503 * STATE_BUSY_RX_AACK and a SHR was detected.
504 */
505 if (trx_state == STATE_BUSY_RX_AACK) {
506 /* Undocumented race condition. If we send a state
507 * change to STATE_RX_AACK_ON the transceiver could
508 * change his state automatically to STATE_BUSY_RX_AACK
509 * if a SHR was detected. This is not an error, but we
510 * can't assert this.
511 */
512 if (ctx->to_state == STATE_RX_AACK_ON)
513 goto done;
514
515 /* If we change to STATE_TX_ON without forcing and
516 * transceiver state is STATE_BUSY_RX_AACK, we wait
517 * 'tFrame + tPAck' receiving time. In this time the
518 * PDU should be received. If the transceiver is still
519 * in STATE_BUSY_RX_AACK, we run a force state change
520 * to STATE_TX_ON. This is a timeout handling, if the
521 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100522 *
523 * Additional we do several retries to try to get into
524 * TX_ON state without forcing. If the retries are
525 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
526 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200527 */
528 if (ctx->to_state == STATE_TX_ON) {
Alexander Aringba6d2232015-03-01 21:55:28 +0100529 u8 state = STATE_TX_ON;
530
531 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
532 state = STATE_FORCE_TX_ON;
533 lp->tx_retry++;
534
535 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200536 ctx->complete,
537 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200538 return;
539 }
540 }
541
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200542 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
543 ctx->from_state, ctx->to_state, trx_state);
544 }
545
546done:
547 if (ctx->complete)
548 ctx->complete(context);
549}
550
551/* Do state change timing delay. */
552static void
553at86rf230_async_state_delay(void *context)
554{
555 struct at86rf230_state_change *ctx = context;
556 struct at86rf230_local *lp = ctx->lp;
557 struct at86rf2xx_chip_data *c = lp->data;
558 bool force = false;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200559
560 /* The force state changes are will show as normal states in the
561 * state status subregister. We change the to_state to the
562 * corresponding one and remember if it was a force change, this
563 * differs if we do a state change from STATE_BUSY_RX_AACK.
564 */
565 switch (ctx->to_state) {
566 case STATE_FORCE_TX_ON:
567 ctx->to_state = STATE_TX_ON;
568 force = true;
569 break;
570 case STATE_FORCE_TRX_OFF:
571 ctx->to_state = STATE_TRX_OFF;
572 force = true;
573 break;
574 default:
575 break;
576 }
577
578 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200579 case STATE_TRX_OFF:
580 switch (ctx->to_state) {
581 case STATE_RX_AACK_ON:
582 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
583 goto change;
584 case STATE_TX_ON:
585 usleep_range(c->t_off_to_tx_on,
586 c->t_off_to_tx_on + 10);
587 goto change;
588 default:
589 break;
590 }
591 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200592 case STATE_BUSY_RX_AACK:
593 switch (ctx->to_state) {
594 case STATE_TX_ON:
595 /* Wait for worst case receiving time if we
596 * didn't make a force change from BUSY_RX_AACK
597 * to TX_ON.
598 */
599 if (!force) {
600 usleep_range(c->t_frame + c->t_p_ack,
601 c->t_frame + c->t_p_ack + 1000);
602 goto change;
603 }
604 break;
605 default:
606 break;
607 }
608 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200609 /* Default value, means RESET state */
610 case STATE_P_ON:
611 switch (ctx->to_state) {
612 case STATE_TRX_OFF:
613 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
614 goto change;
615 default:
616 break;
617 }
618 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200619 default:
620 break;
621 }
622
623 /* Default delay is 1us in the most cases */
624 udelay(1);
625
626change:
Alexander Aring97fed792014-10-07 10:38:32 +0200627 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
628 at86rf230_async_state_assert,
629 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200630}
631
632static void
633at86rf230_async_state_change_start(void *context)
634{
635 struct at86rf230_state_change *ctx = context;
636 struct at86rf230_local *lp = ctx->lp;
637 u8 *buf = ctx->buf;
638 const u8 trx_state = buf[1] & 0x1f;
639 int rc;
640
641 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
642 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
643 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200644 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
645 at86rf230_async_state_change_start,
646 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200647 return;
648 }
649
650 /* Check if we already are in the state which we change in */
651 if (trx_state == ctx->to_state) {
652 if (ctx->complete)
653 ctx->complete(context);
654 return;
655 }
656
657 /* Set current state to the context of state change */
658 ctx->from_state = trx_state;
659
660 /* Going into the next step for a state change which do a timing
661 * relevant delay.
662 */
663 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
664 buf[1] = ctx->to_state;
665 ctx->trx.len = 2;
666 ctx->msg.complete = at86rf230_async_state_delay;
667 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200668 if (rc) {
669 if (ctx->irq_enable)
670 enable_irq(lp->spi->irq);
671
Alexander Aring4fef7d32014-12-15 10:25:55 +0100672 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200673 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000674}
675
Alexander Aring97fed792014-10-07 10:38:32 +0200676static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200677at86rf230_async_state_change(struct at86rf230_local *lp,
678 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200679 const u8 state, void (*complete)(void *context),
680 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000681{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200682 /* Initialization for the state change context */
683 ctx->to_state = state;
684 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200685 ctx->irq_enable = irq_enable;
686 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
687 at86rf230_async_state_change_start,
688 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200689}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000690
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200691static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200692at86rf230_sync_state_change_complete(void *context)
693{
694 struct at86rf230_state_change *ctx = context;
695 struct at86rf230_local *lp = ctx->lp;
696
697 complete(&lp->state_complete);
698}
699
700/* This function do a sync framework above the async state change.
701 * Some callbacks of the IEEE 802.15.4 driver interface need to be
702 * handled synchronously.
703 */
704static int
705at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
706{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100707 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200708
Alexander Aring97fed792014-10-07 10:38:32 +0200709 at86rf230_async_state_change(lp, &lp->state, state,
710 at86rf230_sync_state_change_complete,
711 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200712
713 rc = wait_for_completion_timeout(&lp->state_complete,
714 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200715 if (!rc) {
716 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200717 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200718 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200719
720 return 0;
721}
722
723static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200724at86rf230_tx_complete(void *context)
725{
726 struct at86rf230_state_change *ctx = context;
727 struct at86rf230_local *lp = ctx->lp;
Alexander Aring955aee82014-10-26 09:37:15 +0100728 struct sk_buff *skb = lp->tx_skb;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000729
Alexander Aring35e92a82014-10-07 10:38:31 +0200730 enable_irq(lp->spi->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100731
Alexander Aring5f5c5c22014-12-15 10:25:53 +0100732 ieee802154_xmit_complete(lp->hw, skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200733}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000734
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200735static void
736at86rf230_tx_on(void *context)
737{
738 struct at86rf230_state_change *ctx = context;
739 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000740
Alexander Aring97fed792014-10-07 10:38:32 +0200741 at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
742 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200743}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000744
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200745static void
746at86rf230_tx_trac_error(void *context)
747{
748 struct at86rf230_state_change *ctx = context;
749 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000750
Alexander Aring97fed792014-10-07 10:38:32 +0200751 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
752 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200753}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000754
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200755static void
756at86rf230_tx_trac_check(void *context)
757{
758 struct at86rf230_state_change *ctx = context;
759 struct at86rf230_local *lp = ctx->lp;
760 const u8 *buf = ctx->buf;
761 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000762
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200763 /* If trac status is different than zero we need to do a state change
764 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
765 * state to TX_ON.
766 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100767 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200768 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
769 at86rf230_tx_trac_error, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100770 else
771 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200772}
773
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200774static void
775at86rf230_tx_trac_status(void *context)
776{
777 struct at86rf230_state_change *ctx = context;
778 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200779
Alexander Aring97fed792014-10-07 10:38:32 +0200780 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
781 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200782}
783
784static void
785at86rf230_rx(struct at86rf230_local *lp,
Alexander Aringb89c3342014-10-27 17:13:42 +0100786 const u8 *data, const u8 len, const u8 lqi)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200787{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200788 struct sk_buff *skb;
789 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
790
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200791 memcpy(rx_local_buf, data, len);
792 enable_irq(lp->spi->irq);
793
Alexander Aring61a22812014-10-27 17:13:29 +0100794 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200795 if (!skb) {
796 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
797 return;
798 }
799
800 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100801 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200802}
803
804static void
805at86rf230_rx_read_frame_complete(void *context)
806{
807 struct at86rf230_state_change *ctx = context;
808 struct at86rf230_local *lp = ctx->lp;
809 const u8 *buf = lp->irq.buf;
Alexander Aringd0e73c42014-10-27 17:13:41 +0100810 u8 len = buf[1];
811
812 if (!ieee802154_is_valid_psdu_len(len)) {
813 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
814 len = IEEE802154_MTU;
815 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200816
Alexander Aring2ac0f3a2014-10-29 21:34:43 +0100817 at86rf230_rx(lp, buf + 2, len, buf[2 + len]);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200818}
819
Alexander Aring97fed792014-10-07 10:38:32 +0200820static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200821at86rf230_rx_read_frame(struct at86rf230_local *lp)
822{
Alexander Aring97fed792014-10-07 10:38:32 +0200823 int rc;
824
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200825 u8 *buf = lp->irq.buf;
826
827 buf[0] = CMD_FB;
828 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
829 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200830 rc = spi_async(lp->spi, &lp->irq.msg);
831 if (rc) {
832 enable_irq(lp->spi->irq);
833 at86rf230_async_error(lp, &lp->irq, rc);
834 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200835}
836
837static void
838at86rf230_rx_trac_check(void *context)
839{
840 struct at86rf230_state_change *ctx = context;
841 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200842
843 /* Possible check on trac status here. This could be useful to make
844 * some stats why receive is failed. Not used at the moment, but it's
845 * maybe timing relevant. Datasheet doesn't say anything about this.
846 * The programming guide say do it so.
847 */
848
Alexander Aring97fed792014-10-07 10:38:32 +0200849 at86rf230_rx_read_frame(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200850}
851
Alexander Aring97fed792014-10-07 10:38:32 +0200852static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200853at86rf230_irq_trx_end(struct at86rf230_local *lp)
854{
855 spin_lock(&lp->lock);
856 if (lp->is_tx) {
857 lp->is_tx = 0;
858 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200859
860 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200861 at86rf230_async_state_change(lp, &lp->irq,
862 STATE_FORCE_TX_ON,
863 at86rf230_tx_trac_status,
864 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200865 else
Alexander Aring97fed792014-10-07 10:38:32 +0200866 at86rf230_async_state_change(lp, &lp->irq,
867 STATE_RX_AACK_ON,
868 at86rf230_tx_complete,
869 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200870 } else {
871 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200872 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
873 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200874 }
875}
876
877static void
878at86rf230_irq_status(void *context)
879{
880 struct at86rf230_state_change *ctx = context;
881 struct at86rf230_local *lp = ctx->lp;
882 const u8 *buf = lp->irq.buf;
883 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200884
885 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200886 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200887 } else {
888 enable_irq(lp->spi->irq);
889 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
890 irq);
891 }
892}
893
894static irqreturn_t at86rf230_isr(int irq, void *data)
895{
896 struct at86rf230_local *lp = data;
897 struct at86rf230_state_change *ctx = &lp->irq;
898 u8 *buf = ctx->buf;
899 int rc;
900
Alexander Aring90566362014-10-07 10:38:29 +0200901 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200902
903 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
904 ctx->trx.len = 2;
905 ctx->msg.complete = at86rf230_irq_status;
906 rc = spi_async(lp->spi, &ctx->msg);
907 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200908 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200909 at86rf230_async_error(lp, ctx, rc);
910 return IRQ_NONE;
911 }
912
913 return IRQ_HANDLED;
914}
915
916static void
917at86rf230_write_frame_complete(void *context)
918{
919 struct at86rf230_state_change *ctx = context;
920 struct at86rf230_local *lp = ctx->lp;
921 u8 *buf = ctx->buf;
922 int rc;
923
924 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
925 buf[1] = STATE_BUSY_TX;
926 ctx->trx.len = 2;
927 ctx->msg.complete = NULL;
928 rc = spi_async(lp->spi, &ctx->msg);
929 if (rc)
930 at86rf230_async_error(lp, ctx, rc);
931}
932
933static void
934at86rf230_write_frame(void *context)
935{
936 struct at86rf230_state_change *ctx = context;
937 struct at86rf230_local *lp = ctx->lp;
938 struct sk_buff *skb = lp->tx_skb;
939 u8 *buf = lp->tx.buf;
940 int rc;
941
942 spin_lock(&lp->lock);
943 lp->is_tx = 1;
944 spin_unlock(&lp->lock);
945
946 buf[0] = CMD_FB | CMD_WRITE;
947 buf[1] = skb->len + 2;
948 memcpy(buf + 2, skb->data, skb->len);
949 lp->tx.trx.len = skb->len + 2;
950 lp->tx.msg.complete = at86rf230_write_frame_complete;
951 rc = spi_async(lp->spi, &lp->tx.msg);
952 if (rc)
953 at86rf230_async_error(lp, ctx, rc);
954}
955
956static void
957at86rf230_xmit_tx_on(void *context)
958{
959 struct at86rf230_state_change *ctx = context;
960 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200961
Alexander Aring97fed792014-10-07 10:38:32 +0200962 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
963 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200964}
965
966static int
Alexander Aring5a504392014-10-25 17:16:34 +0200967at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200968{
Alexander Aring5a504392014-10-25 17:16:34 +0200969 struct at86rf230_local *lp = hw->priv;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200970 struct at86rf230_state_change *ctx = &lp->tx;
971
972 void (*tx_complete)(void *context) = at86rf230_write_frame;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200973
974 lp->tx_skb = skb;
975
976 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
977 * are in STATE_TX_ON. The pfad differs here, so we change
978 * the complete handler.
979 */
980 if (lp->tx_aret)
981 tx_complete = at86rf230_xmit_tx_on;
982
Alexander Aringba6d2232015-03-01 21:55:28 +0100983 lp->tx_retry = 0;
Alexander Aring97fed792014-10-07 10:38:32 +0200984 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
985
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200986 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000987}
988
989static int
Alexander Aring5a504392014-10-25 17:16:34 +0200990at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000991{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000992 BUG_ON(!level);
993 *level = 0xbe;
994 return 0;
995}
996
997static int
Alexander Aring5a504392014-10-25 17:16:34 +0200998at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000999{
Alexander Aring5a504392014-10-25 17:16:34 +02001000 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001001}
1002
1003static void
Alexander Aring5a504392014-10-25 17:16:34 +02001004at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001005{
Alexander Aring5a504392014-10-25 17:16:34 +02001006 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001007}
1008
1009static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001010at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001011{
1012 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1013}
1014
1015static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001016at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001017{
1018 int rc;
1019
1020 if (channel == 0)
1021 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1022 else
1023 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1024 if (rc < 0)
1025 return rc;
1026
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001027 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001028 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001029 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001030 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001031 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001032 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001033 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001034 if (rc < 0)
1035 return rc;
1036
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001037 /* This sets the symbol_duration according frequency on the 212.
1038 * TODO move this handling while set channel and page in cfg802154.
1039 * We can do that, this timings are according 802.15.4 standard.
1040 * If we do that in cfg802154, this is a more generic calculation.
1041 *
1042 * This should also protected from ifs_timer. Means cancel timer and
1043 * init with a new value. For now, this is okay.
1044 */
1045 if (channel == 0) {
1046 if (page == 0) {
1047 /* SUB:0 and BPSK:0 -> BPSK-20 */
1048 lp->hw->phy->symbol_duration = 50;
1049 } else {
1050 /* SUB:1 and BPSK:0 -> BPSK-40 */
1051 lp->hw->phy->symbol_duration = 25;
1052 }
1053 } else {
1054 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001055 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001056 lp->hw->phy->symbol_duration = 40;
1057 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001058 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001059 lp->hw->phy->symbol_duration = 16;
1060 }
1061
1062 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1063 lp->hw->phy->symbol_duration;
1064 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1065 lp->hw->phy->symbol_duration;
1066
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001067 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1068}
1069
1070static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001071at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001072{
Alexander Aring5a504392014-10-25 17:16:34 +02001073 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001074 int rc;
1075
Alexander Aringa53d1f72014-07-03 00:20:46 +02001076 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001077 /* Wait for PLL */
1078 usleep_range(lp->data->t_channel_switch,
1079 lp->data->t_channel_switch + 10);
Alexander Aring820bd662014-11-12 03:36:56 +01001080 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001081}
1082
1083static int
Alexander Aring5a504392014-10-25 17:16:34 +02001084at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001085 struct ieee802154_hw_addr_filt *filt,
1086 unsigned long changed)
1087{
Alexander Aring5a504392014-10-25 17:16:34 +02001088 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001089
Alexander Aring57205c12014-10-25 05:25:09 +02001090 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001091 u16 addr = le16_to_cpu(filt->short_addr);
1092
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001093 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001094 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001095 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1096 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001097 }
1098
Alexander Aring57205c12014-10-25 05:25:09 +02001099 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001100 u16 pan = le16_to_cpu(filt->pan_id);
1101
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001102 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001103 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001104 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1105 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001106 }
1107
Alexander Aring57205c12014-10-25 05:25:09 +02001108 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001109 u8 i, addr[8];
1110
1111 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001112 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001113 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001114 for (i = 0; i < 8; i++)
1115 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001116 }
1117
Alexander Aring57205c12014-10-25 05:25:09 +02001118 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001119 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001120 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001121 if (filt->pan_coord)
1122 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1123 else
1124 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1125 }
1126
1127 return 0;
1128}
1129
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001130static int
Alexander Aring5a504392014-10-25 17:16:34 +02001131at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001132{
Alexander Aring5a504392014-10-25 17:16:34 +02001133 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001134
1135 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1136 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1137 * 0dB.
1138 * thus, supported values for db range from -26 to 5, for 31dB of
1139 * reduction to 0dB of reduction.
1140 */
1141 if (db > 5 || db < -26)
1142 return -EINVAL;
1143
1144 db = -(db - 5);
1145
Jean Sacren677676c2014-03-01 15:54:36 -07001146 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001147}
1148
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001149static int
Alexander Aring5a504392014-10-25 17:16:34 +02001150at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001151{
Alexander Aring5a504392014-10-25 17:16:34 +02001152 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001153
1154 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1155}
1156
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001157static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001158at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1159 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001160{
Alexander Aring5a504392014-10-25 17:16:34 +02001161 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001162 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001163
Alexander Aring7fe9a382014-12-10 15:33:12 +01001164 /* mapping 802.15.4 to driver spec */
1165 switch (cca->mode) {
1166 case NL802154_CCA_ENERGY:
1167 val = 1;
1168 break;
1169 case NL802154_CCA_CARRIER:
1170 val = 2;
1171 break;
1172 case NL802154_CCA_ENERGY_CARRIER:
1173 switch (cca->opt) {
1174 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1175 val = 3;
1176 break;
1177 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1178 val = 0;
1179 break;
1180 default:
1181 return -EINVAL;
1182 }
1183 break;
1184 default:
1185 return -EINVAL;
1186 }
1187
1188 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001189}
1190
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001191static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001192at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1193{
1194 return (level - lp->data->rssi_base_val) * 100 / 207;
1195}
1196
1197static int
1198at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1199{
1200 return (level - lp->data->rssi_base_val) / 2;
1201}
1202
1203static int
Alexander Aring5a504392014-10-25 17:16:34 +02001204at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001205{
Alexander Aring5a504392014-10-25 17:16:34 +02001206 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001207
Alexander Aringa53d1f72014-07-03 00:20:46 +02001208 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001209 return -EINVAL;
1210
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001211 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1212 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001213}
1214
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001215static int
Alexander Aring5a504392014-10-25 17:16:34 +02001216at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001217 u8 retries)
1218{
Alexander Aring5a504392014-10-25 17:16:34 +02001219 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001220 int rc;
1221
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001222 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1223 if (rc)
1224 return rc;
1225
1226 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1227 if (rc)
1228 return rc;
1229
Alexander Aring39d7f322014-04-05 13:49:26 +02001230 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001231}
1232
1233static int
Alexander Aring5a504392014-10-25 17:16:34 +02001234at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001235{
Alexander Aring5a504392014-10-25 17:16:34 +02001236 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001237 int rc = 0;
1238
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001239 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001240 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001241
1242 if (retries >= 0)
1243 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1244
1245 return rc;
1246}
1247
Alexander Aring92f45f52014-10-29 21:34:33 +01001248static int
1249at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1250{
1251 struct at86rf230_local *lp = hw->priv;
1252 int rc;
1253
1254 if (on) {
1255 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1256 if (rc < 0)
1257 return rc;
1258
1259 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1260 if (rc < 0)
1261 return rc;
1262 } else {
1263 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1264 if (rc < 0)
1265 return rc;
1266
1267 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1268 if (rc < 0)
1269 return rc;
1270 }
1271
1272 return 0;
1273}
1274
Alexander Aring16301862014-10-28 18:21:18 +01001275static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001276 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001277 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001278 .ed = at86rf230_ed,
1279 .set_channel = at86rf230_channel,
1280 .start = at86rf230_start,
1281 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001282 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001283 .set_txpower = at86rf230_set_txpower,
1284 .set_lbt = at86rf230_set_lbt,
1285 .set_cca_mode = at86rf230_set_cca_mode,
1286 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1287 .set_csma_params = at86rf230_set_csma_params,
1288 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f52014-10-29 21:34:33 +01001289 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001290};
1291
Alexander Aringa53d1f72014-07-03 00:20:46 +02001292static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001293 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001294 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001295 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001296 .t_off_to_aack = 80,
1297 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001298 .t_frame = 4096,
1299 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001300 .rssi_base_val = -91,
1301 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001302 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001303};
1304
1305static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001306 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001307 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001308 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001309 .t_off_to_aack = 110,
1310 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001311 .t_frame = 4096,
1312 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001313 .rssi_base_val = -91,
1314 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001315 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001316};
1317
1318static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001319 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001320 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001321 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001322 .t_off_to_aack = 200,
1323 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001324 .t_frame = 4096,
1325 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001326 .rssi_base_val = -100,
1327 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001328 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001329};
1330
Alexander Aringccdaeb22015-02-27 09:58:26 +01001331static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001332{
Alexander Aring1db05582014-07-03 00:20:50 +02001333 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001334 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001335 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001336
Alexander Aring09e536c2014-07-03 00:20:52 +02001337 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001338 if (rc)
1339 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001340
Alexander Aring4af619a2014-04-24 19:09:05 +02001341 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001342 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1343 irq_type == IRQ_TYPE_EDGE_FALLING)
1344 dev_warn(&lp->spi->dev,
1345 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001346 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1347 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001348 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001349
Alexander Aring18c65042014-04-24 19:09:18 +02001350 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001351 if (rc)
1352 return rc;
1353
Alexander Aring6bd2b132014-07-03 00:20:49 +02001354 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1355 if (rc)
1356 return rc;
1357
Sascha Herrmann057dad62013-04-14 22:33:29 +00001358 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001359 if (rc)
1360 return rc;
1361
Alexander Aringbe64f072015-02-27 09:58:28 +01001362 /* reset values differs in at86rf231 and at86rf233 */
1363 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1364 if (rc)
1365 return rc;
1366
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001367 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1368 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1369 if (rc)
1370 return rc;
1371 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1372 if (rc)
1373 return rc;
1374
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001375 /* CLKM changes are applied immediately */
1376 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1377 if (rc)
1378 return rc;
1379
1380 /* Turn CLKM Off */
1381 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1382 if (rc)
1383 return rc;
1384 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001385 usleep_range(lp->data->t_sleep_cycle,
1386 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001387
Alexander Aringccdaeb22015-02-27 09:58:26 +01001388 /* xtal_trim value is calculated by:
1389 * CL = 0.5 * (CX + CTRIM + CPAR)
1390 *
1391 * whereas:
1392 * CL = capacitor of used crystal
1393 * CX = connected capacitors at xtal pins
1394 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1395 * but this is different on each board setup. You need to fine
1396 * tuning this value via CTRIM.
1397 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1398 * 0 pF upto 4.5 pF.
1399 *
1400 * Examples:
1401 * atben transceiver:
1402 *
1403 * CL = 8 pF
1404 * CX = 12 pF
1405 * CPAR = 3 pF (We assume the magic constant from datasheet)
1406 * CTRIM = 0.9 pF
1407 *
1408 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1409 *
1410 * xtal_trim = 0x3
1411 *
1412 * openlabs transceiver:
1413 *
1414 * CL = 16 pF
1415 * CX = 22 pF
1416 * CPAR = 3 pF (We assume the magic constant from datasheet)
1417 * CTRIM = 4.5 pF
1418 *
1419 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1420 *
1421 * xtal_trim = 0xf
1422 */
1423 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1424 if (rc)
1425 return rc;
1426
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001427 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001428 if (rc)
1429 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001430 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001431 dev_err(&lp->spi->dev, "DVDD error\n");
1432 return -EINVAL;
1433 }
1434
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001435 /* Force setting slotted operation bit to 0. Sometimes the atben
1436 * sets this bit and I don't know why. We set this always force
1437 * to zero while probing.
1438 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001439 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001440}
1441
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001442static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001443at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1444 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001445{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001446 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001447 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001448
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001449 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1450 if (!pdata)
1451 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001452
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001453 *rstn = pdata->rstn;
1454 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001455 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001456 return 0;
1457 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001458
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001459 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1460 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001461 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1462 if (ret < 0 && ret != -EINVAL)
1463 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001464
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001465 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001466}
1467
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001468static int
1469at86rf230_detect_device(struct at86rf230_local *lp)
1470{
1471 unsigned int part, version, val;
1472 u16 man_id = 0;
1473 const char *chip;
1474 int rc;
1475
1476 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1477 if (rc)
1478 return rc;
1479 man_id |= val;
1480
1481 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1482 if (rc)
1483 return rc;
1484 man_id |= (val << 8);
1485
1486 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1487 if (rc)
1488 return rc;
1489
Andrey Yurovsky75989682014-12-17 13:14:42 -08001490 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001491 if (rc)
1492 return rc;
1493
1494 if (man_id != 0x001f) {
1495 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1496 man_id >> 8, man_id & 0xFF);
1497 return -EINVAL;
1498 }
1499
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001500 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8fc84e2014-10-29 21:34:31 +01001501 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
Alexander Aring92f45f52014-10-29 21:34:33 +01001502 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001503
Alexander Aringb48a7c12014-12-10 15:33:14 +01001504 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1505
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001506 switch (part) {
1507 case 2:
1508 chip = "at86rf230";
1509 rc = -ENOTSUPP;
1510 break;
1511 case 3:
1512 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001513 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001514 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001515 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001516 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001517 break;
1518 case 7:
1519 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001520 lp->data = &at86rf212_data;
1521 lp->hw->flags |= IEEE802154_HW_LBT;
1522 lp->hw->phy->channels_supported[0] = 0x00007FF;
1523 lp->hw->phy->channels_supported[2] = 0x00007FF;
1524 lp->hw->phy->current_channel = 5;
1525 lp->hw->phy->symbol_duration = 25;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001526 break;
1527 case 11:
1528 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001529 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001530 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001531 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001532 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001533 break;
1534 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001535 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001536 rc = -ENOTSUPP;
1537 break;
1538 }
1539
1540 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1541
1542 return rc;
1543}
1544
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001545static void
1546at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1547{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001548 lp->state.lp = lp;
1549 spi_message_init(&lp->state.msg);
1550 lp->state.msg.context = &lp->state;
1551 lp->state.trx.tx_buf = lp->state.buf;
1552 lp->state.trx.rx_buf = lp->state.buf;
1553 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1554
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001555 lp->irq.lp = lp;
1556 spi_message_init(&lp->irq.msg);
1557 lp->irq.msg.context = &lp->irq;
1558 lp->irq.trx.tx_buf = lp->irq.buf;
1559 lp->irq.trx.rx_buf = lp->irq.buf;
1560 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1561
1562 lp->tx.lp = lp;
1563 spi_message_init(&lp->tx.msg);
1564 lp->tx.msg.context = &lp->tx;
1565 lp->tx.trx.tx_buf = lp->tx.buf;
1566 lp->tx.trx.rx_buf = lp->tx.buf;
1567 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1568}
1569
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001570static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001571{
Alexander Aring5a504392014-10-25 17:16:34 +02001572 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001573 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001574 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001575 int rc, irq_type, rstn, slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001576 u8 xtal_trim;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001577
1578 if (!spi->irq) {
1579 dev_err(&spi->dev, "no IRQ specified\n");
1580 return -EINVAL;
1581 }
1582
Alexander Aringccdaeb22015-02-27 09:58:26 +01001583 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001584 if (rc < 0) {
1585 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1586 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001587 }
1588
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001589 if (gpio_is_valid(rstn)) {
1590 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001591 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001592 if (rc)
1593 return rc;
1594 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001595
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001596 if (gpio_is_valid(slp_tr)) {
1597 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001598 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001599 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001600 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001601 }
1602
1603 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001604 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001605 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001606 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001607 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001608 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001609 usleep_range(120, 240);
1610 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001611
Alexander Aring5a504392014-10-25 17:16:34 +02001612 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1613 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001614 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001615
Alexander Aring5a504392014-10-25 17:16:34 +02001616 lp = hw->priv;
1617 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001618 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001619 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001620 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001621 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001622
Alexander Aringf76014f772014-07-03 00:20:44 +02001623 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1624 if (IS_ERR(lp->regmap)) {
1625 rc = PTR_ERR(lp->regmap);
1626 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1627 rc);
1628 goto free_dev;
1629 }
1630
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001631 at86rf230_setup_spi_messages(lp);
1632
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001633 rc = at86rf230_detect_device(lp);
1634 if (rc < 0)
1635 goto free_dev;
1636
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001637 spin_lock_init(&lp->lock);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001638 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001639
1640 spi_set_drvdata(spi, lp);
1641
Alexander Aringccdaeb22015-02-27 09:58:26 +01001642 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001643 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001644 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001645
Alexander Aring19626942014-04-24 19:09:15 +02001646 /* Read irq status register to reset irq line */
1647 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001648 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001649 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001650
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001651 irq_type = irq_get_trigger_type(spi->irq);
1652 if (!irq_type)
1653 irq_type = IRQF_TRIGGER_RISING;
1654
1655 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1656 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001657 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001658 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001659
Alexander Aring5a504392014-10-25 17:16:34 +02001660 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001661 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001662 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001663
1664 return rc;
1665
Alexander Aring640985e2014-07-03 00:20:43 +02001666free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001667 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001668
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001669 return rc;
1670}
1671
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001672static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001673{
1674 struct at86rf230_local *lp = spi_get_drvdata(spi);
1675
Alexander Aring17e84a92014-03-31 03:26:51 +02001676 /* mask all at86rf230 irq's */
1677 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001678 ieee802154_unregister_hw(lp->hw);
1679 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001680 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001681
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001682 return 0;
1683}
1684
Alexander Aring1086b4f2014-04-24 19:09:11 +02001685static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001686 { .compatible = "atmel,at86rf230", },
1687 { .compatible = "atmel,at86rf231", },
1688 { .compatible = "atmel,at86rf233", },
1689 { .compatible = "atmel,at86rf212", },
1690 { },
1691};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001692MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001693
Alexander Aring90b15522014-04-24 19:09:12 +02001694static const struct spi_device_id at86rf230_device_id[] = {
1695 { .name = "at86rf230", },
1696 { .name = "at86rf231", },
1697 { .name = "at86rf233", },
1698 { .name = "at86rf212", },
1699 { },
1700};
1701MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1702
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001703static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001704 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001705 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001706 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001707 .name = "at86rf230",
1708 .owner = THIS_MODULE,
1709 },
1710 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001711 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001712};
1713
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001714module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001715
1716MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1717MODULE_LICENSE("GPL v2");