blob: 492fb7e7675d56415e9a31557903f62591f85ba6 [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 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22 */
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020026#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000027#include <linux/gpio.h>
28#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000029#include <linux/spinlock.h>
30#include <linux/spi/spi.h>
31#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020032#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000033#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010034#include <linux/of_gpio.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000035
Alexander Aring1d15d6b2014-07-03 00:20:48 +020036#include <net/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000037#include <net/mac802154.h>
38#include <net/wpan-phy.h>
39
Alexander Aringa53d1f72014-07-03 00:20:46 +020040struct at86rf230_local;
41/* at86rf2xx chip depend data.
42 * All timings are in us.
43 */
44struct at86rf2xx_chip_data {
Alexander Aring1d15d6b2014-07-03 00:20:48 +020045 u16 t_frame;
46 u16 t_p_ack;
47 /* short interframe spacing time */
48 u16 t_sifs;
49 /* long interframe spacing time */
50 u16 t_lifs;
51 /* completion timeout for tx in msecs */
52 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020053 int rssi_base_val;
54
55 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020056 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020057};
58
Alexander Aring1d15d6b2014-07-03 00:20:48 +020059#define AT86RF2XX_MAX_BUF (127 + 3)
60
61struct at86rf230_state_change {
62 struct at86rf230_local *lp;
63
64 struct spi_message msg;
65 struct spi_transfer trx;
66 u8 buf[AT86RF2XX_MAX_BUF];
67
68 void (*complete)(void *context);
69 u8 from_state;
70 u8 to_state;
71};
72
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000073struct at86rf230_local {
74 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000075
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000076 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020077 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020078 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
Alexander Aring1d15d6b2014-07-03 00:20:48 +020080 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020081
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010082 bool tx_aret;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020083 bool is_tx;
84 /* spinlock for is_tx protection */
85 spinlock_t lock;
86 struct completion tx_complete;
87 struct sk_buff *tx_skb;
88 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000089};
90
91#define RG_TRX_STATUS (0x01)
92#define SR_TRX_STATUS 0x01, 0x1f, 0
93#define SR_RESERVED_01_3 0x01, 0x20, 5
94#define SR_CCA_STATUS 0x01, 0x40, 6
95#define SR_CCA_DONE 0x01, 0x80, 7
96#define RG_TRX_STATE (0x02)
97#define SR_TRX_CMD 0x02, 0x1f, 0
98#define SR_TRAC_STATUS 0x02, 0xe0, 5
99#define RG_TRX_CTRL_0 (0x03)
100#define SR_CLKM_CTRL 0x03, 0x07, 0
101#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
102#define SR_PAD_IO_CLKM 0x03, 0x30, 4
103#define SR_PAD_IO 0x03, 0xc0, 6
104#define RG_TRX_CTRL_1 (0x04)
105#define SR_IRQ_POLARITY 0x04, 0x01, 0
106#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
107#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
108#define SR_RX_BL_CTRL 0x04, 0x10, 4
109#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
110#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
111#define SR_PA_EXT_EN 0x04, 0x80, 7
112#define RG_PHY_TX_PWR (0x05)
113#define SR_TX_PWR 0x05, 0x0f, 0
114#define SR_PA_LT 0x05, 0x30, 4
115#define SR_PA_BUF_LT 0x05, 0xc0, 6
116#define RG_PHY_RSSI (0x06)
117#define SR_RSSI 0x06, 0x1f, 0
118#define SR_RND_VALUE 0x06, 0x60, 5
119#define SR_RX_CRC_VALID 0x06, 0x80, 7
120#define RG_PHY_ED_LEVEL (0x07)
121#define SR_ED_LEVEL 0x07, 0xff, 0
122#define RG_PHY_CC_CCA (0x08)
123#define SR_CHANNEL 0x08, 0x1f, 0
124#define SR_CCA_MODE 0x08, 0x60, 5
125#define SR_CCA_REQUEST 0x08, 0x80, 7
126#define RG_CCA_THRES (0x09)
127#define SR_CCA_ED_THRES 0x09, 0x0f, 0
128#define SR_RESERVED_09_1 0x09, 0xf0, 4
129#define RG_RX_CTRL (0x0a)
130#define SR_PDT_THRES 0x0a, 0x0f, 0
131#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
132#define RG_SFD_VALUE (0x0b)
133#define SR_SFD_VALUE 0x0b, 0xff, 0
134#define RG_TRX_CTRL_2 (0x0c)
135#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100136#define SR_SUB_MODE 0x0c, 0x04, 2
137#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100138#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
139#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000140#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
141#define RG_ANT_DIV (0x0d)
142#define SR_ANT_CTRL 0x0d, 0x03, 0
143#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
144#define SR_ANT_DIV_EN 0x0d, 0x08, 3
145#define SR_RESERVED_0d_2 0x0d, 0x70, 4
146#define SR_ANT_SEL 0x0d, 0x80, 7
147#define RG_IRQ_MASK (0x0e)
148#define SR_IRQ_MASK 0x0e, 0xff, 0
149#define RG_IRQ_STATUS (0x0f)
150#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
151#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
152#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
153#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
154#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
155#define SR_IRQ_5_AMI 0x0f, 0x20, 5
156#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
157#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
158#define RG_VREG_CTRL (0x10)
159#define SR_RESERVED_10_6 0x10, 0x03, 0
160#define SR_DVDD_OK 0x10, 0x04, 2
161#define SR_DVREG_EXT 0x10, 0x08, 3
162#define SR_RESERVED_10_3 0x10, 0x30, 4
163#define SR_AVDD_OK 0x10, 0x40, 6
164#define SR_AVREG_EXT 0x10, 0x80, 7
165#define RG_BATMON (0x11)
166#define SR_BATMON_VTH 0x11, 0x0f, 0
167#define SR_BATMON_HR 0x11, 0x10, 4
168#define SR_BATMON_OK 0x11, 0x20, 5
169#define SR_RESERVED_11_1 0x11, 0xc0, 6
170#define RG_XOSC_CTRL (0x12)
171#define SR_XTAL_TRIM 0x12, 0x0f, 0
172#define SR_XTAL_MODE 0x12, 0xf0, 4
173#define RG_RX_SYN (0x15)
174#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
175#define SR_RESERVED_15_2 0x15, 0x70, 4
176#define SR_RX_PDT_DIS 0x15, 0x80, 7
177#define RG_XAH_CTRL_1 (0x17)
178#define SR_RESERVED_17_8 0x17, 0x01, 0
179#define SR_AACK_PROM_MODE 0x17, 0x02, 1
180#define SR_AACK_ACK_TIME 0x17, 0x04, 2
181#define SR_RESERVED_17_5 0x17, 0x08, 3
182#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
183#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100184#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000185#define SR_RESERVED_17_1 0x17, 0x80, 7
186#define RG_FTN_CTRL (0x18)
187#define SR_RESERVED_18_2 0x18, 0x7f, 0
188#define SR_FTN_START 0x18, 0x80, 7
189#define RG_PLL_CF (0x1a)
190#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
191#define SR_PLL_CF_START 0x1a, 0x80, 7
192#define RG_PLL_DCU (0x1b)
193#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
194#define SR_RESERVED_1b_2 0x1b, 0x40, 6
195#define SR_PLL_DCU_START 0x1b, 0x80, 7
196#define RG_PART_NUM (0x1c)
197#define SR_PART_NUM 0x1c, 0xff, 0
198#define RG_VERSION_NUM (0x1d)
199#define SR_VERSION_NUM 0x1d, 0xff, 0
200#define RG_MAN_ID_0 (0x1e)
201#define SR_MAN_ID_0 0x1e, 0xff, 0
202#define RG_MAN_ID_1 (0x1f)
203#define SR_MAN_ID_1 0x1f, 0xff, 0
204#define RG_SHORT_ADDR_0 (0x20)
205#define SR_SHORT_ADDR_0 0x20, 0xff, 0
206#define RG_SHORT_ADDR_1 (0x21)
207#define SR_SHORT_ADDR_1 0x21, 0xff, 0
208#define RG_PAN_ID_0 (0x22)
209#define SR_PAN_ID_0 0x22, 0xff, 0
210#define RG_PAN_ID_1 (0x23)
211#define SR_PAN_ID_1 0x23, 0xff, 0
212#define RG_IEEE_ADDR_0 (0x24)
213#define SR_IEEE_ADDR_0 0x24, 0xff, 0
214#define RG_IEEE_ADDR_1 (0x25)
215#define SR_IEEE_ADDR_1 0x25, 0xff, 0
216#define RG_IEEE_ADDR_2 (0x26)
217#define SR_IEEE_ADDR_2 0x26, 0xff, 0
218#define RG_IEEE_ADDR_3 (0x27)
219#define SR_IEEE_ADDR_3 0x27, 0xff, 0
220#define RG_IEEE_ADDR_4 (0x28)
221#define SR_IEEE_ADDR_4 0x28, 0xff, 0
222#define RG_IEEE_ADDR_5 (0x29)
223#define SR_IEEE_ADDR_5 0x29, 0xff, 0
224#define RG_IEEE_ADDR_6 (0x2a)
225#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
226#define RG_IEEE_ADDR_7 (0x2b)
227#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
228#define RG_XAH_CTRL_0 (0x2c)
229#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
230#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
231#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
232#define RG_CSMA_SEED_0 (0x2d)
233#define SR_CSMA_SEED_0 0x2d, 0xff, 0
234#define RG_CSMA_SEED_1 (0x2e)
235#define SR_CSMA_SEED_1 0x2e, 0x07, 0
236#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
237#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
238#define SR_AACK_SET_PD 0x2e, 0x20, 5
239#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
240#define RG_CSMA_BE (0x2f)
241#define SR_MIN_BE 0x2f, 0x0f, 0
242#define SR_MAX_BE 0x2f, 0xf0, 4
243
244#define CMD_REG 0x80
245#define CMD_REG_MASK 0x3f
246#define CMD_WRITE 0x40
247#define CMD_FB 0x20
248
249#define IRQ_BAT_LOW (1 << 7)
250#define IRQ_TRX_UR (1 << 6)
251#define IRQ_AMI (1 << 5)
252#define IRQ_CCA_ED (1 << 4)
253#define IRQ_TRX_END (1 << 3)
254#define IRQ_RX_START (1 << 2)
255#define IRQ_PLL_UNL (1 << 1)
256#define IRQ_PLL_LOCK (1 << 0)
257
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000258#define IRQ_ACTIVE_HIGH 0
259#define IRQ_ACTIVE_LOW 1
260
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000261#define STATE_P_ON 0x00 /* BUSY */
262#define STATE_BUSY_RX 0x01
263#define STATE_BUSY_TX 0x02
264#define STATE_FORCE_TRX_OFF 0x03
265#define STATE_FORCE_TX_ON 0x04 /* IDLE */
266/* 0x05 */ /* INVALID_PARAMETER */
267#define STATE_RX_ON 0x06
268/* 0x07 */ /* SUCCESS */
269#define STATE_TRX_OFF 0x08
270#define STATE_TX_ON 0x09
271/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
272#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500273#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000274#define STATE_BUSY_RX_AACK 0x11
275#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000276#define STATE_RX_AACK_ON 0x16
277#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000278#define STATE_RX_ON_NOCLK 0x1C
279#define STATE_RX_AACK_ON_NOCLK 0x1D
280#define STATE_BUSY_RX_AACK_NOCLK 0x1E
281#define STATE_TRANSITION_IN_PROGRESS 0x1F
282
Alexander Aringf76014f772014-07-03 00:20:44 +0200283#define AT86RF2XX_NUMREGS 0x3F
284
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200285static int
286at86rf230_async_state_change(struct at86rf230_local *lp,
287 struct at86rf230_state_change *ctx,
288 const u8 state, void (*complete)(void *context));
289
Alexander Aringf76014f772014-07-03 00:20:44 +0200290static inline int
291__at86rf230_write(struct at86rf230_local *lp,
292 unsigned int addr, unsigned int data)
293{
294 return regmap_write(lp->regmap, addr, data);
295}
296
297static inline int
298__at86rf230_read(struct at86rf230_local *lp,
299 unsigned int addr, unsigned int *data)
300{
301 return regmap_read(lp->regmap, addr, data);
302}
303
304static inline int
305at86rf230_read_subreg(struct at86rf230_local *lp,
306 unsigned int addr, unsigned int mask,
307 unsigned int shift, unsigned int *data)
308{
309 int rc;
310
311 rc = __at86rf230_read(lp, addr, data);
312 if (rc > 0)
313 *data = (*data & mask) >> shift;
314
315 return rc;
316}
317
318static inline int
319at86rf230_write_subreg(struct at86rf230_local *lp,
320 unsigned int addr, unsigned int mask,
321 unsigned int shift, unsigned int data)
322{
323 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
324}
325
326static bool
327at86rf230_reg_writeable(struct device *dev, unsigned int reg)
328{
329 switch (reg) {
330 case RG_TRX_STATE:
331 case RG_TRX_CTRL_0:
332 case RG_TRX_CTRL_1:
333 case RG_PHY_TX_PWR:
334 case RG_PHY_ED_LEVEL:
335 case RG_PHY_CC_CCA:
336 case RG_CCA_THRES:
337 case RG_RX_CTRL:
338 case RG_SFD_VALUE:
339 case RG_TRX_CTRL_2:
340 case RG_ANT_DIV:
341 case RG_IRQ_MASK:
342 case RG_VREG_CTRL:
343 case RG_BATMON:
344 case RG_XOSC_CTRL:
345 case RG_RX_SYN:
346 case RG_XAH_CTRL_1:
347 case RG_FTN_CTRL:
348 case RG_PLL_CF:
349 case RG_PLL_DCU:
350 case RG_SHORT_ADDR_0:
351 case RG_SHORT_ADDR_1:
352 case RG_PAN_ID_0:
353 case RG_PAN_ID_1:
354 case RG_IEEE_ADDR_0:
355 case RG_IEEE_ADDR_1:
356 case RG_IEEE_ADDR_2:
357 case RG_IEEE_ADDR_3:
358 case RG_IEEE_ADDR_4:
359 case RG_IEEE_ADDR_5:
360 case RG_IEEE_ADDR_6:
361 case RG_IEEE_ADDR_7:
362 case RG_XAH_CTRL_0:
363 case RG_CSMA_SEED_0:
364 case RG_CSMA_SEED_1:
365 case RG_CSMA_BE:
366 return true;
367 default:
368 return false;
369 }
370}
371
372static bool
373at86rf230_reg_readable(struct device *dev, unsigned int reg)
374{
375 bool rc;
376
377 /* all writeable are also readable */
378 rc = at86rf230_reg_writeable(dev, reg);
379 if (rc)
380 return rc;
381
382 /* readonly regs */
383 switch (reg) {
384 case RG_TRX_STATUS:
385 case RG_PHY_RSSI:
386 case RG_IRQ_STATUS:
387 case RG_PART_NUM:
388 case RG_VERSION_NUM:
389 case RG_MAN_ID_1:
390 case RG_MAN_ID_0:
391 return true;
392 default:
393 return false;
394 }
395}
396
397static bool
398at86rf230_reg_volatile(struct device *dev, unsigned int reg)
399{
400 /* can be changed during runtime */
401 switch (reg) {
402 case RG_TRX_STATUS:
403 case RG_TRX_STATE:
404 case RG_PHY_RSSI:
405 case RG_PHY_ED_LEVEL:
406 case RG_IRQ_STATUS:
407 case RG_VREG_CTRL:
408 return true;
409 default:
410 return false;
411 }
412}
413
414static bool
415at86rf230_reg_precious(struct device *dev, unsigned int reg)
416{
417 /* don't clear irq line on read */
418 switch (reg) {
419 case RG_IRQ_STATUS:
420 return true;
421 default:
422 return false;
423 }
424}
425
426static struct regmap_config at86rf230_regmap_spi_config = {
427 .reg_bits = 8,
428 .val_bits = 8,
429 .write_flag_mask = CMD_REG | CMD_WRITE,
430 .read_flag_mask = CMD_REG,
431 .cache_type = REGCACHE_RBTREE,
432 .max_register = AT86RF2XX_NUMREGS,
433 .writeable_reg = at86rf230_reg_writeable,
434 .readable_reg = at86rf230_reg_readable,
435 .volatile_reg = at86rf230_reg_volatile,
436 .precious_reg = at86rf230_reg_precious,
437};
438
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200439static void
440at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000441{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200442 struct at86rf230_state_change *ctx = context;
443 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000444
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200445 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL);
446}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000447
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200448static void
449at86rf230_async_error(struct at86rf230_local *lp,
450 struct at86rf230_state_change *ctx, int rc)
451{
452 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000453
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200454 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
455 at86rf230_async_error_recover);
456}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000457
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200458/* Generic function to get some register value in async mode */
459static int
460at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
461 struct at86rf230_state_change *ctx,
462 void (*complete)(void *context))
463{
464 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000465
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200466 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
467 ctx->trx.len = 2;
468 ctx->msg.complete = complete;
469 return spi_async(lp->spi, &ctx->msg);
470}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000471
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200472static void
473at86rf230_async_state_assert(void *context)
474{
475 struct at86rf230_state_change *ctx = context;
476 struct at86rf230_local *lp = ctx->lp;
477 const u8 *buf = ctx->buf;
478 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000479
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200480 /* Assert state change */
481 if (trx_state != ctx->to_state) {
482 /* Special handling if transceiver state is in
483 * STATE_BUSY_RX_AACK and a SHR was detected.
484 */
485 if (trx_state == STATE_BUSY_RX_AACK) {
486 /* Undocumented race condition. If we send a state
487 * change to STATE_RX_AACK_ON the transceiver could
488 * change his state automatically to STATE_BUSY_RX_AACK
489 * if a SHR was detected. This is not an error, but we
490 * can't assert this.
491 */
492 if (ctx->to_state == STATE_RX_AACK_ON)
493 goto done;
494
495 /* If we change to STATE_TX_ON without forcing and
496 * transceiver state is STATE_BUSY_RX_AACK, we wait
497 * 'tFrame + tPAck' receiving time. In this time the
498 * PDU should be received. If the transceiver is still
499 * in STATE_BUSY_RX_AACK, we run a force state change
500 * to STATE_TX_ON. This is a timeout handling, if the
501 * transceiver stucks in STATE_BUSY_RX_AACK.
502 */
503 if (ctx->to_state == STATE_TX_ON) {
504 at86rf230_async_state_change(lp, ctx,
505 STATE_FORCE_TX_ON,
506 ctx->complete);
507 return;
508 }
509 }
510
511
512 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
513 ctx->from_state, ctx->to_state, trx_state);
514 }
515
516done:
517 if (ctx->complete)
518 ctx->complete(context);
519}
520
521/* Do state change timing delay. */
522static void
523at86rf230_async_state_delay(void *context)
524{
525 struct at86rf230_state_change *ctx = context;
526 struct at86rf230_local *lp = ctx->lp;
527 struct at86rf2xx_chip_data *c = lp->data;
528 bool force = false;
529 int rc;
530
531 /* The force state changes are will show as normal states in the
532 * state status subregister. We change the to_state to the
533 * corresponding one and remember if it was a force change, this
534 * differs if we do a state change from STATE_BUSY_RX_AACK.
535 */
536 switch (ctx->to_state) {
537 case STATE_FORCE_TX_ON:
538 ctx->to_state = STATE_TX_ON;
539 force = true;
540 break;
541 case STATE_FORCE_TRX_OFF:
542 ctx->to_state = STATE_TRX_OFF;
543 force = true;
544 break;
545 default:
546 break;
547 }
548
549 switch (ctx->from_state) {
550 case STATE_BUSY_RX_AACK:
551 switch (ctx->to_state) {
552 case STATE_TX_ON:
553 /* Wait for worst case receiving time if we
554 * didn't make a force change from BUSY_RX_AACK
555 * to TX_ON.
556 */
557 if (!force) {
558 usleep_range(c->t_frame + c->t_p_ack,
559 c->t_frame + c->t_p_ack + 1000);
560 goto change;
561 }
562 break;
563 default:
564 break;
565 }
566 break;
567 default:
568 break;
569 }
570
571 /* Default delay is 1us in the most cases */
572 udelay(1);
573
574change:
575 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
576 at86rf230_async_state_assert);
577 if (rc)
578 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
579}
580
581static void
582at86rf230_async_state_change_start(void *context)
583{
584 struct at86rf230_state_change *ctx = context;
585 struct at86rf230_local *lp = ctx->lp;
586 u8 *buf = ctx->buf;
587 const u8 trx_state = buf[1] & 0x1f;
588 int rc;
589
590 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
591 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
592 udelay(1);
593 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
594 at86rf230_async_state_change_start);
595 if (rc)
596 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
597 return;
598 }
599
600 /* Check if we already are in the state which we change in */
601 if (trx_state == ctx->to_state) {
602 if (ctx->complete)
603 ctx->complete(context);
604 return;
605 }
606
607 /* Set current state to the context of state change */
608 ctx->from_state = trx_state;
609
610 /* Going into the next step for a state change which do a timing
611 * relevant delay.
612 */
613 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
614 buf[1] = ctx->to_state;
615 ctx->trx.len = 2;
616 ctx->msg.complete = at86rf230_async_state_delay;
617 rc = spi_async(lp->spi, &ctx->msg);
618 if (rc)
619 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000620}
621
622static int
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200623at86rf230_async_state_change(struct at86rf230_local *lp,
624 struct at86rf230_state_change *ctx,
625 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000626{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200627 /* Initialization for the state change context */
628 ctx->to_state = state;
629 ctx->complete = complete;
630 return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
631 at86rf230_async_state_change_start);
632}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000633
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200634static void
635at86rf230_tx_complete(void *context)
636{
637 struct at86rf230_state_change *ctx = context;
638 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000639
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200640 complete(&lp->tx_complete);
641}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000642
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200643static void
644at86rf230_tx_on(void *context)
645{
646 struct at86rf230_state_change *ctx = context;
647 struct at86rf230_local *lp = ctx->lp;
648 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000649
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200650 rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
651 at86rf230_tx_complete);
652 if (rc)
653 at86rf230_async_error(lp, ctx, rc);
654}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000655
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200656static void
657at86rf230_tx_trac_error(void *context)
658{
659 struct at86rf230_state_change *ctx = context;
660 struct at86rf230_local *lp = ctx->lp;
661 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000662
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200663 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
664 at86rf230_tx_on);
665 if (rc)
666 at86rf230_async_error(lp, ctx, rc);
667}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000668
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200669static void
670at86rf230_tx_trac_check(void *context)
671{
672 struct at86rf230_state_change *ctx = context;
673 struct at86rf230_local *lp = ctx->lp;
674 const u8 *buf = ctx->buf;
675 const u8 trac = (buf[1] & 0xe0) >> 5;
676 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000677
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200678 /* If trac status is different than zero we need to do a state change
679 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
680 * state to TX_ON.
681 */
682 if (trac) {
683 rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
684 at86rf230_tx_trac_error);
685 if (rc)
686 at86rf230_async_error(lp, ctx, rc);
687 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000688 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000689
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200690 at86rf230_tx_on(context);
691}
692
693
694static void
695at86rf230_tx_trac_status(void *context)
696{
697 struct at86rf230_state_change *ctx = context;
698 struct at86rf230_local *lp = ctx->lp;
699 int rc;
700
701 rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
702 at86rf230_tx_trac_check);
703 if (rc)
704 at86rf230_async_error(lp, ctx, rc);
705}
706
707static void
708at86rf230_rx(struct at86rf230_local *lp,
709 const u8 *data, u8 len)
710{
711 u8 lqi;
712 struct sk_buff *skb;
713 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
714
715 if (len < 2)
716 return;
717
718 /* read full frame buffer and invalid lqi value to lowest
719 * indicator if frame was is in a corrupted state.
720 */
721 if (len > IEEE802154_MTU) {
722 lqi = 0;
723 len = IEEE802154_MTU;
724 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
725 } else {
726 lqi = data[len];
727 }
728
729 memcpy(rx_local_buf, data, len);
730 enable_irq(lp->spi->irq);
731
732 skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC);
733 if (!skb) {
734 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
735 return;
736 }
737
738 memcpy(skb_put(skb, len), rx_local_buf, len);
739
740 /* We do not put CRC into the frame */
741 skb_trim(skb, len - 2);
742
743 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
744}
745
746static void
747at86rf230_rx_read_frame_complete(void *context)
748{
749 struct at86rf230_state_change *ctx = context;
750 struct at86rf230_local *lp = ctx->lp;
751 const u8 *buf = lp->irq.buf;
752 const u8 len = buf[1];
753
754 at86rf230_rx(lp, buf + 2, len);
755}
756
757static int
758at86rf230_rx_read_frame(struct at86rf230_local *lp)
759{
760 u8 *buf = lp->irq.buf;
761
762 buf[0] = CMD_FB;
763 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
764 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
765 return spi_async(lp->spi, &lp->irq.msg);
766}
767
768static void
769at86rf230_rx_trac_check(void *context)
770{
771 struct at86rf230_state_change *ctx = context;
772 struct at86rf230_local *lp = ctx->lp;
773 int rc;
774
775 /* Possible check on trac status here. This could be useful to make
776 * some stats why receive is failed. Not used at the moment, but it's
777 * maybe timing relevant. Datasheet doesn't say anything about this.
778 * The programming guide say do it so.
779 */
780
781 rc = at86rf230_rx_read_frame(lp);
782 if (rc) {
783 enable_irq(lp->spi->irq);
784 at86rf230_async_error(lp, ctx, rc);
785 }
786}
787
788static int
789at86rf230_irq_trx_end(struct at86rf230_local *lp)
790{
791 spin_lock(&lp->lock);
792 if (lp->is_tx) {
793 lp->is_tx = 0;
794 spin_unlock(&lp->lock);
795 enable_irq(lp->spi->irq);
796
797 if (lp->tx_aret)
798 return at86rf230_async_state_change(lp, &lp->irq,
799 STATE_FORCE_TX_ON,
800 at86rf230_tx_trac_status);
801 else
802 return at86rf230_async_state_change(lp, &lp->irq,
803 STATE_RX_AACK_ON,
804 at86rf230_tx_complete);
805 } else {
806 spin_unlock(&lp->lock);
807 return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
808 at86rf230_rx_trac_check);
809 }
810}
811
812static void
813at86rf230_irq_status(void *context)
814{
815 struct at86rf230_state_change *ctx = context;
816 struct at86rf230_local *lp = ctx->lp;
817 const u8 *buf = lp->irq.buf;
818 const u8 irq = buf[1];
819 int rc;
820
821 if (irq & IRQ_TRX_END) {
822 rc = at86rf230_irq_trx_end(lp);
823 if (rc)
824 at86rf230_async_error(lp, ctx, rc);
825 } else {
826 enable_irq(lp->spi->irq);
827 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
828 irq);
829 }
830}
831
832static irqreturn_t at86rf230_isr(int irq, void *data)
833{
834 struct at86rf230_local *lp = data;
835 struct at86rf230_state_change *ctx = &lp->irq;
836 u8 *buf = ctx->buf;
837 int rc;
838
839 disable_irq_nosync(lp->spi->irq);
840
841 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
842 ctx->trx.len = 2;
843 ctx->msg.complete = at86rf230_irq_status;
844 rc = spi_async(lp->spi, &ctx->msg);
845 if (rc) {
846 at86rf230_async_error(lp, ctx, rc);
847 return IRQ_NONE;
848 }
849
850 return IRQ_HANDLED;
851}
852
853static void
854at86rf230_write_frame_complete(void *context)
855{
856 struct at86rf230_state_change *ctx = context;
857 struct at86rf230_local *lp = ctx->lp;
858 u8 *buf = ctx->buf;
859 int rc;
860
861 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
862 buf[1] = STATE_BUSY_TX;
863 ctx->trx.len = 2;
864 ctx->msg.complete = NULL;
865 rc = spi_async(lp->spi, &ctx->msg);
866 if (rc)
867 at86rf230_async_error(lp, ctx, rc);
868}
869
870static void
871at86rf230_write_frame(void *context)
872{
873 struct at86rf230_state_change *ctx = context;
874 struct at86rf230_local *lp = ctx->lp;
875 struct sk_buff *skb = lp->tx_skb;
876 u8 *buf = lp->tx.buf;
877 int rc;
878
879 spin_lock(&lp->lock);
880 lp->is_tx = 1;
881 spin_unlock(&lp->lock);
882
883 buf[0] = CMD_FB | CMD_WRITE;
884 buf[1] = skb->len + 2;
885 memcpy(buf + 2, skb->data, skb->len);
886 lp->tx.trx.len = skb->len + 2;
887 lp->tx.msg.complete = at86rf230_write_frame_complete;
888 rc = spi_async(lp->spi, &lp->tx.msg);
889 if (rc)
890 at86rf230_async_error(lp, ctx, rc);
891}
892
893static void
894at86rf230_xmit_tx_on(void *context)
895{
896 struct at86rf230_state_change *ctx = context;
897 struct at86rf230_local *lp = ctx->lp;
898 int rc;
899
900 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
901 at86rf230_write_frame);
902 if (rc)
903 at86rf230_async_error(lp, ctx, rc);
904}
905
906static int
907at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
908{
909 struct at86rf230_local *lp = dev->priv;
910 struct at86rf230_state_change *ctx = &lp->tx;
911
912 void (*tx_complete)(void *context) = at86rf230_write_frame;
913 int rc;
914
915 lp->tx_skb = skb;
916
917 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
918 * are in STATE_TX_ON. The pfad differs here, so we change
919 * the complete handler.
920 */
921 if (lp->tx_aret)
922 tx_complete = at86rf230_xmit_tx_on;
923
924 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
925 tx_complete);
926 if (rc) {
927 at86rf230_async_error(lp, ctx, rc);
928 return rc;
929 }
930 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
931 msecs_to_jiffies(lp->data->t_tx_timeout));
932 if (!rc) {
933 at86rf230_async_error(lp, ctx, rc);
934 return -ETIMEDOUT;
935 }
936
937 /* Interfame spacing time, which is phy depend.
938 * TODO
939 * Move this handling in MAC 802.15.4 layer.
940 * This is currently a workaround to avoid fragmenation issues.
941 */
942 if (skb->len > 18)
943 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
944 else
945 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
946
947 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000948}
949
950static int
951at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
952{
953 might_sleep();
954 BUG_ON(!level);
955 *level = 0xbe;
956 return 0;
957}
958
959static int
960at86rf230_state(struct ieee802154_dev *dev, int state)
961{
962 struct at86rf230_local *lp = dev->priv;
963 int rc;
Alexander Aringf76014f772014-07-03 00:20:44 +0200964 unsigned int val;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000965 u8 desired_status;
966
967 might_sleep();
968
969 if (state == STATE_FORCE_TX_ON)
970 desired_status = STATE_TX_ON;
971 else if (state == STATE_FORCE_TRX_OFF)
972 desired_status = STATE_TRX_OFF;
973 else
974 desired_status = state;
975
976 do {
977 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
978 if (rc)
979 goto err;
980 } while (val == STATE_TRANSITION_IN_PROGRESS);
981
982 if (val == desired_status)
983 return 0;
984
985 /* state is equal to phy states */
986 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state);
987 if (rc)
988 goto err;
989
990 do {
991 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
992 if (rc)
993 goto err;
994 } while (val == STATE_TRANSITION_IN_PROGRESS);
995
996
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100997 if (val == desired_status ||
998 (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) ||
999 (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001000 return 0;
1001
1002 pr_err("unexpected state change: %d, asked for %d\n", val, state);
1003 return -EBUSY;
1004
1005err:
1006 pr_err("error: %d\n", rc);
1007 return rc;
1008}
1009
1010static int
1011at86rf230_start(struct ieee802154_dev *dev)
1012{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001013 u8 rc;
1014
Alexander Aring7332fcb2014-03-15 09:29:03 +01001015 rc = at86rf230_state(dev, STATE_TX_ON);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001016 if (rc)
1017 return rc;
1018
Phoebe Buckheister5b520bb2014-02-17 11:34:07 +01001019 return at86rf230_state(dev, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001020}
1021
1022static void
1023at86rf230_stop(struct ieee802154_dev *dev)
1024{
1025 at86rf230_state(dev, STATE_FORCE_TRX_OFF);
1026}
1027
1028static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001029at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001030{
1031 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1032}
1033
1034static int
1035at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1036{
1037 int rc;
1038
1039 if (channel == 0)
1040 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1041 else
1042 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1043 if (rc < 0)
1044 return rc;
1045
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001046 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001047 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001048 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001049 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001050 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001051 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001052 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001053 if (rc < 0)
1054 return rc;
1055
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001056 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1057}
1058
1059static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001060at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1061{
1062 struct at86rf230_local *lp = dev->priv;
1063 int rc;
1064
1065 might_sleep();
1066
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001067 if (page < 0 || page > 31 ||
1068 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001069 WARN_ON(1);
1070 return -EINVAL;
1071 }
1072
Alexander Aringa53d1f72014-07-03 00:20:46 +02001073 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001074 if (rc < 0)
1075 return rc;
1076
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001077 msleep(1); /* Wait for PLL */
1078 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001079 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001080
1081 return 0;
1082}
1083
1084static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001085at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1086 struct ieee802154_hw_addr_filt *filt,
1087 unsigned long changed)
1088{
1089 struct at86rf230_local *lp = dev->priv;
1090
1091 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001092 u16 addr = le16_to_cpu(filt->short_addr);
1093
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001094 dev_vdbg(&lp->spi->dev,
1095 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001096 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1097 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001098 }
1099
1100 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001101 u16 pan = le16_to_cpu(filt->pan_id);
1102
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001103 dev_vdbg(&lp->spi->dev,
1104 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001105 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1106 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001107 }
1108
1109 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001110 u8 i, addr[8];
1111
1112 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001113 dev_vdbg(&lp->spi->dev,
1114 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001115 for (i = 0; i < 8; i++)
1116 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001117 }
1118
1119 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
1120 dev_vdbg(&lp->spi->dev,
1121 "at86rf230_set_hw_addr_filt called for panc change\n");
1122 if (filt->pan_coord)
1123 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1124 else
1125 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1126 }
1127
1128 return 0;
1129}
1130
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001131static int
Alexander Aring640985e2014-07-03 00:20:43 +02001132at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001133{
1134 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001135
1136 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1137 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1138 * 0dB.
1139 * thus, supported values for db range from -26 to 5, for 31dB of
1140 * reduction to 0dB of reduction.
1141 */
1142 if (db > 5 || db < -26)
1143 return -EINVAL;
1144
1145 db = -(db - 5);
1146
Jean Sacren677676c2014-03-01 15:54:36 -07001147 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001148}
1149
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001150static int
Alexander Aring640985e2014-07-03 00:20:43 +02001151at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001152{
1153 struct at86rf230_local *lp = dev->priv;
1154
1155 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1156}
1157
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001158static int
Alexander Aring640985e2014-07-03 00:20:43 +02001159at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001160{
1161 struct at86rf230_local *lp = dev->priv;
1162
1163 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1164}
1165
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001166static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001167at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1168{
1169 return (level - lp->data->rssi_base_val) * 100 / 207;
1170}
1171
1172static int
1173at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1174{
1175 return (level - lp->data->rssi_base_val) / 2;
1176}
1177
1178static int
Alexander Aring640985e2014-07-03 00:20:43 +02001179at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001180{
1181 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001182
Alexander Aringa53d1f72014-07-03 00:20:46 +02001183 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001184 return -EINVAL;
1185
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001186 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1187 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001188}
1189
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001190static int
Alexander Aring640985e2014-07-03 00:20:43 +02001191at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001192 u8 retries)
1193{
1194 struct at86rf230_local *lp = dev->priv;
1195 int rc;
1196
1197 if (min_be > max_be || max_be > 8 || retries > 5)
1198 return -EINVAL;
1199
1200 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1201 if (rc)
1202 return rc;
1203
1204 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1205 if (rc)
1206 return rc;
1207
Alexander Aring39d7f322014-04-05 13:49:26 +02001208 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001209}
1210
1211static int
Alexander Aring640985e2014-07-03 00:20:43 +02001212at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001213{
1214 struct at86rf230_local *lp = dev->priv;
1215 int rc = 0;
1216
1217 if (retries < -1 || retries > 15)
1218 return -EINVAL;
1219
1220 lp->tx_aret = retries >= 0;
1221
1222 if (retries >= 0)
1223 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1224
1225 return rc;
1226}
1227
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001228static struct ieee802154_ops at86rf230_ops = {
1229 .owner = THIS_MODULE,
1230 .xmit = at86rf230_xmit,
1231 .ed = at86rf230_ed,
1232 .set_channel = at86rf230_channel,
1233 .start = at86rf230_start,
1234 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001235 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001236 .set_txpower = at86rf230_set_txpower,
1237 .set_lbt = at86rf230_set_lbt,
1238 .set_cca_mode = at86rf230_set_cca_mode,
1239 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1240 .set_csma_params = at86rf230_set_csma_params,
1241 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001242};
1243
Alexander Aringa53d1f72014-07-03 00:20:46 +02001244static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001245 .t_frame = 4096,
1246 .t_p_ack = 545,
1247 .t_sifs = 192,
1248 .t_lifs = 480,
1249 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001250 .rssi_base_val = -91,
1251 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001252 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001253};
1254
1255static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001256 .t_frame = 4096,
1257 .t_p_ack = 545,
1258 .t_sifs = 192,
1259 .t_lifs = 480,
1260 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001261 .rssi_base_val = -91,
1262 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001263 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001264};
1265
1266static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001267 .t_frame = 4096,
1268 .t_p_ack = 545,
1269 .t_sifs = 192,
1270 .t_lifs = 480,
1271 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001272 .rssi_base_val = -100,
1273 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001274 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001275};
1276
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001277static int at86rf230_hw_init(struct at86rf230_local *lp)
1278{
Alexander Aring4af619a2014-04-24 19:09:05 +02001279 int rc, irq_pol, irq_type;
Alexander Aringf76014f772014-07-03 00:20:44 +02001280 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001281 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001282
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001283 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF);
1284 if (rc)
1285 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001286
Alexander Aring4af619a2014-04-24 19:09:05 +02001287 irq_type = irq_get_trigger_type(lp->spi->irq);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001288 /* configure irq polarity, defaults to high active */
Alexander Aring4af619a2014-04-24 19:09:05 +02001289 if (irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001290 irq_pol = IRQ_ACTIVE_LOW;
1291 else
1292 irq_pol = IRQ_ACTIVE_HIGH;
1293
Alexander Aring18c65042014-04-24 19:09:18 +02001294 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001295 if (rc)
1296 return rc;
1297
Alexander Aring6bd2b132014-07-03 00:20:49 +02001298 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1299 if (rc)
1300 return rc;
1301
Sascha Herrmann057dad62013-04-14 22:33:29 +00001302 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001303 if (rc)
1304 return rc;
1305
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001306 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1307 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1308 if (rc)
1309 return rc;
1310 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1311 if (rc)
1312 return rc;
1313
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001314 /* CLKM changes are applied immediately */
1315 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1316 if (rc)
1317 return rc;
1318
1319 /* Turn CLKM Off */
1320 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1321 if (rc)
1322 return rc;
1323 /* Wait the next SLEEP cycle */
1324 msleep(100);
1325
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001326 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001327 if (rc)
1328 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001329 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001330 dev_err(&lp->spi->dev, "DVDD error\n");
1331 return -EINVAL;
1332 }
1333
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001334 return 0;
1335}
1336
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001337static struct at86rf230_platform_data *
1338at86rf230_get_pdata(struct spi_device *spi)
1339{
1340 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001341
1342 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1343 return spi->dev.platform_data;
1344
1345 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1346 if (!pdata)
1347 goto done;
1348
1349 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1350 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1351
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001352 spi->dev.platform_data = pdata;
1353done:
1354 return pdata;
1355}
1356
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001357static int
1358at86rf230_detect_device(struct at86rf230_local *lp)
1359{
1360 unsigned int part, version, val;
1361 u16 man_id = 0;
1362 const char *chip;
1363 int rc;
1364
1365 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1366 if (rc)
1367 return rc;
1368 man_id |= val;
1369
1370 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1371 if (rc)
1372 return rc;
1373 man_id |= (val << 8);
1374
1375 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1376 if (rc)
1377 return rc;
1378
1379 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1380 if (rc)
1381 return rc;
1382
1383 if (man_id != 0x001f) {
1384 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1385 man_id >> 8, man_id & 0xFF);
1386 return -EINVAL;
1387 }
1388
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001389 lp->dev->extra_tx_headroom = 0;
1390 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1391 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1392
1393 switch (part) {
1394 case 2:
1395 chip = "at86rf230";
1396 rc = -ENOTSUPP;
1397 break;
1398 case 3:
1399 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001400 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001401 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1402 break;
1403 case 7:
1404 chip = "at86rf212";
1405 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001406 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001407 lp->dev->flags |= IEEE802154_HW_LBT;
1408 lp->dev->phy->channels_supported[0] = 0x00007FF;
1409 lp->dev->phy->channels_supported[2] = 0x00007FF;
1410 } else {
1411 rc = -ENOTSUPP;
1412 }
1413 break;
1414 case 11:
1415 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001416 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001417 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1418 break;
1419 default:
1420 chip = "unkown";
1421 rc = -ENOTSUPP;
1422 break;
1423 }
1424
1425 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1426
1427 return rc;
1428}
1429
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001430static void
1431at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1432{
1433 lp->irq.lp = lp;
1434 spi_message_init(&lp->irq.msg);
1435 lp->irq.msg.context = &lp->irq;
1436 lp->irq.trx.tx_buf = lp->irq.buf;
1437 lp->irq.trx.rx_buf = lp->irq.buf;
1438 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1439
1440 lp->tx.lp = lp;
1441 spi_message_init(&lp->tx.msg);
1442 lp->tx.msg.context = &lp->tx;
1443 lp->tx.trx.tx_buf = lp->tx.buf;
1444 lp->tx.trx.rx_buf = lp->tx.buf;
1445 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1446}
1447
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001448static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001449{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001450 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001451 struct ieee802154_dev *dev;
1452 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001453 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001454 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001455
1456 if (!spi->irq) {
1457 dev_err(&spi->dev, "no IRQ specified\n");
1458 return -EINVAL;
1459 }
1460
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001461 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001462 if (!pdata) {
1463 dev_err(&spi->dev, "no platform_data\n");
1464 return -EINVAL;
1465 }
1466
Alexander Aring3fa27572014-03-15 09:29:06 +01001467 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001468 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1469 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001470 if (rc)
1471 return rc;
1472 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001473
1474 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001475 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1476 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001477 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001478 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001479 }
1480
1481 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001482 if (gpio_is_valid(pdata->rstn)) {
1483 udelay(1);
1484 gpio_set_value(pdata->rstn, 0);
1485 udelay(1);
1486 gpio_set_value(pdata->rstn, 1);
1487 usleep_range(120, 240);
1488 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001489
Alexander Aring640985e2014-07-03 00:20:43 +02001490 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001491 if (!dev)
1492 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001493
1494 lp = dev->priv;
1495 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001496 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001497 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001498
Alexander Aringf76014f772014-07-03 00:20:44 +02001499 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1500 if (IS_ERR(lp->regmap)) {
1501 rc = PTR_ERR(lp->regmap);
1502 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1503 rc);
1504 goto free_dev;
1505 }
1506
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001507 at86rf230_setup_spi_messages(lp);
1508
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001509 rc = at86rf230_detect_device(lp);
1510 if (rc < 0)
1511 goto free_dev;
1512
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001513 spin_lock_init(&lp->lock);
1514 init_completion(&lp->tx_complete);
1515
1516 spi_set_drvdata(spi, lp);
1517
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001518 rc = at86rf230_hw_init(lp);
1519 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001520 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001521
Alexander Aring19626942014-04-24 19:09:15 +02001522 /* Read irq status register to reset irq line */
1523 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001524 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001525 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001526
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001527 irq_type = irq_get_trigger_type(spi->irq);
1528 if (!irq_type)
1529 irq_type = IRQF_TRIGGER_RISING;
1530
1531 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1532 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001533 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001534 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001535
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001536 rc = ieee802154_register_device(lp->dev);
1537 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001538 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001539
1540 return rc;
1541
Alexander Aring640985e2014-07-03 00:20:43 +02001542free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001543 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001544
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001545 return rc;
1546}
1547
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001548static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001549{
1550 struct at86rf230_local *lp = spi_get_drvdata(spi);
1551
Alexander Aring17e84a92014-03-31 03:26:51 +02001552 /* mask all at86rf230 irq's */
1553 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001554 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001555 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001556 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001557
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001558 return 0;
1559}
1560
Alexander Aring1086b4f2014-04-24 19:09:11 +02001561static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001562 { .compatible = "atmel,at86rf230", },
1563 { .compatible = "atmel,at86rf231", },
1564 { .compatible = "atmel,at86rf233", },
1565 { .compatible = "atmel,at86rf212", },
1566 { },
1567};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001568MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001569
Alexander Aring90b15522014-04-24 19:09:12 +02001570static const struct spi_device_id at86rf230_device_id[] = {
1571 { .name = "at86rf230", },
1572 { .name = "at86rf231", },
1573 { .name = "at86rf233", },
1574 { .name = "at86rf212", },
1575 { },
1576};
1577MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1578
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001579static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001580 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001581 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001582 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001583 .name = "at86rf230",
1584 .owner = THIS_MODULE,
1585 },
1586 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001587 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001588};
1589
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001590module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001591
1592MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1593MODULE_LICENSE("GPL v2");