blob: 7f27fa35bde33ba67ee6db7650b5fa8e65614784 [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;
Alexander Aringcca990c2015-03-01 21:55:31 +010065 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020066
67 struct spi_message msg;
68 struct spi_transfer trx;
69 u8 buf[AT86RF2XX_MAX_BUF];
70
71 void (*complete)(void *context);
72 u8 from_state;
73 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020074
75 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020076};
77
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000078struct at86rf230_local {
79 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000080
Alexander Aring5a504392014-10-25 17:16:34 +020081 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020082 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020083 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000084
Alexander Aring2e0571c2014-07-03 00:20:51 +020085 struct completion state_complete;
86 struct at86rf230_state_change state;
87
Alexander Aring1d15d6b2014-07-03 00:20:48 +020088 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020089
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010090 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020091 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020092 bool is_tx;
93 /* spinlock for is_tx protection */
94 spinlock_t lock;
Alexander Aringba6d2232015-03-01 21:55:28 +010095 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020096 struct sk_buff *tx_skb;
97 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000098};
99
100#define RG_TRX_STATUS (0x01)
101#define SR_TRX_STATUS 0x01, 0x1f, 0
102#define SR_RESERVED_01_3 0x01, 0x20, 5
103#define SR_CCA_STATUS 0x01, 0x40, 6
104#define SR_CCA_DONE 0x01, 0x80, 7
105#define RG_TRX_STATE (0x02)
106#define SR_TRX_CMD 0x02, 0x1f, 0
107#define SR_TRAC_STATUS 0x02, 0xe0, 5
108#define RG_TRX_CTRL_0 (0x03)
109#define SR_CLKM_CTRL 0x03, 0x07, 0
110#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
111#define SR_PAD_IO_CLKM 0x03, 0x30, 4
112#define SR_PAD_IO 0x03, 0xc0, 6
113#define RG_TRX_CTRL_1 (0x04)
114#define SR_IRQ_POLARITY 0x04, 0x01, 0
115#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
116#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
117#define SR_RX_BL_CTRL 0x04, 0x10, 4
118#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
119#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
120#define SR_PA_EXT_EN 0x04, 0x80, 7
121#define RG_PHY_TX_PWR (0x05)
122#define SR_TX_PWR 0x05, 0x0f, 0
123#define SR_PA_LT 0x05, 0x30, 4
124#define SR_PA_BUF_LT 0x05, 0xc0, 6
125#define RG_PHY_RSSI (0x06)
126#define SR_RSSI 0x06, 0x1f, 0
127#define SR_RND_VALUE 0x06, 0x60, 5
128#define SR_RX_CRC_VALID 0x06, 0x80, 7
129#define RG_PHY_ED_LEVEL (0x07)
130#define SR_ED_LEVEL 0x07, 0xff, 0
131#define RG_PHY_CC_CCA (0x08)
132#define SR_CHANNEL 0x08, 0x1f, 0
133#define SR_CCA_MODE 0x08, 0x60, 5
134#define SR_CCA_REQUEST 0x08, 0x80, 7
135#define RG_CCA_THRES (0x09)
136#define SR_CCA_ED_THRES 0x09, 0x0f, 0
137#define SR_RESERVED_09_1 0x09, 0xf0, 4
138#define RG_RX_CTRL (0x0a)
139#define SR_PDT_THRES 0x0a, 0x0f, 0
140#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
141#define RG_SFD_VALUE (0x0b)
142#define SR_SFD_VALUE 0x0b, 0xff, 0
143#define RG_TRX_CTRL_2 (0x0c)
144#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100145#define SR_SUB_MODE 0x0c, 0x04, 2
146#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100147#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
148#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000149#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
150#define RG_ANT_DIV (0x0d)
151#define SR_ANT_CTRL 0x0d, 0x03, 0
152#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
153#define SR_ANT_DIV_EN 0x0d, 0x08, 3
154#define SR_RESERVED_0d_2 0x0d, 0x70, 4
155#define SR_ANT_SEL 0x0d, 0x80, 7
156#define RG_IRQ_MASK (0x0e)
157#define SR_IRQ_MASK 0x0e, 0xff, 0
158#define RG_IRQ_STATUS (0x0f)
159#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
160#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
161#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
162#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
163#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
164#define SR_IRQ_5_AMI 0x0f, 0x20, 5
165#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
166#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
167#define RG_VREG_CTRL (0x10)
168#define SR_RESERVED_10_6 0x10, 0x03, 0
169#define SR_DVDD_OK 0x10, 0x04, 2
170#define SR_DVREG_EXT 0x10, 0x08, 3
171#define SR_RESERVED_10_3 0x10, 0x30, 4
172#define SR_AVDD_OK 0x10, 0x40, 6
173#define SR_AVREG_EXT 0x10, 0x80, 7
174#define RG_BATMON (0x11)
175#define SR_BATMON_VTH 0x11, 0x0f, 0
176#define SR_BATMON_HR 0x11, 0x10, 4
177#define SR_BATMON_OK 0x11, 0x20, 5
178#define SR_RESERVED_11_1 0x11, 0xc0, 6
179#define RG_XOSC_CTRL (0x12)
180#define SR_XTAL_TRIM 0x12, 0x0f, 0
181#define SR_XTAL_MODE 0x12, 0xf0, 4
182#define RG_RX_SYN (0x15)
183#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
184#define SR_RESERVED_15_2 0x15, 0x70, 4
185#define SR_RX_PDT_DIS 0x15, 0x80, 7
186#define RG_XAH_CTRL_1 (0x17)
187#define SR_RESERVED_17_8 0x17, 0x01, 0
188#define SR_AACK_PROM_MODE 0x17, 0x02, 1
189#define SR_AACK_ACK_TIME 0x17, 0x04, 2
190#define SR_RESERVED_17_5 0x17, 0x08, 3
191#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
192#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100193#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000194#define SR_RESERVED_17_1 0x17, 0x80, 7
195#define RG_FTN_CTRL (0x18)
196#define SR_RESERVED_18_2 0x18, 0x7f, 0
197#define SR_FTN_START 0x18, 0x80, 7
198#define RG_PLL_CF (0x1a)
199#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
200#define SR_PLL_CF_START 0x1a, 0x80, 7
201#define RG_PLL_DCU (0x1b)
202#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
203#define SR_RESERVED_1b_2 0x1b, 0x40, 6
204#define SR_PLL_DCU_START 0x1b, 0x80, 7
205#define RG_PART_NUM (0x1c)
206#define SR_PART_NUM 0x1c, 0xff, 0
207#define RG_VERSION_NUM (0x1d)
208#define SR_VERSION_NUM 0x1d, 0xff, 0
209#define RG_MAN_ID_0 (0x1e)
210#define SR_MAN_ID_0 0x1e, 0xff, 0
211#define RG_MAN_ID_1 (0x1f)
212#define SR_MAN_ID_1 0x1f, 0xff, 0
213#define RG_SHORT_ADDR_0 (0x20)
214#define SR_SHORT_ADDR_0 0x20, 0xff, 0
215#define RG_SHORT_ADDR_1 (0x21)
216#define SR_SHORT_ADDR_1 0x21, 0xff, 0
217#define RG_PAN_ID_0 (0x22)
218#define SR_PAN_ID_0 0x22, 0xff, 0
219#define RG_PAN_ID_1 (0x23)
220#define SR_PAN_ID_1 0x23, 0xff, 0
221#define RG_IEEE_ADDR_0 (0x24)
222#define SR_IEEE_ADDR_0 0x24, 0xff, 0
223#define RG_IEEE_ADDR_1 (0x25)
224#define SR_IEEE_ADDR_1 0x25, 0xff, 0
225#define RG_IEEE_ADDR_2 (0x26)
226#define SR_IEEE_ADDR_2 0x26, 0xff, 0
227#define RG_IEEE_ADDR_3 (0x27)
228#define SR_IEEE_ADDR_3 0x27, 0xff, 0
229#define RG_IEEE_ADDR_4 (0x28)
230#define SR_IEEE_ADDR_4 0x28, 0xff, 0
231#define RG_IEEE_ADDR_5 (0x29)
232#define SR_IEEE_ADDR_5 0x29, 0xff, 0
233#define RG_IEEE_ADDR_6 (0x2a)
234#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
235#define RG_IEEE_ADDR_7 (0x2b)
236#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
237#define RG_XAH_CTRL_0 (0x2c)
238#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
239#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
240#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
241#define RG_CSMA_SEED_0 (0x2d)
242#define SR_CSMA_SEED_0 0x2d, 0xff, 0
243#define RG_CSMA_SEED_1 (0x2e)
244#define SR_CSMA_SEED_1 0x2e, 0x07, 0
245#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
246#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
247#define SR_AACK_SET_PD 0x2e, 0x20, 5
248#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
249#define RG_CSMA_BE (0x2f)
250#define SR_MIN_BE 0x2f, 0x0f, 0
251#define SR_MAX_BE 0x2f, 0xf0, 4
252
253#define CMD_REG 0x80
254#define CMD_REG_MASK 0x3f
255#define CMD_WRITE 0x40
256#define CMD_FB 0x20
257
258#define IRQ_BAT_LOW (1 << 7)
259#define IRQ_TRX_UR (1 << 6)
260#define IRQ_AMI (1 << 5)
261#define IRQ_CCA_ED (1 << 4)
262#define IRQ_TRX_END (1 << 3)
263#define IRQ_RX_START (1 << 2)
264#define IRQ_PLL_UNL (1 << 1)
265#define IRQ_PLL_LOCK (1 << 0)
266
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000267#define IRQ_ACTIVE_HIGH 0
268#define IRQ_ACTIVE_LOW 1
269
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000270#define STATE_P_ON 0x00 /* BUSY */
271#define STATE_BUSY_RX 0x01
272#define STATE_BUSY_TX 0x02
273#define STATE_FORCE_TRX_OFF 0x03
274#define STATE_FORCE_TX_ON 0x04 /* IDLE */
275/* 0x05 */ /* INVALID_PARAMETER */
276#define STATE_RX_ON 0x06
277/* 0x07 */ /* SUCCESS */
278#define STATE_TRX_OFF 0x08
279#define STATE_TX_ON 0x09
280/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
281#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500282#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000283#define STATE_BUSY_RX_AACK 0x11
284#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000285#define STATE_RX_AACK_ON 0x16
286#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000287#define STATE_RX_ON_NOCLK 0x1C
288#define STATE_RX_AACK_ON_NOCLK 0x1D
289#define STATE_BUSY_RX_AACK_NOCLK 0x1E
290#define STATE_TRANSITION_IN_PROGRESS 0x1F
291
Alexander Aringf76014f772014-07-03 00:20:44 +0200292#define AT86RF2XX_NUMREGS 0x3F
293
Alexander Aring97fed792014-10-07 10:38:32 +0200294static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200295at86rf230_async_state_change(struct at86rf230_local *lp,
296 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200297 const u8 state, void (*complete)(void *context),
298 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200299
Alexander Aringf76014f772014-07-03 00:20:44 +0200300static inline int
301__at86rf230_write(struct at86rf230_local *lp,
302 unsigned int addr, unsigned int data)
303{
304 return regmap_write(lp->regmap, addr, data);
305}
306
307static inline int
308__at86rf230_read(struct at86rf230_local *lp,
309 unsigned int addr, unsigned int *data)
310{
311 return regmap_read(lp->regmap, addr, data);
312}
313
314static inline int
315at86rf230_read_subreg(struct at86rf230_local *lp,
316 unsigned int addr, unsigned int mask,
317 unsigned int shift, unsigned int *data)
318{
319 int rc;
320
321 rc = __at86rf230_read(lp, addr, data);
322 if (rc > 0)
323 *data = (*data & mask) >> shift;
324
325 return rc;
326}
327
328static inline int
329at86rf230_write_subreg(struct at86rf230_local *lp,
330 unsigned int addr, unsigned int mask,
331 unsigned int shift, unsigned int data)
332{
333 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
334}
335
336static bool
337at86rf230_reg_writeable(struct device *dev, unsigned int reg)
338{
339 switch (reg) {
340 case RG_TRX_STATE:
341 case RG_TRX_CTRL_0:
342 case RG_TRX_CTRL_1:
343 case RG_PHY_TX_PWR:
344 case RG_PHY_ED_LEVEL:
345 case RG_PHY_CC_CCA:
346 case RG_CCA_THRES:
347 case RG_RX_CTRL:
348 case RG_SFD_VALUE:
349 case RG_TRX_CTRL_2:
350 case RG_ANT_DIV:
351 case RG_IRQ_MASK:
352 case RG_VREG_CTRL:
353 case RG_BATMON:
354 case RG_XOSC_CTRL:
355 case RG_RX_SYN:
356 case RG_XAH_CTRL_1:
357 case RG_FTN_CTRL:
358 case RG_PLL_CF:
359 case RG_PLL_DCU:
360 case RG_SHORT_ADDR_0:
361 case RG_SHORT_ADDR_1:
362 case RG_PAN_ID_0:
363 case RG_PAN_ID_1:
364 case RG_IEEE_ADDR_0:
365 case RG_IEEE_ADDR_1:
366 case RG_IEEE_ADDR_2:
367 case RG_IEEE_ADDR_3:
368 case RG_IEEE_ADDR_4:
369 case RG_IEEE_ADDR_5:
370 case RG_IEEE_ADDR_6:
371 case RG_IEEE_ADDR_7:
372 case RG_XAH_CTRL_0:
373 case RG_CSMA_SEED_0:
374 case RG_CSMA_SEED_1:
375 case RG_CSMA_BE:
376 return true;
377 default:
378 return false;
379 }
380}
381
382static bool
383at86rf230_reg_readable(struct device *dev, unsigned int reg)
384{
385 bool rc;
386
387 /* all writeable are also readable */
388 rc = at86rf230_reg_writeable(dev, reg);
389 if (rc)
390 return rc;
391
392 /* readonly regs */
393 switch (reg) {
394 case RG_TRX_STATUS:
395 case RG_PHY_RSSI:
396 case RG_IRQ_STATUS:
397 case RG_PART_NUM:
398 case RG_VERSION_NUM:
399 case RG_MAN_ID_1:
400 case RG_MAN_ID_0:
401 return true;
402 default:
403 return false;
404 }
405}
406
407static bool
408at86rf230_reg_volatile(struct device *dev, unsigned int reg)
409{
410 /* can be changed during runtime */
411 switch (reg) {
412 case RG_TRX_STATUS:
413 case RG_TRX_STATE:
414 case RG_PHY_RSSI:
415 case RG_PHY_ED_LEVEL:
416 case RG_IRQ_STATUS:
417 case RG_VREG_CTRL:
418 return true;
419 default:
420 return false;
421 }
422}
423
424static bool
425at86rf230_reg_precious(struct device *dev, unsigned int reg)
426{
427 /* don't clear irq line on read */
428 switch (reg) {
429 case RG_IRQ_STATUS:
430 return true;
431 default:
432 return false;
433 }
434}
435
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100436static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200437 .reg_bits = 8,
438 .val_bits = 8,
439 .write_flag_mask = CMD_REG | CMD_WRITE,
440 .read_flag_mask = CMD_REG,
441 .cache_type = REGCACHE_RBTREE,
442 .max_register = AT86RF2XX_NUMREGS,
443 .writeable_reg = at86rf230_reg_writeable,
444 .readable_reg = at86rf230_reg_readable,
445 .volatile_reg = at86rf230_reg_volatile,
446 .precious_reg = at86rf230_reg_precious,
447};
448
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200449static void
450at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000451{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200452 struct at86rf230_state_change *ctx = context;
453 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000454
Alexander Aring97fed792014-10-07 10:38:32 +0200455 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100456 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200457}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000458
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100459static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200460at86rf230_async_error(struct at86rf230_local *lp,
461 struct at86rf230_state_change *ctx, int rc)
462{
463 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000464
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200466 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200467}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000468
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200469/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200470static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200471at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
472 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200473 void (*complete)(void *context),
474 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200475{
Alexander Aring97fed792014-10-07 10:38:32 +0200476 int rc;
477
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200478 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000479
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200480 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
481 ctx->trx.len = 2;
482 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200483 ctx->irq_enable = irq_enable;
484 rc = spi_async(lp->spi, &ctx->msg);
485 if (rc) {
486 if (irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100487 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200488
489 at86rf230_async_error(lp, ctx, rc);
490 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200491}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000492
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200493static void
494at86rf230_async_state_assert(void *context)
495{
496 struct at86rf230_state_change *ctx = context;
497 struct at86rf230_local *lp = ctx->lp;
498 const u8 *buf = ctx->buf;
499 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000500
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200501 /* Assert state change */
502 if (trx_state != ctx->to_state) {
503 /* Special handling if transceiver state is in
504 * STATE_BUSY_RX_AACK and a SHR was detected.
505 */
506 if (trx_state == STATE_BUSY_RX_AACK) {
507 /* Undocumented race condition. If we send a state
508 * change to STATE_RX_AACK_ON the transceiver could
509 * change his state automatically to STATE_BUSY_RX_AACK
510 * if a SHR was detected. This is not an error, but we
511 * can't assert this.
512 */
513 if (ctx->to_state == STATE_RX_AACK_ON)
514 goto done;
515
516 /* If we change to STATE_TX_ON without forcing and
517 * transceiver state is STATE_BUSY_RX_AACK, we wait
518 * 'tFrame + tPAck' receiving time. In this time the
519 * PDU should be received. If the transceiver is still
520 * in STATE_BUSY_RX_AACK, we run a force state change
521 * to STATE_TX_ON. This is a timeout handling, if the
522 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100523 *
524 * Additional we do several retries to try to get into
525 * TX_ON state without forcing. If the retries are
526 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
527 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200528 */
529 if (ctx->to_state == STATE_TX_ON) {
Alexander Aringba6d2232015-03-01 21:55:28 +0100530 u8 state = STATE_TX_ON;
531
532 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
533 state = STATE_FORCE_TX_ON;
534 lp->tx_retry++;
535
536 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200537 ctx->complete,
538 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200539 return;
540 }
541 }
542
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200543 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
544 ctx->from_state, ctx->to_state, trx_state);
545 }
546
547done:
548 if (ctx->complete)
549 ctx->complete(context);
550}
551
552/* Do state change timing delay. */
553static void
554at86rf230_async_state_delay(void *context)
555{
556 struct at86rf230_state_change *ctx = context;
557 struct at86rf230_local *lp = ctx->lp;
558 struct at86rf2xx_chip_data *c = lp->data;
559 bool force = false;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200560
561 /* The force state changes are will show as normal states in the
562 * state status subregister. We change the to_state to the
563 * corresponding one and remember if it was a force change, this
564 * differs if we do a state change from STATE_BUSY_RX_AACK.
565 */
566 switch (ctx->to_state) {
567 case STATE_FORCE_TX_ON:
568 ctx->to_state = STATE_TX_ON;
569 force = true;
570 break;
571 case STATE_FORCE_TRX_OFF:
572 ctx->to_state = STATE_TRX_OFF;
573 force = true;
574 break;
575 default:
576 break;
577 }
578
579 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200580 case STATE_TRX_OFF:
581 switch (ctx->to_state) {
582 case STATE_RX_AACK_ON:
583 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
584 goto change;
585 case STATE_TX_ON:
586 usleep_range(c->t_off_to_tx_on,
587 c->t_off_to_tx_on + 10);
588 goto change;
589 default:
590 break;
591 }
592 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200593 case STATE_BUSY_RX_AACK:
594 switch (ctx->to_state) {
595 case STATE_TX_ON:
596 /* Wait for worst case receiving time if we
597 * didn't make a force change from BUSY_RX_AACK
598 * to TX_ON.
599 */
600 if (!force) {
601 usleep_range(c->t_frame + c->t_p_ack,
602 c->t_frame + c->t_p_ack + 1000);
603 goto change;
604 }
605 break;
606 default:
607 break;
608 }
609 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200610 /* Default value, means RESET state */
611 case STATE_P_ON:
612 switch (ctx->to_state) {
613 case STATE_TRX_OFF:
614 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
615 goto change;
616 default:
617 break;
618 }
619 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200620 default:
621 break;
622 }
623
624 /* Default delay is 1us in the most cases */
625 udelay(1);
626
627change:
Alexander Aring97fed792014-10-07 10:38:32 +0200628 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
629 at86rf230_async_state_assert,
630 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200631}
632
633static void
634at86rf230_async_state_change_start(void *context)
635{
636 struct at86rf230_state_change *ctx = context;
637 struct at86rf230_local *lp = ctx->lp;
638 u8 *buf = ctx->buf;
639 const u8 trx_state = buf[1] & 0x1f;
640 int rc;
641
642 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
643 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
644 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200645 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
646 at86rf230_async_state_change_start,
647 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200648 return;
649 }
650
651 /* Check if we already are in the state which we change in */
652 if (trx_state == ctx->to_state) {
653 if (ctx->complete)
654 ctx->complete(context);
655 return;
656 }
657
658 /* Set current state to the context of state change */
659 ctx->from_state = trx_state;
660
661 /* Going into the next step for a state change which do a timing
662 * relevant delay.
663 */
664 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
665 buf[1] = ctx->to_state;
666 ctx->trx.len = 2;
667 ctx->msg.complete = at86rf230_async_state_delay;
668 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200669 if (rc) {
670 if (ctx->irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100671 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200672
Alexander Aring4fef7d32014-12-15 10:25:55 +0100673 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200674 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000675}
676
Alexander Aring97fed792014-10-07 10:38:32 +0200677static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200678at86rf230_async_state_change(struct at86rf230_local *lp,
679 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200680 const u8 state, void (*complete)(void *context),
681 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000682{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200683 /* Initialization for the state change context */
684 ctx->to_state = state;
685 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200686 ctx->irq_enable = irq_enable;
687 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
688 at86rf230_async_state_change_start,
689 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200690}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000691
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200692static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200693at86rf230_sync_state_change_complete(void *context)
694{
695 struct at86rf230_state_change *ctx = context;
696 struct at86rf230_local *lp = ctx->lp;
697
698 complete(&lp->state_complete);
699}
700
701/* This function do a sync framework above the async state change.
702 * Some callbacks of the IEEE 802.15.4 driver interface need to be
703 * handled synchronously.
704 */
705static int
706at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
707{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100708 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200709
Alexander Aring97fed792014-10-07 10:38:32 +0200710 at86rf230_async_state_change(lp, &lp->state, state,
711 at86rf230_sync_state_change_complete,
712 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200713
714 rc = wait_for_completion_timeout(&lp->state_complete,
715 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200716 if (!rc) {
717 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200718 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200719 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200720
721 return 0;
722}
723
724static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200725at86rf230_tx_complete(void *context)
726{
727 struct at86rf230_state_change *ctx = context;
728 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000729
Alexander Aringcca990c2015-03-01 21:55:31 +0100730 enable_irq(ctx->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100731
Alexander Aringef5428a2015-03-01 21:55:29 +0100732 ieee802154_xmit_complete(lp->hw, lp->tx_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
Alexander Aring74de4c82015-03-01 21:55:30 +0100785at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200786{
Alexander Aring74de4c82015-03-01 21:55:30 +0100787 struct at86rf230_state_change *ctx = context;
788 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200789 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
Alexander Aring74de4c82015-03-01 21:55:30 +0100790 const u8 *buf = lp->irq.buf;
791 struct sk_buff *skb;
792 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200793
Alexander Aring74de4c82015-03-01 21:55:30 +0100794 len = buf[1];
795 if (!ieee802154_is_valid_psdu_len(len)) {
796 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
797 len = IEEE802154_MTU;
798 }
799 lqi = buf[2 + len];
800
801 memcpy(rx_local_buf, buf + 2, len);
Alexander Aringcca990c2015-03-01 21:55:31 +0100802 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200803
Alexander Aring61a22812014-10-27 17:13:29 +0100804 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200805 if (!skb) {
806 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
807 return;
808 }
809
810 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100811 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200812}
813
814static void
Alexander Aringcca990c2015-03-01 21:55:31 +0100815at86rf230_rx_read_frame(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200816{
Alexander Aringcca990c2015-03-01 21:55:31 +0100817 struct at86rf230_state_change *ctx = context;
818 struct at86rf230_local *lp = ctx->lp;
Alexander Aring97fed792014-10-07 10:38:32 +0200819 int rc;
820
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200821 u8 *buf = lp->irq.buf;
822
823 buf[0] = CMD_FB;
824 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
825 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200826 rc = spi_async(lp->spi, &lp->irq.msg);
827 if (rc) {
Alexander Aringcca990c2015-03-01 21:55:31 +0100828 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200829 at86rf230_async_error(lp, &lp->irq, rc);
830 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200831}
832
833static void
834at86rf230_rx_trac_check(void *context)
835{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200836 /* Possible check on trac status here. This could be useful to make
837 * some stats why receive is failed. Not used at the moment, but it's
838 * maybe timing relevant. Datasheet doesn't say anything about this.
839 * The programming guide say do it so.
840 */
841
Alexander Aringcca990c2015-03-01 21:55:31 +0100842 at86rf230_rx_read_frame(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200843}
844
Alexander Aring97fed792014-10-07 10:38:32 +0200845static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200846at86rf230_irq_trx_end(struct at86rf230_local *lp)
847{
848 spin_lock(&lp->lock);
849 if (lp->is_tx) {
850 lp->is_tx = 0;
851 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200852
853 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200854 at86rf230_async_state_change(lp, &lp->irq,
855 STATE_FORCE_TX_ON,
856 at86rf230_tx_trac_status,
857 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200858 else
Alexander Aring97fed792014-10-07 10:38:32 +0200859 at86rf230_async_state_change(lp, &lp->irq,
860 STATE_RX_AACK_ON,
861 at86rf230_tx_complete,
862 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200863 } else {
864 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200865 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
866 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200867 }
868}
869
870static void
871at86rf230_irq_status(void *context)
872{
873 struct at86rf230_state_change *ctx = context;
874 struct at86rf230_local *lp = ctx->lp;
875 const u8 *buf = lp->irq.buf;
876 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200877
878 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200879 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200880 } else {
Alexander Aringcca990c2015-03-01 21:55:31 +0100881 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200882 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
883 irq);
884 }
885}
886
887static irqreturn_t at86rf230_isr(int irq, void *data)
888{
889 struct at86rf230_local *lp = data;
890 struct at86rf230_state_change *ctx = &lp->irq;
891 u8 *buf = ctx->buf;
892 int rc;
893
Alexander Aring90566362014-10-07 10:38:29 +0200894 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200895
896 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
897 ctx->trx.len = 2;
898 ctx->msg.complete = at86rf230_irq_status;
899 rc = spi_async(lp->spi, &ctx->msg);
900 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200901 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200902 at86rf230_async_error(lp, ctx, rc);
903 return IRQ_NONE;
904 }
905
906 return IRQ_HANDLED;
907}
908
909static void
910at86rf230_write_frame_complete(void *context)
911{
912 struct at86rf230_state_change *ctx = context;
913 struct at86rf230_local *lp = ctx->lp;
914 u8 *buf = ctx->buf;
915 int rc;
916
917 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
918 buf[1] = STATE_BUSY_TX;
919 ctx->trx.len = 2;
920 ctx->msg.complete = NULL;
921 rc = spi_async(lp->spi, &ctx->msg);
922 if (rc)
923 at86rf230_async_error(lp, ctx, rc);
924}
925
926static void
927at86rf230_write_frame(void *context)
928{
929 struct at86rf230_state_change *ctx = context;
930 struct at86rf230_local *lp = ctx->lp;
931 struct sk_buff *skb = lp->tx_skb;
932 u8 *buf = lp->tx.buf;
933 int rc;
934
935 spin_lock(&lp->lock);
936 lp->is_tx = 1;
937 spin_unlock(&lp->lock);
938
939 buf[0] = CMD_FB | CMD_WRITE;
940 buf[1] = skb->len + 2;
941 memcpy(buf + 2, skb->data, skb->len);
942 lp->tx.trx.len = skb->len + 2;
943 lp->tx.msg.complete = at86rf230_write_frame_complete;
944 rc = spi_async(lp->spi, &lp->tx.msg);
945 if (rc)
946 at86rf230_async_error(lp, ctx, rc);
947}
948
949static void
950at86rf230_xmit_tx_on(void *context)
951{
952 struct at86rf230_state_change *ctx = context;
953 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200954
Alexander Aring97fed792014-10-07 10:38:32 +0200955 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
956 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200957}
958
959static int
Alexander Aring5a504392014-10-25 17:16:34 +0200960at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200961{
Alexander Aring5a504392014-10-25 17:16:34 +0200962 struct at86rf230_local *lp = hw->priv;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200963 struct at86rf230_state_change *ctx = &lp->tx;
964
965 void (*tx_complete)(void *context) = at86rf230_write_frame;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200966
967 lp->tx_skb = skb;
968
969 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
970 * are in STATE_TX_ON. The pfad differs here, so we change
971 * the complete handler.
972 */
973 if (lp->tx_aret)
974 tx_complete = at86rf230_xmit_tx_on;
975
Alexander Aringba6d2232015-03-01 21:55:28 +0100976 lp->tx_retry = 0;
Alexander Aring97fed792014-10-07 10:38:32 +0200977 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
978
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200979 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000980}
981
982static int
Alexander Aring5a504392014-10-25 17:16:34 +0200983at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000984{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000985 BUG_ON(!level);
986 *level = 0xbe;
987 return 0;
988}
989
990static int
Alexander Aring5a504392014-10-25 17:16:34 +0200991at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000992{
Alexander Aring5a504392014-10-25 17:16:34 +0200993 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000994}
995
996static void
Alexander Aring5a504392014-10-25 17:16:34 +0200997at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000998{
Alexander Aring5a504392014-10-25 17:16:34 +0200999 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001000}
1001
1002static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001003at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001004{
1005 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1006}
1007
1008static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001009at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001010{
1011 int rc;
1012
1013 if (channel == 0)
1014 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1015 else
1016 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1017 if (rc < 0)
1018 return rc;
1019
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001020 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001021 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001022 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001023 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001024 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001025 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001026 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001027 if (rc < 0)
1028 return rc;
1029
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001030 /* This sets the symbol_duration according frequency on the 212.
1031 * TODO move this handling while set channel and page in cfg802154.
1032 * We can do that, this timings are according 802.15.4 standard.
1033 * If we do that in cfg802154, this is a more generic calculation.
1034 *
1035 * This should also protected from ifs_timer. Means cancel timer and
1036 * init with a new value. For now, this is okay.
1037 */
1038 if (channel == 0) {
1039 if (page == 0) {
1040 /* SUB:0 and BPSK:0 -> BPSK-20 */
1041 lp->hw->phy->symbol_duration = 50;
1042 } else {
1043 /* SUB:1 and BPSK:0 -> BPSK-40 */
1044 lp->hw->phy->symbol_duration = 25;
1045 }
1046 } else {
1047 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001048 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001049 lp->hw->phy->symbol_duration = 40;
1050 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001051 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001052 lp->hw->phy->symbol_duration = 16;
1053 }
1054
1055 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1056 lp->hw->phy->symbol_duration;
1057 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1058 lp->hw->phy->symbol_duration;
1059
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001060 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1061}
1062
1063static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001064at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001065{
Alexander Aring5a504392014-10-25 17:16:34 +02001066 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001067 int rc;
1068
Alexander Aringa53d1f72014-07-03 00:20:46 +02001069 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001070 /* Wait for PLL */
1071 usleep_range(lp->data->t_channel_switch,
1072 lp->data->t_channel_switch + 10);
Alexander Aring820bd662014-11-12 03:36:56 +01001073 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001074}
1075
1076static int
Alexander Aring5a504392014-10-25 17:16:34 +02001077at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001078 struct ieee802154_hw_addr_filt *filt,
1079 unsigned long changed)
1080{
Alexander Aring5a504392014-10-25 17:16:34 +02001081 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001082
Alexander Aring57205c12014-10-25 05:25:09 +02001083 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001084 u16 addr = le16_to_cpu(filt->short_addr);
1085
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001086 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001087 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001088 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1089 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001090 }
1091
Alexander Aring57205c12014-10-25 05:25:09 +02001092 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001093 u16 pan = le16_to_cpu(filt->pan_id);
1094
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001095 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001096 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001097 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1098 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001099 }
1100
Alexander Aring57205c12014-10-25 05:25:09 +02001101 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001102 u8 i, addr[8];
1103
1104 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001105 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001106 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001107 for (i = 0; i < 8; i++)
1108 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001109 }
1110
Alexander Aring57205c12014-10-25 05:25:09 +02001111 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
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 panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001114 if (filt->pan_coord)
1115 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1116 else
1117 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1118 }
1119
1120 return 0;
1121}
1122
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001123static int
Alexander Aring5a504392014-10-25 17:16:34 +02001124at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001125{
Alexander Aring5a504392014-10-25 17:16:34 +02001126 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001127
1128 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1129 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1130 * 0dB.
1131 * thus, supported values for db range from -26 to 5, for 31dB of
1132 * reduction to 0dB of reduction.
1133 */
1134 if (db > 5 || db < -26)
1135 return -EINVAL;
1136
1137 db = -(db - 5);
1138
Jean Sacren677676c2014-03-01 15:54:36 -07001139 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001140}
1141
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001142static int
Alexander Aring5a504392014-10-25 17:16:34 +02001143at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001144{
Alexander Aring5a504392014-10-25 17:16:34 +02001145 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001146
1147 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1148}
1149
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001150static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001151at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1152 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001153{
Alexander Aring5a504392014-10-25 17:16:34 +02001154 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001155 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001156
Alexander Aring7fe9a382014-12-10 15:33:12 +01001157 /* mapping 802.15.4 to driver spec */
1158 switch (cca->mode) {
1159 case NL802154_CCA_ENERGY:
1160 val = 1;
1161 break;
1162 case NL802154_CCA_CARRIER:
1163 val = 2;
1164 break;
1165 case NL802154_CCA_ENERGY_CARRIER:
1166 switch (cca->opt) {
1167 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1168 val = 3;
1169 break;
1170 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1171 val = 0;
1172 break;
1173 default:
1174 return -EINVAL;
1175 }
1176 break;
1177 default:
1178 return -EINVAL;
1179 }
1180
1181 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001182}
1183
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001184static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001185at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1186{
1187 return (level - lp->data->rssi_base_val) * 100 / 207;
1188}
1189
1190static int
1191at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1192{
1193 return (level - lp->data->rssi_base_val) / 2;
1194}
1195
1196static int
Alexander Aring5a504392014-10-25 17:16:34 +02001197at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001198{
Alexander Aring5a504392014-10-25 17:16:34 +02001199 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001200
Alexander Aringa53d1f72014-07-03 00:20:46 +02001201 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001202 return -EINVAL;
1203
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001204 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1205 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001206}
1207
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001208static int
Alexander Aring5a504392014-10-25 17:16:34 +02001209at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001210 u8 retries)
1211{
Alexander Aring5a504392014-10-25 17:16:34 +02001212 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001213 int rc;
1214
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001215 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1216 if (rc)
1217 return rc;
1218
1219 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1220 if (rc)
1221 return rc;
1222
Alexander Aring39d7f322014-04-05 13:49:26 +02001223 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001224}
1225
1226static int
Alexander Aring5a504392014-10-25 17:16:34 +02001227at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001228{
Alexander Aring5a504392014-10-25 17:16:34 +02001229 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001230 int rc = 0;
1231
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001232 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001233 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001234
1235 if (retries >= 0)
1236 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1237
1238 return rc;
1239}
1240
Alexander Aring92f45f52014-10-29 21:34:33 +01001241static int
1242at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1243{
1244 struct at86rf230_local *lp = hw->priv;
1245 int rc;
1246
1247 if (on) {
1248 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1249 if (rc < 0)
1250 return rc;
1251
1252 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1253 if (rc < 0)
1254 return rc;
1255 } else {
1256 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1257 if (rc < 0)
1258 return rc;
1259
1260 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1261 if (rc < 0)
1262 return rc;
1263 }
1264
1265 return 0;
1266}
1267
Alexander Aring16301862014-10-28 18:21:18 +01001268static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001269 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001270 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001271 .ed = at86rf230_ed,
1272 .set_channel = at86rf230_channel,
1273 .start = at86rf230_start,
1274 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001275 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001276 .set_txpower = at86rf230_set_txpower,
1277 .set_lbt = at86rf230_set_lbt,
1278 .set_cca_mode = at86rf230_set_cca_mode,
1279 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1280 .set_csma_params = at86rf230_set_csma_params,
1281 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f52014-10-29 21:34:33 +01001282 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001283};
1284
Alexander Aringa53d1f72014-07-03 00:20:46 +02001285static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001286 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001287 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001288 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001289 .t_off_to_aack = 80,
1290 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001291 .t_frame = 4096,
1292 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001293 .rssi_base_val = -91,
1294 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001295 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001296};
1297
1298static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001299 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001300 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001301 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001302 .t_off_to_aack = 110,
1303 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001304 .t_frame = 4096,
1305 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001306 .rssi_base_val = -91,
1307 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001308 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001309};
1310
1311static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001312 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001313 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001314 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001315 .t_off_to_aack = 200,
1316 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001317 .t_frame = 4096,
1318 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001319 .rssi_base_val = -100,
1320 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001321 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001322};
1323
Alexander Aringccdaeb22015-02-27 09:58:26 +01001324static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001325{
Alexander Aring1db05582014-07-03 00:20:50 +02001326 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001327 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001328 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001329
Alexander Aring09e536c2014-07-03 00:20:52 +02001330 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001331 if (rc)
1332 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001333
Alexander Aring4af619a2014-04-24 19:09:05 +02001334 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001335 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1336 irq_type == IRQ_TYPE_EDGE_FALLING)
1337 dev_warn(&lp->spi->dev,
1338 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001339 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1340 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001341 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001342
Alexander Aring18c65042014-04-24 19:09:18 +02001343 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001344 if (rc)
1345 return rc;
1346
Alexander Aring6bd2b132014-07-03 00:20:49 +02001347 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1348 if (rc)
1349 return rc;
1350
Sascha Herrmann057dad62013-04-14 22:33:29 +00001351 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001352 if (rc)
1353 return rc;
1354
Alexander Aringbe64f072015-02-27 09:58:28 +01001355 /* reset values differs in at86rf231 and at86rf233 */
1356 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1357 if (rc)
1358 return rc;
1359
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001360 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1361 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1362 if (rc)
1363 return rc;
1364 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1365 if (rc)
1366 return rc;
1367
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001368 /* CLKM changes are applied immediately */
1369 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1370 if (rc)
1371 return rc;
1372
1373 /* Turn CLKM Off */
1374 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1375 if (rc)
1376 return rc;
1377 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001378 usleep_range(lp->data->t_sleep_cycle,
1379 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001380
Alexander Aringccdaeb22015-02-27 09:58:26 +01001381 /* xtal_trim value is calculated by:
1382 * CL = 0.5 * (CX + CTRIM + CPAR)
1383 *
1384 * whereas:
1385 * CL = capacitor of used crystal
1386 * CX = connected capacitors at xtal pins
1387 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1388 * but this is different on each board setup. You need to fine
1389 * tuning this value via CTRIM.
1390 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1391 * 0 pF upto 4.5 pF.
1392 *
1393 * Examples:
1394 * atben transceiver:
1395 *
1396 * CL = 8 pF
1397 * CX = 12 pF
1398 * CPAR = 3 pF (We assume the magic constant from datasheet)
1399 * CTRIM = 0.9 pF
1400 *
1401 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1402 *
1403 * xtal_trim = 0x3
1404 *
1405 * openlabs transceiver:
1406 *
1407 * CL = 16 pF
1408 * CX = 22 pF
1409 * CPAR = 3 pF (We assume the magic constant from datasheet)
1410 * CTRIM = 4.5 pF
1411 *
1412 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1413 *
1414 * xtal_trim = 0xf
1415 */
1416 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1417 if (rc)
1418 return rc;
1419
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001420 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001421 if (rc)
1422 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001423 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001424 dev_err(&lp->spi->dev, "DVDD error\n");
1425 return -EINVAL;
1426 }
1427
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001428 /* Force setting slotted operation bit to 0. Sometimes the atben
1429 * sets this bit and I don't know why. We set this always force
1430 * to zero while probing.
1431 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001432 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001433}
1434
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001435static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001436at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1437 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001438{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001439 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001440 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001441
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001442 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1443 if (!pdata)
1444 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001445
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001446 *rstn = pdata->rstn;
1447 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001448 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001449 return 0;
1450 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001451
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001452 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1453 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001454 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1455 if (ret < 0 && ret != -EINVAL)
1456 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001457
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001458 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001459}
1460
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001461static int
1462at86rf230_detect_device(struct at86rf230_local *lp)
1463{
1464 unsigned int part, version, val;
1465 u16 man_id = 0;
1466 const char *chip;
1467 int rc;
1468
1469 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1470 if (rc)
1471 return rc;
1472 man_id |= val;
1473
1474 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1475 if (rc)
1476 return rc;
1477 man_id |= (val << 8);
1478
1479 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1480 if (rc)
1481 return rc;
1482
Andrey Yurovsky75989682014-12-17 13:14:42 -08001483 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001484 if (rc)
1485 return rc;
1486
1487 if (man_id != 0x001f) {
1488 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1489 man_id >> 8, man_id & 0xFF);
1490 return -EINVAL;
1491 }
1492
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001493 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8fc84e2014-10-29 21:34:31 +01001494 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
Alexander Aring92f45f52014-10-29 21:34:33 +01001495 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001496
Alexander Aringb48a7c12014-12-10 15:33:14 +01001497 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1498
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001499 switch (part) {
1500 case 2:
1501 chip = "at86rf230";
1502 rc = -ENOTSUPP;
1503 break;
1504 case 3:
1505 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001506 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001507 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001508 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001509 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001510 break;
1511 case 7:
1512 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001513 lp->data = &at86rf212_data;
1514 lp->hw->flags |= IEEE802154_HW_LBT;
1515 lp->hw->phy->channels_supported[0] = 0x00007FF;
1516 lp->hw->phy->channels_supported[2] = 0x00007FF;
1517 lp->hw->phy->current_channel = 5;
1518 lp->hw->phy->symbol_duration = 25;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001519 break;
1520 case 11:
1521 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001522 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001523 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001524 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001525 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001526 break;
1527 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001528 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001529 rc = -ENOTSUPP;
1530 break;
1531 }
1532
1533 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1534
1535 return rc;
1536}
1537
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001538static void
1539at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1540{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001541 lp->state.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001542 lp->state.irq = lp->spi->irq;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001543 spi_message_init(&lp->state.msg);
1544 lp->state.msg.context = &lp->state;
1545 lp->state.trx.tx_buf = lp->state.buf;
1546 lp->state.trx.rx_buf = lp->state.buf;
1547 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1548
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001549 lp->irq.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001550 lp->irq.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001551 spi_message_init(&lp->irq.msg);
1552 lp->irq.msg.context = &lp->irq;
1553 lp->irq.trx.tx_buf = lp->irq.buf;
1554 lp->irq.trx.rx_buf = lp->irq.buf;
1555 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1556
1557 lp->tx.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001558 lp->tx.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001559 spi_message_init(&lp->tx.msg);
1560 lp->tx.msg.context = &lp->tx;
1561 lp->tx.trx.tx_buf = lp->tx.buf;
1562 lp->tx.trx.rx_buf = lp->tx.buf;
1563 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1564}
1565
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001566static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001567{
Alexander Aring5a504392014-10-25 17:16:34 +02001568 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001569 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001570 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001571 int rc, irq_type, rstn, slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001572 u8 xtal_trim;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001573
1574 if (!spi->irq) {
1575 dev_err(&spi->dev, "no IRQ specified\n");
1576 return -EINVAL;
1577 }
1578
Alexander Aringccdaeb22015-02-27 09:58:26 +01001579 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001580 if (rc < 0) {
1581 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1582 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001583 }
1584
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001585 if (gpio_is_valid(rstn)) {
1586 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001587 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001588 if (rc)
1589 return rc;
1590 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001591
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001592 if (gpio_is_valid(slp_tr)) {
1593 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001594 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001595 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001596 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001597 }
1598
1599 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001600 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001601 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001602 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001603 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001604 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001605 usleep_range(120, 240);
1606 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001607
Alexander Aring5a504392014-10-25 17:16:34 +02001608 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1609 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001610 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001611
Alexander Aring5a504392014-10-25 17:16:34 +02001612 lp = hw->priv;
1613 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001614 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001615 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001616 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001617 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001618
Alexander Aringf76014f772014-07-03 00:20:44 +02001619 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1620 if (IS_ERR(lp->regmap)) {
1621 rc = PTR_ERR(lp->regmap);
1622 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1623 rc);
1624 goto free_dev;
1625 }
1626
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001627 at86rf230_setup_spi_messages(lp);
1628
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001629 rc = at86rf230_detect_device(lp);
1630 if (rc < 0)
1631 goto free_dev;
1632
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001633 spin_lock_init(&lp->lock);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001634 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001635
1636 spi_set_drvdata(spi, lp);
1637
Alexander Aringccdaeb22015-02-27 09:58:26 +01001638 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001639 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001640 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001641
Alexander Aring19626942014-04-24 19:09:15 +02001642 /* Read irq status register to reset irq line */
1643 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001644 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001645 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001646
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001647 irq_type = irq_get_trigger_type(spi->irq);
1648 if (!irq_type)
1649 irq_type = IRQF_TRIGGER_RISING;
1650
1651 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1652 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001653 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001654 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001655
Alexander Aring5a504392014-10-25 17:16:34 +02001656 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001657 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001658 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001659
1660 return rc;
1661
Alexander Aring640985e2014-07-03 00:20:43 +02001662free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001663 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001664
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001665 return rc;
1666}
1667
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001668static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001669{
1670 struct at86rf230_local *lp = spi_get_drvdata(spi);
1671
Alexander Aring17e84a92014-03-31 03:26:51 +02001672 /* mask all at86rf230 irq's */
1673 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001674 ieee802154_unregister_hw(lp->hw);
1675 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001676 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001677
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001678 return 0;
1679}
1680
Alexander Aring1086b4f2014-04-24 19:09:11 +02001681static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001682 { .compatible = "atmel,at86rf230", },
1683 { .compatible = "atmel,at86rf231", },
1684 { .compatible = "atmel,at86rf233", },
1685 { .compatible = "atmel,at86rf212", },
1686 { },
1687};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001688MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001689
Alexander Aring90b15522014-04-24 19:09:12 +02001690static const struct spi_device_id at86rf230_device_id[] = {
1691 { .name = "at86rf230", },
1692 { .name = "at86rf231", },
1693 { .name = "at86rf233", },
1694 { .name = "at86rf212", },
1695 { },
1696};
1697MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1698
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001699static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001700 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001701 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001702 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001703 .name = "at86rf230",
1704 .owner = THIS_MODULE,
1705 },
1706 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001707 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001708};
1709
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001710module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001711
1712MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1713MODULE_LICENSE("GPL v2");