blob: a433d20587e1e408b3cba38f6a7b32fb7450b8c8 [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;
49 /* short interframe spacing time */
50 u16 t_sifs;
51 /* long interframe spacing time */
52 u16 t_lifs;
53 /* completion timeout for tx in msecs */
54 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020055 int rssi_base_val;
56
57 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020058 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020059};
60
Alexander Aring1d15d6b2014-07-03 00:20:48 +020061#define AT86RF2XX_MAX_BUF (127 + 3)
62
63struct at86rf230_state_change {
64 struct at86rf230_local *lp;
65
66 struct spi_message msg;
67 struct spi_transfer trx;
68 u8 buf[AT86RF2XX_MAX_BUF];
69
70 void (*complete)(void *context);
71 u8 from_state;
72 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020073
74 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020075};
76
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000077struct at86rf230_local {
78 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000080 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020081 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020082 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000083
Alexander Aring2e0571c2014-07-03 00:20:51 +020084 struct completion state_complete;
85 struct at86rf230_state_change state;
86
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020088
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010089 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020090 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020091 bool is_tx;
92 /* spinlock for is_tx protection */
93 spinlock_t lock;
94 struct completion tx_complete;
95 struct sk_buff *tx_skb;
96 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000097};
98
99#define RG_TRX_STATUS (0x01)
100#define SR_TRX_STATUS 0x01, 0x1f, 0
101#define SR_RESERVED_01_3 0x01, 0x20, 5
102#define SR_CCA_STATUS 0x01, 0x40, 6
103#define SR_CCA_DONE 0x01, 0x80, 7
104#define RG_TRX_STATE (0x02)
105#define SR_TRX_CMD 0x02, 0x1f, 0
106#define SR_TRAC_STATUS 0x02, 0xe0, 5
107#define RG_TRX_CTRL_0 (0x03)
108#define SR_CLKM_CTRL 0x03, 0x07, 0
109#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
110#define SR_PAD_IO_CLKM 0x03, 0x30, 4
111#define SR_PAD_IO 0x03, 0xc0, 6
112#define RG_TRX_CTRL_1 (0x04)
113#define SR_IRQ_POLARITY 0x04, 0x01, 0
114#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
115#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
116#define SR_RX_BL_CTRL 0x04, 0x10, 4
117#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
118#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
119#define SR_PA_EXT_EN 0x04, 0x80, 7
120#define RG_PHY_TX_PWR (0x05)
121#define SR_TX_PWR 0x05, 0x0f, 0
122#define SR_PA_LT 0x05, 0x30, 4
123#define SR_PA_BUF_LT 0x05, 0xc0, 6
124#define RG_PHY_RSSI (0x06)
125#define SR_RSSI 0x06, 0x1f, 0
126#define SR_RND_VALUE 0x06, 0x60, 5
127#define SR_RX_CRC_VALID 0x06, 0x80, 7
128#define RG_PHY_ED_LEVEL (0x07)
129#define SR_ED_LEVEL 0x07, 0xff, 0
130#define RG_PHY_CC_CCA (0x08)
131#define SR_CHANNEL 0x08, 0x1f, 0
132#define SR_CCA_MODE 0x08, 0x60, 5
133#define SR_CCA_REQUEST 0x08, 0x80, 7
134#define RG_CCA_THRES (0x09)
135#define SR_CCA_ED_THRES 0x09, 0x0f, 0
136#define SR_RESERVED_09_1 0x09, 0xf0, 4
137#define RG_RX_CTRL (0x0a)
138#define SR_PDT_THRES 0x0a, 0x0f, 0
139#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
140#define RG_SFD_VALUE (0x0b)
141#define SR_SFD_VALUE 0x0b, 0xff, 0
142#define RG_TRX_CTRL_2 (0x0c)
143#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100144#define SR_SUB_MODE 0x0c, 0x04, 2
145#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100146#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
147#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000148#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
149#define RG_ANT_DIV (0x0d)
150#define SR_ANT_CTRL 0x0d, 0x03, 0
151#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
152#define SR_ANT_DIV_EN 0x0d, 0x08, 3
153#define SR_RESERVED_0d_2 0x0d, 0x70, 4
154#define SR_ANT_SEL 0x0d, 0x80, 7
155#define RG_IRQ_MASK (0x0e)
156#define SR_IRQ_MASK 0x0e, 0xff, 0
157#define RG_IRQ_STATUS (0x0f)
158#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
159#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
160#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
161#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
162#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
163#define SR_IRQ_5_AMI 0x0f, 0x20, 5
164#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
165#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
166#define RG_VREG_CTRL (0x10)
167#define SR_RESERVED_10_6 0x10, 0x03, 0
168#define SR_DVDD_OK 0x10, 0x04, 2
169#define SR_DVREG_EXT 0x10, 0x08, 3
170#define SR_RESERVED_10_3 0x10, 0x30, 4
171#define SR_AVDD_OK 0x10, 0x40, 6
172#define SR_AVREG_EXT 0x10, 0x80, 7
173#define RG_BATMON (0x11)
174#define SR_BATMON_VTH 0x11, 0x0f, 0
175#define SR_BATMON_HR 0x11, 0x10, 4
176#define SR_BATMON_OK 0x11, 0x20, 5
177#define SR_RESERVED_11_1 0x11, 0xc0, 6
178#define RG_XOSC_CTRL (0x12)
179#define SR_XTAL_TRIM 0x12, 0x0f, 0
180#define SR_XTAL_MODE 0x12, 0xf0, 4
181#define RG_RX_SYN (0x15)
182#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
183#define SR_RESERVED_15_2 0x15, 0x70, 4
184#define SR_RX_PDT_DIS 0x15, 0x80, 7
185#define RG_XAH_CTRL_1 (0x17)
186#define SR_RESERVED_17_8 0x17, 0x01, 0
187#define SR_AACK_PROM_MODE 0x17, 0x02, 1
188#define SR_AACK_ACK_TIME 0x17, 0x04, 2
189#define SR_RESERVED_17_5 0x17, 0x08, 3
190#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
191#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100192#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000193#define SR_RESERVED_17_1 0x17, 0x80, 7
194#define RG_FTN_CTRL (0x18)
195#define SR_RESERVED_18_2 0x18, 0x7f, 0
196#define SR_FTN_START 0x18, 0x80, 7
197#define RG_PLL_CF (0x1a)
198#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
199#define SR_PLL_CF_START 0x1a, 0x80, 7
200#define RG_PLL_DCU (0x1b)
201#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
202#define SR_RESERVED_1b_2 0x1b, 0x40, 6
203#define SR_PLL_DCU_START 0x1b, 0x80, 7
204#define RG_PART_NUM (0x1c)
205#define SR_PART_NUM 0x1c, 0xff, 0
206#define RG_VERSION_NUM (0x1d)
207#define SR_VERSION_NUM 0x1d, 0xff, 0
208#define RG_MAN_ID_0 (0x1e)
209#define SR_MAN_ID_0 0x1e, 0xff, 0
210#define RG_MAN_ID_1 (0x1f)
211#define SR_MAN_ID_1 0x1f, 0xff, 0
212#define RG_SHORT_ADDR_0 (0x20)
213#define SR_SHORT_ADDR_0 0x20, 0xff, 0
214#define RG_SHORT_ADDR_1 (0x21)
215#define SR_SHORT_ADDR_1 0x21, 0xff, 0
216#define RG_PAN_ID_0 (0x22)
217#define SR_PAN_ID_0 0x22, 0xff, 0
218#define RG_PAN_ID_1 (0x23)
219#define SR_PAN_ID_1 0x23, 0xff, 0
220#define RG_IEEE_ADDR_0 (0x24)
221#define SR_IEEE_ADDR_0 0x24, 0xff, 0
222#define RG_IEEE_ADDR_1 (0x25)
223#define SR_IEEE_ADDR_1 0x25, 0xff, 0
224#define RG_IEEE_ADDR_2 (0x26)
225#define SR_IEEE_ADDR_2 0x26, 0xff, 0
226#define RG_IEEE_ADDR_3 (0x27)
227#define SR_IEEE_ADDR_3 0x27, 0xff, 0
228#define RG_IEEE_ADDR_4 (0x28)
229#define SR_IEEE_ADDR_4 0x28, 0xff, 0
230#define RG_IEEE_ADDR_5 (0x29)
231#define SR_IEEE_ADDR_5 0x29, 0xff, 0
232#define RG_IEEE_ADDR_6 (0x2a)
233#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
234#define RG_IEEE_ADDR_7 (0x2b)
235#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
236#define RG_XAH_CTRL_0 (0x2c)
237#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
238#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
239#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
240#define RG_CSMA_SEED_0 (0x2d)
241#define SR_CSMA_SEED_0 0x2d, 0xff, 0
242#define RG_CSMA_SEED_1 (0x2e)
243#define SR_CSMA_SEED_1 0x2e, 0x07, 0
244#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
245#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
246#define SR_AACK_SET_PD 0x2e, 0x20, 5
247#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
248#define RG_CSMA_BE (0x2f)
249#define SR_MIN_BE 0x2f, 0x0f, 0
250#define SR_MAX_BE 0x2f, 0xf0, 4
251
252#define CMD_REG 0x80
253#define CMD_REG_MASK 0x3f
254#define CMD_WRITE 0x40
255#define CMD_FB 0x20
256
257#define IRQ_BAT_LOW (1 << 7)
258#define IRQ_TRX_UR (1 << 6)
259#define IRQ_AMI (1 << 5)
260#define IRQ_CCA_ED (1 << 4)
261#define IRQ_TRX_END (1 << 3)
262#define IRQ_RX_START (1 << 2)
263#define IRQ_PLL_UNL (1 << 1)
264#define IRQ_PLL_LOCK (1 << 0)
265
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000266#define IRQ_ACTIVE_HIGH 0
267#define IRQ_ACTIVE_LOW 1
268
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000269#define STATE_P_ON 0x00 /* BUSY */
270#define STATE_BUSY_RX 0x01
271#define STATE_BUSY_TX 0x02
272#define STATE_FORCE_TRX_OFF 0x03
273#define STATE_FORCE_TX_ON 0x04 /* IDLE */
274/* 0x05 */ /* INVALID_PARAMETER */
275#define STATE_RX_ON 0x06
276/* 0x07 */ /* SUCCESS */
277#define STATE_TRX_OFF 0x08
278#define STATE_TX_ON 0x09
279/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
280#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500281#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000282#define STATE_BUSY_RX_AACK 0x11
283#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000284#define STATE_RX_AACK_ON 0x16
285#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000286#define STATE_RX_ON_NOCLK 0x1C
287#define STATE_RX_AACK_ON_NOCLK 0x1D
288#define STATE_BUSY_RX_AACK_NOCLK 0x1E
289#define STATE_TRANSITION_IN_PROGRESS 0x1F
290
Alexander Aringf76014f772014-07-03 00:20:44 +0200291#define AT86RF2XX_NUMREGS 0x3F
292
Alexander Aring97fed792014-10-07 10:38:32 +0200293static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200294at86rf230_async_state_change(struct at86rf230_local *lp,
295 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200296 const u8 state, void (*complete)(void *context),
297 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200298
Alexander Aringf76014f772014-07-03 00:20:44 +0200299static inline int
300__at86rf230_write(struct at86rf230_local *lp,
301 unsigned int addr, unsigned int data)
302{
303 return regmap_write(lp->regmap, addr, data);
304}
305
306static inline int
307__at86rf230_read(struct at86rf230_local *lp,
308 unsigned int addr, unsigned int *data)
309{
310 return regmap_read(lp->regmap, addr, data);
311}
312
313static inline int
314at86rf230_read_subreg(struct at86rf230_local *lp,
315 unsigned int addr, unsigned int mask,
316 unsigned int shift, unsigned int *data)
317{
318 int rc;
319
320 rc = __at86rf230_read(lp, addr, data);
321 if (rc > 0)
322 *data = (*data & mask) >> shift;
323
324 return rc;
325}
326
327static inline int
328at86rf230_write_subreg(struct at86rf230_local *lp,
329 unsigned int addr, unsigned int mask,
330 unsigned int shift, unsigned int data)
331{
332 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
333}
334
335static bool
336at86rf230_reg_writeable(struct device *dev, unsigned int reg)
337{
338 switch (reg) {
339 case RG_TRX_STATE:
340 case RG_TRX_CTRL_0:
341 case RG_TRX_CTRL_1:
342 case RG_PHY_TX_PWR:
343 case RG_PHY_ED_LEVEL:
344 case RG_PHY_CC_CCA:
345 case RG_CCA_THRES:
346 case RG_RX_CTRL:
347 case RG_SFD_VALUE:
348 case RG_TRX_CTRL_2:
349 case RG_ANT_DIV:
350 case RG_IRQ_MASK:
351 case RG_VREG_CTRL:
352 case RG_BATMON:
353 case RG_XOSC_CTRL:
354 case RG_RX_SYN:
355 case RG_XAH_CTRL_1:
356 case RG_FTN_CTRL:
357 case RG_PLL_CF:
358 case RG_PLL_DCU:
359 case RG_SHORT_ADDR_0:
360 case RG_SHORT_ADDR_1:
361 case RG_PAN_ID_0:
362 case RG_PAN_ID_1:
363 case RG_IEEE_ADDR_0:
364 case RG_IEEE_ADDR_1:
365 case RG_IEEE_ADDR_2:
366 case RG_IEEE_ADDR_3:
367 case RG_IEEE_ADDR_4:
368 case RG_IEEE_ADDR_5:
369 case RG_IEEE_ADDR_6:
370 case RG_IEEE_ADDR_7:
371 case RG_XAH_CTRL_0:
372 case RG_CSMA_SEED_0:
373 case RG_CSMA_SEED_1:
374 case RG_CSMA_BE:
375 return true;
376 default:
377 return false;
378 }
379}
380
381static bool
382at86rf230_reg_readable(struct device *dev, unsigned int reg)
383{
384 bool rc;
385
386 /* all writeable are also readable */
387 rc = at86rf230_reg_writeable(dev, reg);
388 if (rc)
389 return rc;
390
391 /* readonly regs */
392 switch (reg) {
393 case RG_TRX_STATUS:
394 case RG_PHY_RSSI:
395 case RG_IRQ_STATUS:
396 case RG_PART_NUM:
397 case RG_VERSION_NUM:
398 case RG_MAN_ID_1:
399 case RG_MAN_ID_0:
400 return true;
401 default:
402 return false;
403 }
404}
405
406static bool
407at86rf230_reg_volatile(struct device *dev, unsigned int reg)
408{
409 /* can be changed during runtime */
410 switch (reg) {
411 case RG_TRX_STATUS:
412 case RG_TRX_STATE:
413 case RG_PHY_RSSI:
414 case RG_PHY_ED_LEVEL:
415 case RG_IRQ_STATUS:
416 case RG_VREG_CTRL:
417 return true;
418 default:
419 return false;
420 }
421}
422
423static bool
424at86rf230_reg_precious(struct device *dev, unsigned int reg)
425{
426 /* don't clear irq line on read */
427 switch (reg) {
428 case RG_IRQ_STATUS:
429 return true;
430 default:
431 return false;
432 }
433}
434
435static struct regmap_config at86rf230_regmap_spi_config = {
436 .reg_bits = 8,
437 .val_bits = 8,
438 .write_flag_mask = CMD_REG | CMD_WRITE,
439 .read_flag_mask = CMD_REG,
440 .cache_type = REGCACHE_RBTREE,
441 .max_register = AT86RF2XX_NUMREGS,
442 .writeable_reg = at86rf230_reg_writeable,
443 .readable_reg = at86rf230_reg_readable,
444 .volatile_reg = at86rf230_reg_volatile,
445 .precious_reg = at86rf230_reg_precious,
446};
447
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200448static void
449at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000450{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200451 struct at86rf230_state_change *ctx = context;
452 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000453
Alexander Aring97fed792014-10-07 10:38:32 +0200454 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200455}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000456
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200457static void
458at86rf230_async_error(struct at86rf230_local *lp,
459 struct at86rf230_state_change *ctx, int rc)
460{
461 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000462
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200463 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200464 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000466
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200467/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200468static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200469at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
470 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200471 void (*complete)(void *context),
472 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200473{
Alexander Aring97fed792014-10-07 10:38:32 +0200474 int rc;
475
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200476 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000477
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200478 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
479 ctx->trx.len = 2;
480 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200481 ctx->irq_enable = irq_enable;
482 rc = spi_async(lp->spi, &ctx->msg);
483 if (rc) {
484 if (irq_enable)
485 enable_irq(lp->spi->irq);
486
487 at86rf230_async_error(lp, ctx, rc);
488 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200489}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000490
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200491static void
492at86rf230_async_state_assert(void *context)
493{
494 struct at86rf230_state_change *ctx = context;
495 struct at86rf230_local *lp = ctx->lp;
496 const u8 *buf = ctx->buf;
497 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000498
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200499 /* Assert state change */
500 if (trx_state != ctx->to_state) {
501 /* Special handling if transceiver state is in
502 * STATE_BUSY_RX_AACK and a SHR was detected.
503 */
504 if (trx_state == STATE_BUSY_RX_AACK) {
505 /* Undocumented race condition. If we send a state
506 * change to STATE_RX_AACK_ON the transceiver could
507 * change his state automatically to STATE_BUSY_RX_AACK
508 * if a SHR was detected. This is not an error, but we
509 * can't assert this.
510 */
511 if (ctx->to_state == STATE_RX_AACK_ON)
512 goto done;
513
514 /* If we change to STATE_TX_ON without forcing and
515 * transceiver state is STATE_BUSY_RX_AACK, we wait
516 * 'tFrame + tPAck' receiving time. In this time the
517 * PDU should be received. If the transceiver is still
518 * in STATE_BUSY_RX_AACK, we run a force state change
519 * to STATE_TX_ON. This is a timeout handling, if the
520 * transceiver stucks in STATE_BUSY_RX_AACK.
521 */
522 if (ctx->to_state == STATE_TX_ON) {
523 at86rf230_async_state_change(lp, ctx,
524 STATE_FORCE_TX_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200525 ctx->complete,
526 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200527 return;
528 }
529 }
530
531
532 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
533 ctx->from_state, ctx->to_state, trx_state);
534 }
535
536done:
537 if (ctx->complete)
538 ctx->complete(context);
539}
540
541/* Do state change timing delay. */
542static void
543at86rf230_async_state_delay(void *context)
544{
545 struct at86rf230_state_change *ctx = context;
546 struct at86rf230_local *lp = ctx->lp;
547 struct at86rf2xx_chip_data *c = lp->data;
548 bool force = false;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200549
550 /* The force state changes are will show as normal states in the
551 * state status subregister. We change the to_state to the
552 * corresponding one and remember if it was a force change, this
553 * differs if we do a state change from STATE_BUSY_RX_AACK.
554 */
555 switch (ctx->to_state) {
556 case STATE_FORCE_TX_ON:
557 ctx->to_state = STATE_TX_ON;
558 force = true;
559 break;
560 case STATE_FORCE_TRX_OFF:
561 ctx->to_state = STATE_TRX_OFF;
562 force = true;
563 break;
564 default:
565 break;
566 }
567
568 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200569 case STATE_TRX_OFF:
570 switch (ctx->to_state) {
571 case STATE_RX_AACK_ON:
572 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
573 goto change;
574 case STATE_TX_ON:
575 usleep_range(c->t_off_to_tx_on,
576 c->t_off_to_tx_on + 10);
577 goto change;
578 default:
579 break;
580 }
581 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200582 case STATE_BUSY_RX_AACK:
583 switch (ctx->to_state) {
584 case STATE_TX_ON:
585 /* Wait for worst case receiving time if we
586 * didn't make a force change from BUSY_RX_AACK
587 * to TX_ON.
588 */
589 if (!force) {
590 usleep_range(c->t_frame + c->t_p_ack,
591 c->t_frame + c->t_p_ack + 1000);
592 goto change;
593 }
594 break;
595 default:
596 break;
597 }
598 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200599 /* Default value, means RESET state */
600 case STATE_P_ON:
601 switch (ctx->to_state) {
602 case STATE_TRX_OFF:
603 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
604 goto change;
605 default:
606 break;
607 }
608 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200609 default:
610 break;
611 }
612
613 /* Default delay is 1us in the most cases */
614 udelay(1);
615
616change:
Alexander Aring97fed792014-10-07 10:38:32 +0200617 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
618 at86rf230_async_state_assert,
619 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200620}
621
622static void
623at86rf230_async_state_change_start(void *context)
624{
625 struct at86rf230_state_change *ctx = context;
626 struct at86rf230_local *lp = ctx->lp;
627 u8 *buf = ctx->buf;
628 const u8 trx_state = buf[1] & 0x1f;
629 int rc;
630
631 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
632 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
633 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200634 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
635 at86rf230_async_state_change_start,
636 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200637 return;
638 }
639
640 /* Check if we already are in the state which we change in */
641 if (trx_state == ctx->to_state) {
642 if (ctx->complete)
643 ctx->complete(context);
644 return;
645 }
646
647 /* Set current state to the context of state change */
648 ctx->from_state = trx_state;
649
650 /* Going into the next step for a state change which do a timing
651 * relevant delay.
652 */
653 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
654 buf[1] = ctx->to_state;
655 ctx->trx.len = 2;
656 ctx->msg.complete = at86rf230_async_state_delay;
657 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200658 if (rc) {
659 if (ctx->irq_enable)
660 enable_irq(lp->spi->irq);
661
662 at86rf230_async_error(lp, &lp->state, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200663 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000664}
665
Alexander Aring97fed792014-10-07 10:38:32 +0200666static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200667at86rf230_async_state_change(struct at86rf230_local *lp,
668 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200669 const u8 state, void (*complete)(void *context),
670 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000671{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200672 /* Initialization for the state change context */
673 ctx->to_state = state;
674 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200675 ctx->irq_enable = irq_enable;
676 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
677 at86rf230_async_state_change_start,
678 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200679}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000680
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200681static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200682at86rf230_sync_state_change_complete(void *context)
683{
684 struct at86rf230_state_change *ctx = context;
685 struct at86rf230_local *lp = ctx->lp;
686
687 complete(&lp->state_complete);
688}
689
690/* This function do a sync framework above the async state change.
691 * Some callbacks of the IEEE 802.15.4 driver interface need to be
692 * handled synchronously.
693 */
694static int
695at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
696{
697 int rc;
698
Alexander Aring97fed792014-10-07 10:38:32 +0200699 at86rf230_async_state_change(lp, &lp->state, state,
700 at86rf230_sync_state_change_complete,
701 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200702
703 rc = wait_for_completion_timeout(&lp->state_complete,
704 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200705 if (!rc) {
706 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200707 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200708 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200709
710 return 0;
711}
712
713static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200714at86rf230_tx_complete(void *context)
715{
716 struct at86rf230_state_change *ctx = context;
717 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000718
Alexander Aring35e92a82014-10-07 10:38:31 +0200719 enable_irq(lp->spi->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200720 complete(&lp->tx_complete);
721}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000722
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200723static void
724at86rf230_tx_on(void *context)
725{
726 struct at86rf230_state_change *ctx = context;
727 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000728
Alexander Aring97fed792014-10-07 10:38:32 +0200729 at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
730 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200731}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000732
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200733static void
734at86rf230_tx_trac_error(void *context)
735{
736 struct at86rf230_state_change *ctx = context;
737 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000738
Alexander Aring97fed792014-10-07 10:38:32 +0200739 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
740 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200741}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000742
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200743static void
744at86rf230_tx_trac_check(void *context)
745{
746 struct at86rf230_state_change *ctx = context;
747 struct at86rf230_local *lp = ctx->lp;
748 const u8 *buf = ctx->buf;
749 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000750
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200751 /* If trac status is different than zero we need to do a state change
752 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
753 * state to TX_ON.
754 */
755 if (trac) {
Alexander Aring97fed792014-10-07 10:38:32 +0200756 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
757 at86rf230_tx_trac_error, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200758 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000759 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000760
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200761 at86rf230_tx_on(context);
762}
763
764
765static void
766at86rf230_tx_trac_status(void *context)
767{
768 struct at86rf230_state_change *ctx = context;
769 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200770
Alexander Aring97fed792014-10-07 10:38:32 +0200771 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
772 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200773}
774
775static void
776at86rf230_rx(struct at86rf230_local *lp,
777 const u8 *data, u8 len)
778{
779 u8 lqi;
780 struct sk_buff *skb;
781 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
782
783 if (len < 2)
784 return;
785
786 /* read full frame buffer and invalid lqi value to lowest
787 * indicator if frame was is in a corrupted state.
788 */
789 if (len > IEEE802154_MTU) {
790 lqi = 0;
791 len = IEEE802154_MTU;
792 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
793 } else {
794 lqi = data[len];
795 }
796
797 memcpy(rx_local_buf, data, len);
798 enable_irq(lp->spi->irq);
799
800 skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC);
801 if (!skb) {
802 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
803 return;
804 }
805
806 memcpy(skb_put(skb, len), rx_local_buf, len);
807
808 /* We do not put CRC into the frame */
809 skb_trim(skb, len - 2);
810
811 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
812}
813
814static void
815at86rf230_rx_read_frame_complete(void *context)
816{
817 struct at86rf230_state_change *ctx = context;
818 struct at86rf230_local *lp = ctx->lp;
819 const u8 *buf = lp->irq.buf;
820 const u8 len = buf[1];
821
822 at86rf230_rx(lp, buf + 2, len);
823}
824
Alexander Aring97fed792014-10-07 10:38:32 +0200825static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200826at86rf230_rx_read_frame(struct at86rf230_local *lp)
827{
Alexander Aring97fed792014-10-07 10:38:32 +0200828 int rc;
829
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200830 u8 *buf = lp->irq.buf;
831
832 buf[0] = CMD_FB;
833 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
834 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200835 rc = spi_async(lp->spi, &lp->irq.msg);
836 if (rc) {
837 enable_irq(lp->spi->irq);
838 at86rf230_async_error(lp, &lp->irq, rc);
839 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200840}
841
842static void
843at86rf230_rx_trac_check(void *context)
844{
845 struct at86rf230_state_change *ctx = context;
846 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200847
848 /* Possible check on trac status here. This could be useful to make
849 * some stats why receive is failed. Not used at the moment, but it's
850 * maybe timing relevant. Datasheet doesn't say anything about this.
851 * The programming guide say do it so.
852 */
853
Alexander Aring97fed792014-10-07 10:38:32 +0200854 at86rf230_rx_read_frame(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855}
856
Alexander Aring97fed792014-10-07 10:38:32 +0200857static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200858at86rf230_irq_trx_end(struct at86rf230_local *lp)
859{
860 spin_lock(&lp->lock);
861 if (lp->is_tx) {
862 lp->is_tx = 0;
863 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200864
865 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200866 at86rf230_async_state_change(lp, &lp->irq,
867 STATE_FORCE_TX_ON,
868 at86rf230_tx_trac_status,
869 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200870 else
Alexander Aring97fed792014-10-07 10:38:32 +0200871 at86rf230_async_state_change(lp, &lp->irq,
872 STATE_RX_AACK_ON,
873 at86rf230_tx_complete,
874 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200875 } else {
876 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200877 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
878 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200879 }
880}
881
882static void
883at86rf230_irq_status(void *context)
884{
885 struct at86rf230_state_change *ctx = context;
886 struct at86rf230_local *lp = ctx->lp;
887 const u8 *buf = lp->irq.buf;
888 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200889
890 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200891 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200892 } else {
893 enable_irq(lp->spi->irq);
894 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
895 irq);
896 }
897}
898
899static irqreturn_t at86rf230_isr(int irq, void *data)
900{
901 struct at86rf230_local *lp = data;
902 struct at86rf230_state_change *ctx = &lp->irq;
903 u8 *buf = ctx->buf;
904 int rc;
905
Alexander Aring90566362014-10-07 10:38:29 +0200906 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200907
908 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
909 ctx->trx.len = 2;
910 ctx->msg.complete = at86rf230_irq_status;
911 rc = spi_async(lp->spi, &ctx->msg);
912 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200913 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200914 at86rf230_async_error(lp, ctx, rc);
915 return IRQ_NONE;
916 }
917
918 return IRQ_HANDLED;
919}
920
921static void
922at86rf230_write_frame_complete(void *context)
923{
924 struct at86rf230_state_change *ctx = context;
925 struct at86rf230_local *lp = ctx->lp;
926 u8 *buf = ctx->buf;
927 int rc;
928
929 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
930 buf[1] = STATE_BUSY_TX;
931 ctx->trx.len = 2;
932 ctx->msg.complete = NULL;
933 rc = spi_async(lp->spi, &ctx->msg);
934 if (rc)
935 at86rf230_async_error(lp, ctx, rc);
936}
937
938static void
939at86rf230_write_frame(void *context)
940{
941 struct at86rf230_state_change *ctx = context;
942 struct at86rf230_local *lp = ctx->lp;
943 struct sk_buff *skb = lp->tx_skb;
944 u8 *buf = lp->tx.buf;
945 int rc;
946
947 spin_lock(&lp->lock);
948 lp->is_tx = 1;
949 spin_unlock(&lp->lock);
950
951 buf[0] = CMD_FB | CMD_WRITE;
952 buf[1] = skb->len + 2;
953 memcpy(buf + 2, skb->data, skb->len);
954 lp->tx.trx.len = skb->len + 2;
955 lp->tx.msg.complete = at86rf230_write_frame_complete;
956 rc = spi_async(lp->spi, &lp->tx.msg);
957 if (rc)
958 at86rf230_async_error(lp, ctx, rc);
959}
960
961static void
962at86rf230_xmit_tx_on(void *context)
963{
964 struct at86rf230_state_change *ctx = context;
965 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200966
Alexander Aring97fed792014-10-07 10:38:32 +0200967 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
968 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200969}
970
971static int
972at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
973{
974 struct at86rf230_local *lp = dev->priv;
975 struct at86rf230_state_change *ctx = &lp->tx;
976
977 void (*tx_complete)(void *context) = at86rf230_write_frame;
978 int rc;
979
980 lp->tx_skb = skb;
981
982 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
983 * are in STATE_TX_ON. The pfad differs here, so we change
984 * the complete handler.
985 */
986 if (lp->tx_aret)
987 tx_complete = at86rf230_xmit_tx_on;
988
Alexander Aring97fed792014-10-07 10:38:32 +0200989 at86rf230_async_state_change(lp, ctx, STATE_TX_ON, tx_complete, false);
990
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200991 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
992 msecs_to_jiffies(lp->data->t_tx_timeout));
993 if (!rc) {
Alexander Aring464f0292014-10-07 10:38:25 +0200994 at86rf230_async_error(lp, ctx, -ETIMEDOUT);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200995 return -ETIMEDOUT;
996 }
997
Alexander Aring850f43a2014-10-07 10:38:27 +0200998 if (lp->max_frame_retries > 0)
999 return 0;
1000
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001001 /* Interfame spacing time, which is phy depend.
1002 * TODO
1003 * Move this handling in MAC 802.15.4 layer.
1004 * This is currently a workaround to avoid fragmenation issues.
1005 */
1006 if (skb->len > 18)
1007 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
1008 else
1009 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
1010
1011 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001012}
1013
1014static int
1015at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
1016{
1017 might_sleep();
1018 BUG_ON(!level);
1019 *level = 0xbe;
1020 return 0;
1021}
1022
1023static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001024at86rf230_start(struct ieee802154_dev *dev)
1025{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001026 return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001027}
1028
1029static void
1030at86rf230_stop(struct ieee802154_dev *dev)
1031{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001032 at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001033}
1034
1035static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001036at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001037{
1038 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1039}
1040
1041static int
1042at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1043{
1044 int rc;
1045
1046 if (channel == 0)
1047 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1048 else
1049 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1050 if (rc < 0)
1051 return rc;
1052
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001053 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001054 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001055 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001056 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001057 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001058 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001059 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001060 if (rc < 0)
1061 return rc;
1062
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001063 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1064}
1065
1066static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001067at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1068{
1069 struct at86rf230_local *lp = dev->priv;
1070 int rc;
1071
1072 might_sleep();
1073
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001074 if (page < 0 || page > 31 ||
1075 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001076 WARN_ON(1);
1077 return -EINVAL;
1078 }
1079
Alexander Aringa53d1f72014-07-03 00:20:46 +02001080 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001081 if (rc < 0)
1082 return rc;
1083
Alexander Aring984e0c62014-07-03 00:20:53 +02001084 /* Wait for PLL */
1085 usleep_range(lp->data->t_channel_switch,
1086 lp->data->t_channel_switch + 10);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001087 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001088 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001089
1090 return 0;
1091}
1092
1093static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001094at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1095 struct ieee802154_hw_addr_filt *filt,
1096 unsigned long changed)
1097{
1098 struct at86rf230_local *lp = dev->priv;
1099
Alexander Aring57205c12014-10-25 05:25:09 +02001100 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001101 u16 addr = le16_to_cpu(filt->short_addr);
1102
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001103 dev_vdbg(&lp->spi->dev,
1104 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001105 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1106 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001107 }
1108
Alexander Aring57205c12014-10-25 05:25:09 +02001109 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001110 u16 pan = le16_to_cpu(filt->pan_id);
1111
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001112 dev_vdbg(&lp->spi->dev,
1113 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001114 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1115 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001116 }
1117
Alexander Aring57205c12014-10-25 05:25:09 +02001118 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001119 u8 i, addr[8];
1120
1121 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001122 dev_vdbg(&lp->spi->dev,
1123 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001124 for (i = 0; i < 8; i++)
1125 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001126 }
1127
Alexander Aring57205c12014-10-25 05:25:09 +02001128 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001129 dev_vdbg(&lp->spi->dev,
1130 "at86rf230_set_hw_addr_filt called for panc change\n");
1131 if (filt->pan_coord)
1132 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1133 else
1134 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1135 }
1136
1137 return 0;
1138}
1139
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001140static int
Alexander Aring640985e2014-07-03 00:20:43 +02001141at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001142{
1143 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001144
1145 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1146 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1147 * 0dB.
1148 * thus, supported values for db range from -26 to 5, for 31dB of
1149 * reduction to 0dB of reduction.
1150 */
1151 if (db > 5 || db < -26)
1152 return -EINVAL;
1153
1154 db = -(db - 5);
1155
Jean Sacren677676c2014-03-01 15:54:36 -07001156 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001157}
1158
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001159static int
Alexander Aring640985e2014-07-03 00:20:43 +02001160at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001161{
1162 struct at86rf230_local *lp = dev->priv;
1163
1164 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1165}
1166
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001167static int
Alexander Aring640985e2014-07-03 00:20:43 +02001168at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001169{
1170 struct at86rf230_local *lp = dev->priv;
1171
1172 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1173}
1174
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001175static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001176at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1177{
1178 return (level - lp->data->rssi_base_val) * 100 / 207;
1179}
1180
1181static int
1182at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1183{
1184 return (level - lp->data->rssi_base_val) / 2;
1185}
1186
1187static int
Alexander Aring640985e2014-07-03 00:20:43 +02001188at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001189{
1190 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001191
Alexander Aringa53d1f72014-07-03 00:20:46 +02001192 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001193 return -EINVAL;
1194
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001195 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1196 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001197}
1198
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001199static int
Alexander Aring640985e2014-07-03 00:20:43 +02001200at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001201 u8 retries)
1202{
1203 struct at86rf230_local *lp = dev->priv;
1204 int rc;
1205
1206 if (min_be > max_be || max_be > 8 || retries > 5)
1207 return -EINVAL;
1208
1209 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1210 if (rc)
1211 return rc;
1212
1213 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1214 if (rc)
1215 return rc;
1216
Alexander Aring39d7f322014-04-05 13:49:26 +02001217 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001218}
1219
1220static int
Alexander Aring640985e2014-07-03 00:20:43 +02001221at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001222{
1223 struct at86rf230_local *lp = dev->priv;
1224 int rc = 0;
1225
1226 if (retries < -1 || retries > 15)
1227 return -EINVAL;
1228
1229 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001230 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001231
1232 if (retries >= 0)
1233 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1234
1235 return rc;
1236}
1237
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001238static struct ieee802154_ops at86rf230_ops = {
1239 .owner = THIS_MODULE,
1240 .xmit = at86rf230_xmit,
1241 .ed = at86rf230_ed,
1242 .set_channel = at86rf230_channel,
1243 .start = at86rf230_start,
1244 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001245 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001246 .set_txpower = at86rf230_set_txpower,
1247 .set_lbt = at86rf230_set_lbt,
1248 .set_cca_mode = at86rf230_set_cca_mode,
1249 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1250 .set_csma_params = at86rf230_set_csma_params,
1251 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001252};
1253
Alexander Aringa53d1f72014-07-03 00:20:46 +02001254static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001255 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001256 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001257 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001258 .t_off_to_aack = 80,
1259 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001260 .t_frame = 4096,
1261 .t_p_ack = 545,
1262 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001263 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001264 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001265 .rssi_base_val = -91,
1266 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001267 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001268};
1269
1270static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001271 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001272 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001273 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001274 .t_off_to_aack = 110,
1275 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001276 .t_frame = 4096,
1277 .t_p_ack = 545,
1278 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001279 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001280 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001281 .rssi_base_val = -91,
1282 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001283 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001284};
1285
1286static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001287 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001288 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001289 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001290 .t_off_to_aack = 200,
1291 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001292 .t_frame = 4096,
1293 .t_p_ack = 545,
1294 .t_sifs = 192,
Alexander Aring7ad38b82014-10-07 10:38:28 +02001295 .t_lifs = 640,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001296 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001297 .rssi_base_val = -100,
1298 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001299 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001300};
1301
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001302static int at86rf230_hw_init(struct at86rf230_local *lp)
1303{
Alexander Aring1db05582014-07-03 00:20:50 +02001304 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001305 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001306 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001307
Alexander Aring09e536c2014-07-03 00:20:52 +02001308 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001309 if (rc)
1310 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001311
Alexander Aring4af619a2014-04-24 19:09:05 +02001312 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001313 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001314 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001315
Alexander Aring18c65042014-04-24 19:09:18 +02001316 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001317 if (rc)
1318 return rc;
1319
Alexander Aring6bd2b132014-07-03 00:20:49 +02001320 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1321 if (rc)
1322 return rc;
1323
Sascha Herrmann057dad62013-04-14 22:33:29 +00001324 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001325 if (rc)
1326 return rc;
1327
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001328 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1329 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1330 if (rc)
1331 return rc;
1332 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1333 if (rc)
1334 return rc;
1335
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001336 /* CLKM changes are applied immediately */
1337 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1338 if (rc)
1339 return rc;
1340
1341 /* Turn CLKM Off */
1342 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1343 if (rc)
1344 return rc;
1345 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001346 usleep_range(lp->data->t_sleep_cycle,
1347 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001348
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001349 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001350 if (rc)
1351 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001352 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001353 dev_err(&lp->spi->dev, "DVDD error\n");
1354 return -EINVAL;
1355 }
1356
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001357 return 0;
1358}
1359
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001360static struct at86rf230_platform_data *
1361at86rf230_get_pdata(struct spi_device *spi)
1362{
1363 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001364
1365 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1366 return spi->dev.platform_data;
1367
1368 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1369 if (!pdata)
1370 goto done;
1371
1372 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1373 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1374
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001375 spi->dev.platform_data = pdata;
1376done:
1377 return pdata;
1378}
1379
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001380static int
1381at86rf230_detect_device(struct at86rf230_local *lp)
1382{
1383 unsigned int part, version, val;
1384 u16 man_id = 0;
1385 const char *chip;
1386 int rc;
1387
1388 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1389 if (rc)
1390 return rc;
1391 man_id |= val;
1392
1393 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1394 if (rc)
1395 return rc;
1396 man_id |= (val << 8);
1397
1398 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1399 if (rc)
1400 return rc;
1401
1402 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1403 if (rc)
1404 return rc;
1405
1406 if (man_id != 0x001f) {
1407 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1408 man_id >> 8, man_id & 0xFF);
1409 return -EINVAL;
1410 }
1411
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001412 lp->dev->extra_tx_headroom = 0;
1413 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1414 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1415
1416 switch (part) {
1417 case 2:
1418 chip = "at86rf230";
1419 rc = -ENOTSUPP;
1420 break;
1421 case 3:
1422 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001423 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001424 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1425 break;
1426 case 7:
1427 chip = "at86rf212";
1428 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001429 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001430 lp->dev->flags |= IEEE802154_HW_LBT;
1431 lp->dev->phy->channels_supported[0] = 0x00007FF;
1432 lp->dev->phy->channels_supported[2] = 0x00007FF;
1433 } else {
1434 rc = -ENOTSUPP;
1435 }
1436 break;
1437 case 11:
1438 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001439 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001440 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1441 break;
1442 default:
1443 chip = "unkown";
1444 rc = -ENOTSUPP;
1445 break;
1446 }
1447
1448 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1449
1450 return rc;
1451}
1452
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001453static void
1454at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1455{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001456 lp->state.lp = lp;
1457 spi_message_init(&lp->state.msg);
1458 lp->state.msg.context = &lp->state;
1459 lp->state.trx.tx_buf = lp->state.buf;
1460 lp->state.trx.rx_buf = lp->state.buf;
1461 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1462
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001463 lp->irq.lp = lp;
1464 spi_message_init(&lp->irq.msg);
1465 lp->irq.msg.context = &lp->irq;
1466 lp->irq.trx.tx_buf = lp->irq.buf;
1467 lp->irq.trx.rx_buf = lp->irq.buf;
1468 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1469
1470 lp->tx.lp = lp;
1471 spi_message_init(&lp->tx.msg);
1472 lp->tx.msg.context = &lp->tx;
1473 lp->tx.trx.tx_buf = lp->tx.buf;
1474 lp->tx.trx.rx_buf = lp->tx.buf;
1475 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1476}
1477
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001478static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001479{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001480 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001481 struct ieee802154_dev *dev;
1482 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001483 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001484 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001485
1486 if (!spi->irq) {
1487 dev_err(&spi->dev, "no IRQ specified\n");
1488 return -EINVAL;
1489 }
1490
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001491 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001492 if (!pdata) {
1493 dev_err(&spi->dev, "no platform_data\n");
1494 return -EINVAL;
1495 }
1496
Alexander Aring3fa27572014-03-15 09:29:06 +01001497 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001498 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1499 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001500 if (rc)
1501 return rc;
1502 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001503
1504 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001505 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1506 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001507 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001508 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001509 }
1510
1511 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001512 if (gpio_is_valid(pdata->rstn)) {
1513 udelay(1);
1514 gpio_set_value(pdata->rstn, 0);
1515 udelay(1);
1516 gpio_set_value(pdata->rstn, 1);
1517 usleep_range(120, 240);
1518 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001519
Alexander Aring640985e2014-07-03 00:20:43 +02001520 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001521 if (!dev)
1522 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001523
1524 lp = dev->priv;
1525 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001526 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001527 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001528
Alexander Aringf76014f772014-07-03 00:20:44 +02001529 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1530 if (IS_ERR(lp->regmap)) {
1531 rc = PTR_ERR(lp->regmap);
1532 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1533 rc);
1534 goto free_dev;
1535 }
1536
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001537 at86rf230_setup_spi_messages(lp);
1538
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001539 rc = at86rf230_detect_device(lp);
1540 if (rc < 0)
1541 goto free_dev;
1542
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001543 spin_lock_init(&lp->lock);
1544 init_completion(&lp->tx_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001545 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001546
1547 spi_set_drvdata(spi, lp);
1548
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001549 rc = at86rf230_hw_init(lp);
1550 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001551 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001552
Alexander Aring19626942014-04-24 19:09:15 +02001553 /* Read irq status register to reset irq line */
1554 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001555 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001556 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001557
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001558 irq_type = irq_get_trigger_type(spi->irq);
1559 if (!irq_type)
1560 irq_type = IRQF_TRIGGER_RISING;
1561
1562 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1563 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001564 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001565 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001566
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001567 rc = ieee802154_register_device(lp->dev);
1568 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001569 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001570
1571 return rc;
1572
Alexander Aring640985e2014-07-03 00:20:43 +02001573free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001574 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001575
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001576 return rc;
1577}
1578
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001579static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001580{
1581 struct at86rf230_local *lp = spi_get_drvdata(spi);
1582
Alexander Aring17e84a92014-03-31 03:26:51 +02001583 /* mask all at86rf230 irq's */
1584 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001585 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001586 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001587 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001588
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001589 return 0;
1590}
1591
Alexander Aring1086b4f2014-04-24 19:09:11 +02001592static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001593 { .compatible = "atmel,at86rf230", },
1594 { .compatible = "atmel,at86rf231", },
1595 { .compatible = "atmel,at86rf233", },
1596 { .compatible = "atmel,at86rf212", },
1597 { },
1598};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001599MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001600
Alexander Aring90b15522014-04-24 19:09:12 +02001601static const struct spi_device_id at86rf230_device_id[] = {
1602 { .name = "at86rf230", },
1603 { .name = "at86rf231", },
1604 { .name = "at86rf233", },
1605 { .name = "at86rf212", },
1606 { },
1607};
1608MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1609
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001610static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001611 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001612 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001613 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001614 .name = "at86rf230",
1615 .owner = THIS_MODULE,
1616 },
1617 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001618 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001619};
1620
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001621module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001622
1623MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1624MODULE_LICENSE("GPL v2");