blob: 2a25324d3a64e00bc25b7fc488515fb6fe2e1d93 [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>
Alexander Aring01ebd602014-07-03 00:20:55 +020022 * Alexander Aring <aar@pengutronix.de>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000023 */
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020027#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000028#include <linux/gpio.h>
29#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000030#include <linux/spinlock.h>
31#include <linux/spi/spi.h>
32#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020033#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000034#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010035#include <linux/of_gpio.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000036
Alexander Aring1d15d6b2014-07-03 00:20:48 +020037#include <net/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000038#include <net/mac802154.h>
39#include <net/wpan-phy.h>
40
Alexander Aringa53d1f72014-07-03 00:20:46 +020041struct at86rf230_local;
42/* at86rf2xx chip depend data.
43 * All timings are in us.
44 */
45struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020046 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020047 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020048 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020049 u16 t_off_to_aack;
50 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020051 u16 t_frame;
52 u16 t_p_ack;
53 /* short interframe spacing time */
54 u16 t_sifs;
55 /* long interframe spacing time */
56 u16 t_lifs;
57 /* completion timeout for tx in msecs */
58 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020059 int rssi_base_val;
60
61 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020062 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020063};
64
Alexander Aring1d15d6b2014-07-03 00:20:48 +020065#define AT86RF2XX_MAX_BUF (127 + 3)
66
67struct at86rf230_state_change {
68 struct at86rf230_local *lp;
69
70 struct spi_message msg;
71 struct spi_transfer trx;
72 u8 buf[AT86RF2XX_MAX_BUF];
73
74 void (*complete)(void *context);
75 u8 from_state;
76 u8 to_state;
77};
78
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079struct at86rf230_local {
80 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000081
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000082 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020083 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020084 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000085
Alexander Aring2e0571c2014-07-03 00:20:51 +020086 struct completion state_complete;
87 struct at86rf230_state_change state;
88
Alexander Aring1d15d6b2014-07-03 00:20:48 +020089 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020090
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010091 bool tx_aret;
Alexander Aring850f43a2014-10-07 10:38:27 +020092 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020093 bool is_tx;
94 /* spinlock for is_tx protection */
95 spinlock_t lock;
96 struct completion tx_complete;
97 struct sk_buff *tx_skb;
98 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000099};
100
101#define RG_TRX_STATUS (0x01)
102#define SR_TRX_STATUS 0x01, 0x1f, 0
103#define SR_RESERVED_01_3 0x01, 0x20, 5
104#define SR_CCA_STATUS 0x01, 0x40, 6
105#define SR_CCA_DONE 0x01, 0x80, 7
106#define RG_TRX_STATE (0x02)
107#define SR_TRX_CMD 0x02, 0x1f, 0
108#define SR_TRAC_STATUS 0x02, 0xe0, 5
109#define RG_TRX_CTRL_0 (0x03)
110#define SR_CLKM_CTRL 0x03, 0x07, 0
111#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
112#define SR_PAD_IO_CLKM 0x03, 0x30, 4
113#define SR_PAD_IO 0x03, 0xc0, 6
114#define RG_TRX_CTRL_1 (0x04)
115#define SR_IRQ_POLARITY 0x04, 0x01, 0
116#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
117#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
118#define SR_RX_BL_CTRL 0x04, 0x10, 4
119#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
120#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
121#define SR_PA_EXT_EN 0x04, 0x80, 7
122#define RG_PHY_TX_PWR (0x05)
123#define SR_TX_PWR 0x05, 0x0f, 0
124#define SR_PA_LT 0x05, 0x30, 4
125#define SR_PA_BUF_LT 0x05, 0xc0, 6
126#define RG_PHY_RSSI (0x06)
127#define SR_RSSI 0x06, 0x1f, 0
128#define SR_RND_VALUE 0x06, 0x60, 5
129#define SR_RX_CRC_VALID 0x06, 0x80, 7
130#define RG_PHY_ED_LEVEL (0x07)
131#define SR_ED_LEVEL 0x07, 0xff, 0
132#define RG_PHY_CC_CCA (0x08)
133#define SR_CHANNEL 0x08, 0x1f, 0
134#define SR_CCA_MODE 0x08, 0x60, 5
135#define SR_CCA_REQUEST 0x08, 0x80, 7
136#define RG_CCA_THRES (0x09)
137#define SR_CCA_ED_THRES 0x09, 0x0f, 0
138#define SR_RESERVED_09_1 0x09, 0xf0, 4
139#define RG_RX_CTRL (0x0a)
140#define SR_PDT_THRES 0x0a, 0x0f, 0
141#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
142#define RG_SFD_VALUE (0x0b)
143#define SR_SFD_VALUE 0x0b, 0xff, 0
144#define RG_TRX_CTRL_2 (0x0c)
145#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100146#define SR_SUB_MODE 0x0c, 0x04, 2
147#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100148#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
149#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000150#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
151#define RG_ANT_DIV (0x0d)
152#define SR_ANT_CTRL 0x0d, 0x03, 0
153#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
154#define SR_ANT_DIV_EN 0x0d, 0x08, 3
155#define SR_RESERVED_0d_2 0x0d, 0x70, 4
156#define SR_ANT_SEL 0x0d, 0x80, 7
157#define RG_IRQ_MASK (0x0e)
158#define SR_IRQ_MASK 0x0e, 0xff, 0
159#define RG_IRQ_STATUS (0x0f)
160#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
161#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
162#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
163#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
164#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
165#define SR_IRQ_5_AMI 0x0f, 0x20, 5
166#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
167#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
168#define RG_VREG_CTRL (0x10)
169#define SR_RESERVED_10_6 0x10, 0x03, 0
170#define SR_DVDD_OK 0x10, 0x04, 2
171#define SR_DVREG_EXT 0x10, 0x08, 3
172#define SR_RESERVED_10_3 0x10, 0x30, 4
173#define SR_AVDD_OK 0x10, 0x40, 6
174#define SR_AVREG_EXT 0x10, 0x80, 7
175#define RG_BATMON (0x11)
176#define SR_BATMON_VTH 0x11, 0x0f, 0
177#define SR_BATMON_HR 0x11, 0x10, 4
178#define SR_BATMON_OK 0x11, 0x20, 5
179#define SR_RESERVED_11_1 0x11, 0xc0, 6
180#define RG_XOSC_CTRL (0x12)
181#define SR_XTAL_TRIM 0x12, 0x0f, 0
182#define SR_XTAL_MODE 0x12, 0xf0, 4
183#define RG_RX_SYN (0x15)
184#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
185#define SR_RESERVED_15_2 0x15, 0x70, 4
186#define SR_RX_PDT_DIS 0x15, 0x80, 7
187#define RG_XAH_CTRL_1 (0x17)
188#define SR_RESERVED_17_8 0x17, 0x01, 0
189#define SR_AACK_PROM_MODE 0x17, 0x02, 1
190#define SR_AACK_ACK_TIME 0x17, 0x04, 2
191#define SR_RESERVED_17_5 0x17, 0x08, 3
192#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
193#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100194#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000195#define SR_RESERVED_17_1 0x17, 0x80, 7
196#define RG_FTN_CTRL (0x18)
197#define SR_RESERVED_18_2 0x18, 0x7f, 0
198#define SR_FTN_START 0x18, 0x80, 7
199#define RG_PLL_CF (0x1a)
200#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
201#define SR_PLL_CF_START 0x1a, 0x80, 7
202#define RG_PLL_DCU (0x1b)
203#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
204#define SR_RESERVED_1b_2 0x1b, 0x40, 6
205#define SR_PLL_DCU_START 0x1b, 0x80, 7
206#define RG_PART_NUM (0x1c)
207#define SR_PART_NUM 0x1c, 0xff, 0
208#define RG_VERSION_NUM (0x1d)
209#define SR_VERSION_NUM 0x1d, 0xff, 0
210#define RG_MAN_ID_0 (0x1e)
211#define SR_MAN_ID_0 0x1e, 0xff, 0
212#define RG_MAN_ID_1 (0x1f)
213#define SR_MAN_ID_1 0x1f, 0xff, 0
214#define RG_SHORT_ADDR_0 (0x20)
215#define SR_SHORT_ADDR_0 0x20, 0xff, 0
216#define RG_SHORT_ADDR_1 (0x21)
217#define SR_SHORT_ADDR_1 0x21, 0xff, 0
218#define RG_PAN_ID_0 (0x22)
219#define SR_PAN_ID_0 0x22, 0xff, 0
220#define RG_PAN_ID_1 (0x23)
221#define SR_PAN_ID_1 0x23, 0xff, 0
222#define RG_IEEE_ADDR_0 (0x24)
223#define SR_IEEE_ADDR_0 0x24, 0xff, 0
224#define RG_IEEE_ADDR_1 (0x25)
225#define SR_IEEE_ADDR_1 0x25, 0xff, 0
226#define RG_IEEE_ADDR_2 (0x26)
227#define SR_IEEE_ADDR_2 0x26, 0xff, 0
228#define RG_IEEE_ADDR_3 (0x27)
229#define SR_IEEE_ADDR_3 0x27, 0xff, 0
230#define RG_IEEE_ADDR_4 (0x28)
231#define SR_IEEE_ADDR_4 0x28, 0xff, 0
232#define RG_IEEE_ADDR_5 (0x29)
233#define SR_IEEE_ADDR_5 0x29, 0xff, 0
234#define RG_IEEE_ADDR_6 (0x2a)
235#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
236#define RG_IEEE_ADDR_7 (0x2b)
237#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
238#define RG_XAH_CTRL_0 (0x2c)
239#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
240#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
241#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
242#define RG_CSMA_SEED_0 (0x2d)
243#define SR_CSMA_SEED_0 0x2d, 0xff, 0
244#define RG_CSMA_SEED_1 (0x2e)
245#define SR_CSMA_SEED_1 0x2e, 0x07, 0
246#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
247#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
248#define SR_AACK_SET_PD 0x2e, 0x20, 5
249#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
250#define RG_CSMA_BE (0x2f)
251#define SR_MIN_BE 0x2f, 0x0f, 0
252#define SR_MAX_BE 0x2f, 0xf0, 4
253
254#define CMD_REG 0x80
255#define CMD_REG_MASK 0x3f
256#define CMD_WRITE 0x40
257#define CMD_FB 0x20
258
259#define IRQ_BAT_LOW (1 << 7)
260#define IRQ_TRX_UR (1 << 6)
261#define IRQ_AMI (1 << 5)
262#define IRQ_CCA_ED (1 << 4)
263#define IRQ_TRX_END (1 << 3)
264#define IRQ_RX_START (1 << 2)
265#define IRQ_PLL_UNL (1 << 1)
266#define IRQ_PLL_LOCK (1 << 0)
267
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000268#define IRQ_ACTIVE_HIGH 0
269#define IRQ_ACTIVE_LOW 1
270
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000271#define STATE_P_ON 0x00 /* BUSY */
272#define STATE_BUSY_RX 0x01
273#define STATE_BUSY_TX 0x02
274#define STATE_FORCE_TRX_OFF 0x03
275#define STATE_FORCE_TX_ON 0x04 /* IDLE */
276/* 0x05 */ /* INVALID_PARAMETER */
277#define STATE_RX_ON 0x06
278/* 0x07 */ /* SUCCESS */
279#define STATE_TRX_OFF 0x08
280#define STATE_TX_ON 0x09
281/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
282#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500283#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000284#define STATE_BUSY_RX_AACK 0x11
285#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000286#define STATE_RX_AACK_ON 0x16
287#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000288#define STATE_RX_ON_NOCLK 0x1C
289#define STATE_RX_AACK_ON_NOCLK 0x1D
290#define STATE_BUSY_RX_AACK_NOCLK 0x1E
291#define STATE_TRANSITION_IN_PROGRESS 0x1F
292
Alexander Aringf76014f772014-07-03 00:20:44 +0200293#define AT86RF2XX_NUMREGS 0x3F
294
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200295static int
296at86rf230_async_state_change(struct at86rf230_local *lp,
297 struct at86rf230_state_change *ctx,
298 const u8 state, void (*complete)(void *context));
299
Alexander Aringf76014f772014-07-03 00:20:44 +0200300static inline int
301__at86rf230_write(struct at86rf230_local *lp,
302 unsigned int addr, unsigned int data)
303{
304 return regmap_write(lp->regmap, addr, data);
305}
306
307static inline int
308__at86rf230_read(struct at86rf230_local *lp,
309 unsigned int addr, unsigned int *data)
310{
311 return regmap_read(lp->regmap, addr, data);
312}
313
314static inline int
315at86rf230_read_subreg(struct at86rf230_local *lp,
316 unsigned int addr, unsigned int mask,
317 unsigned int shift, unsigned int *data)
318{
319 int rc;
320
321 rc = __at86rf230_read(lp, addr, data);
322 if (rc > 0)
323 *data = (*data & mask) >> shift;
324
325 return rc;
326}
327
328static inline int
329at86rf230_write_subreg(struct at86rf230_local *lp,
330 unsigned int addr, unsigned int mask,
331 unsigned int shift, unsigned int data)
332{
333 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
334}
335
336static bool
337at86rf230_reg_writeable(struct device *dev, unsigned int reg)
338{
339 switch (reg) {
340 case RG_TRX_STATE:
341 case RG_TRX_CTRL_0:
342 case RG_TRX_CTRL_1:
343 case RG_PHY_TX_PWR:
344 case RG_PHY_ED_LEVEL:
345 case RG_PHY_CC_CCA:
346 case RG_CCA_THRES:
347 case RG_RX_CTRL:
348 case RG_SFD_VALUE:
349 case RG_TRX_CTRL_2:
350 case RG_ANT_DIV:
351 case RG_IRQ_MASK:
352 case RG_VREG_CTRL:
353 case RG_BATMON:
354 case RG_XOSC_CTRL:
355 case RG_RX_SYN:
356 case RG_XAH_CTRL_1:
357 case RG_FTN_CTRL:
358 case RG_PLL_CF:
359 case RG_PLL_DCU:
360 case RG_SHORT_ADDR_0:
361 case RG_SHORT_ADDR_1:
362 case RG_PAN_ID_0:
363 case RG_PAN_ID_1:
364 case RG_IEEE_ADDR_0:
365 case RG_IEEE_ADDR_1:
366 case RG_IEEE_ADDR_2:
367 case RG_IEEE_ADDR_3:
368 case RG_IEEE_ADDR_4:
369 case RG_IEEE_ADDR_5:
370 case RG_IEEE_ADDR_6:
371 case RG_IEEE_ADDR_7:
372 case RG_XAH_CTRL_0:
373 case RG_CSMA_SEED_0:
374 case RG_CSMA_SEED_1:
375 case RG_CSMA_BE:
376 return true;
377 default:
378 return false;
379 }
380}
381
382static bool
383at86rf230_reg_readable(struct device *dev, unsigned int reg)
384{
385 bool rc;
386
387 /* all writeable are also readable */
388 rc = at86rf230_reg_writeable(dev, reg);
389 if (rc)
390 return rc;
391
392 /* readonly regs */
393 switch (reg) {
394 case RG_TRX_STATUS:
395 case RG_PHY_RSSI:
396 case RG_IRQ_STATUS:
397 case RG_PART_NUM:
398 case RG_VERSION_NUM:
399 case RG_MAN_ID_1:
400 case RG_MAN_ID_0:
401 return true;
402 default:
403 return false;
404 }
405}
406
407static bool
408at86rf230_reg_volatile(struct device *dev, unsigned int reg)
409{
410 /* can be changed during runtime */
411 switch (reg) {
412 case RG_TRX_STATUS:
413 case RG_TRX_STATE:
414 case RG_PHY_RSSI:
415 case RG_PHY_ED_LEVEL:
416 case RG_IRQ_STATUS:
417 case RG_VREG_CTRL:
418 return true;
419 default:
420 return false;
421 }
422}
423
424static bool
425at86rf230_reg_precious(struct device *dev, unsigned int reg)
426{
427 /* don't clear irq line on read */
428 switch (reg) {
429 case RG_IRQ_STATUS:
430 return true;
431 default:
432 return false;
433 }
434}
435
436static struct regmap_config at86rf230_regmap_spi_config = {
437 .reg_bits = 8,
438 .val_bits = 8,
439 .write_flag_mask = CMD_REG | CMD_WRITE,
440 .read_flag_mask = CMD_REG,
441 .cache_type = REGCACHE_RBTREE,
442 .max_register = AT86RF2XX_NUMREGS,
443 .writeable_reg = at86rf230_reg_writeable,
444 .readable_reg = at86rf230_reg_readable,
445 .volatile_reg = at86rf230_reg_volatile,
446 .precious_reg = at86rf230_reg_precious,
447};
448
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200449static void
450at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000451{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200452 struct at86rf230_state_change *ctx = context;
453 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000454
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200455 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL);
456}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000457
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200458static void
459at86rf230_async_error(struct at86rf230_local *lp,
460 struct at86rf230_state_change *ctx, int rc)
461{
462 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000463
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200464 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
465 at86rf230_async_error_recover);
466}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000467
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200468/* Generic function to get some register value in async mode */
469static int
470at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
471 struct at86rf230_state_change *ctx,
472 void (*complete)(void *context))
473{
474 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000475
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200476 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
477 ctx->trx.len = 2;
478 ctx->msg.complete = complete;
479 return spi_async(lp->spi, &ctx->msg);
480}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000481
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200482static void
483at86rf230_async_state_assert(void *context)
484{
485 struct at86rf230_state_change *ctx = context;
486 struct at86rf230_local *lp = ctx->lp;
487 const u8 *buf = ctx->buf;
488 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000489
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200490 /* Assert state change */
491 if (trx_state != ctx->to_state) {
492 /* Special handling if transceiver state is in
493 * STATE_BUSY_RX_AACK and a SHR was detected.
494 */
495 if (trx_state == STATE_BUSY_RX_AACK) {
496 /* Undocumented race condition. If we send a state
497 * change to STATE_RX_AACK_ON the transceiver could
498 * change his state automatically to STATE_BUSY_RX_AACK
499 * if a SHR was detected. This is not an error, but we
500 * can't assert this.
501 */
502 if (ctx->to_state == STATE_RX_AACK_ON)
503 goto done;
504
505 /* If we change to STATE_TX_ON without forcing and
506 * transceiver state is STATE_BUSY_RX_AACK, we wait
507 * 'tFrame + tPAck' receiving time. In this time the
508 * PDU should be received. If the transceiver is still
509 * in STATE_BUSY_RX_AACK, we run a force state change
510 * to STATE_TX_ON. This is a timeout handling, if the
511 * transceiver stucks in STATE_BUSY_RX_AACK.
512 */
513 if (ctx->to_state == STATE_TX_ON) {
514 at86rf230_async_state_change(lp, ctx,
515 STATE_FORCE_TX_ON,
516 ctx->complete);
517 return;
518 }
519 }
520
521
522 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
523 ctx->from_state, ctx->to_state, trx_state);
524 }
525
526done:
527 if (ctx->complete)
528 ctx->complete(context);
529}
530
531/* Do state change timing delay. */
532static void
533at86rf230_async_state_delay(void *context)
534{
535 struct at86rf230_state_change *ctx = context;
536 struct at86rf230_local *lp = ctx->lp;
537 struct at86rf2xx_chip_data *c = lp->data;
538 bool force = false;
539 int rc;
540
541 /* The force state changes are will show as normal states in the
542 * state status subregister. We change the to_state to the
543 * corresponding one and remember if it was a force change, this
544 * differs if we do a state change from STATE_BUSY_RX_AACK.
545 */
546 switch (ctx->to_state) {
547 case STATE_FORCE_TX_ON:
548 ctx->to_state = STATE_TX_ON;
549 force = true;
550 break;
551 case STATE_FORCE_TRX_OFF:
552 ctx->to_state = STATE_TRX_OFF;
553 force = true;
554 break;
555 default:
556 break;
557 }
558
559 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200560 case STATE_TRX_OFF:
561 switch (ctx->to_state) {
562 case STATE_RX_AACK_ON:
563 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
564 goto change;
565 case STATE_TX_ON:
566 usleep_range(c->t_off_to_tx_on,
567 c->t_off_to_tx_on + 10);
568 goto change;
569 default:
570 break;
571 }
572 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200573 case STATE_BUSY_RX_AACK:
574 switch (ctx->to_state) {
575 case STATE_TX_ON:
576 /* Wait for worst case receiving time if we
577 * didn't make a force change from BUSY_RX_AACK
578 * to TX_ON.
579 */
580 if (!force) {
581 usleep_range(c->t_frame + c->t_p_ack,
582 c->t_frame + c->t_p_ack + 1000);
583 goto change;
584 }
585 break;
586 default:
587 break;
588 }
589 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200590 /* Default value, means RESET state */
591 case STATE_P_ON:
592 switch (ctx->to_state) {
593 case STATE_TRX_OFF:
594 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
595 goto change;
596 default:
597 break;
598 }
599 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200600 default:
601 break;
602 }
603
604 /* Default delay is 1us in the most cases */
605 udelay(1);
606
607change:
608 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
609 at86rf230_async_state_assert);
610 if (rc)
611 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
612}
613
614static void
615at86rf230_async_state_change_start(void *context)
616{
617 struct at86rf230_state_change *ctx = context;
618 struct at86rf230_local *lp = ctx->lp;
619 u8 *buf = ctx->buf;
620 const u8 trx_state = buf[1] & 0x1f;
621 int rc;
622
623 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
624 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
625 udelay(1);
626 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
627 at86rf230_async_state_change_start);
628 if (rc)
629 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
630 return;
631 }
632
633 /* Check if we already are in the state which we change in */
634 if (trx_state == ctx->to_state) {
635 if (ctx->complete)
636 ctx->complete(context);
637 return;
638 }
639
640 /* Set current state to the context of state change */
641 ctx->from_state = trx_state;
642
643 /* Going into the next step for a state change which do a timing
644 * relevant delay.
645 */
646 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
647 buf[1] = ctx->to_state;
648 ctx->trx.len = 2;
649 ctx->msg.complete = at86rf230_async_state_delay;
650 rc = spi_async(lp->spi, &ctx->msg);
651 if (rc)
652 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000653}
654
655static int
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200656at86rf230_async_state_change(struct at86rf230_local *lp,
657 struct at86rf230_state_change *ctx,
658 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000659{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200660 /* Initialization for the state change context */
661 ctx->to_state = state;
662 ctx->complete = complete;
663 return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
664 at86rf230_async_state_change_start);
665}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000666
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200667static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200668at86rf230_sync_state_change_complete(void *context)
669{
670 struct at86rf230_state_change *ctx = context;
671 struct at86rf230_local *lp = ctx->lp;
672
673 complete(&lp->state_complete);
674}
675
676/* This function do a sync framework above the async state change.
677 * Some callbacks of the IEEE 802.15.4 driver interface need to be
678 * handled synchronously.
679 */
680static int
681at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
682{
683 int rc;
684
685 rc = at86rf230_async_state_change(lp, &lp->state, state,
686 at86rf230_sync_state_change_complete);
687 if (rc) {
688 at86rf230_async_error(lp, &lp->state, rc);
689 return rc;
690 }
691
692 rc = wait_for_completion_timeout(&lp->state_complete,
693 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200694 if (!rc) {
695 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200696 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200697 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200698
699 return 0;
700}
701
702static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200703at86rf230_tx_complete(void *context)
704{
705 struct at86rf230_state_change *ctx = context;
706 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000707
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200708 complete(&lp->tx_complete);
709}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000710
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200711static void
712at86rf230_tx_on(void *context)
713{
714 struct at86rf230_state_change *ctx = context;
715 struct at86rf230_local *lp = ctx->lp;
716 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000717
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200718 rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
719 at86rf230_tx_complete);
720 if (rc)
721 at86rf230_async_error(lp, ctx, rc);
722}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000723
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200724static void
725at86rf230_tx_trac_error(void *context)
726{
727 struct at86rf230_state_change *ctx = context;
728 struct at86rf230_local *lp = ctx->lp;
729 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000730
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200731 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
732 at86rf230_tx_on);
733 if (rc)
734 at86rf230_async_error(lp, ctx, rc);
735}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000736
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200737static void
738at86rf230_tx_trac_check(void *context)
739{
740 struct at86rf230_state_change *ctx = context;
741 struct at86rf230_local *lp = ctx->lp;
742 const u8 *buf = ctx->buf;
743 const u8 trac = (buf[1] & 0xe0) >> 5;
744 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000745
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200746 /* If trac status is different than zero we need to do a state change
747 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
748 * state to TX_ON.
749 */
750 if (trac) {
751 rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
752 at86rf230_tx_trac_error);
753 if (rc)
754 at86rf230_async_error(lp, ctx, rc);
755 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000756 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000757
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200758 at86rf230_tx_on(context);
759}
760
761
762static void
763at86rf230_tx_trac_status(void *context)
764{
765 struct at86rf230_state_change *ctx = context;
766 struct at86rf230_local *lp = ctx->lp;
767 int rc;
768
769 rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
770 at86rf230_tx_trac_check);
771 if (rc)
772 at86rf230_async_error(lp, ctx, rc);
773}
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
825static int
826at86rf230_rx_read_frame(struct at86rf230_local *lp)
827{
828 u8 *buf = lp->irq.buf;
829
830 buf[0] = CMD_FB;
831 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
832 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
833 return spi_async(lp->spi, &lp->irq.msg);
834}
835
836static void
837at86rf230_rx_trac_check(void *context)
838{
839 struct at86rf230_state_change *ctx = context;
840 struct at86rf230_local *lp = ctx->lp;
841 int rc;
842
843 /* Possible check on trac status here. This could be useful to make
844 * some stats why receive is failed. Not used at the moment, but it's
845 * maybe timing relevant. Datasheet doesn't say anything about this.
846 * The programming guide say do it so.
847 */
848
849 rc = at86rf230_rx_read_frame(lp);
850 if (rc) {
851 enable_irq(lp->spi->irq);
852 at86rf230_async_error(lp, ctx, rc);
853 }
854}
855
856static int
857at86rf230_irq_trx_end(struct at86rf230_local *lp)
858{
859 spin_lock(&lp->lock);
860 if (lp->is_tx) {
861 lp->is_tx = 0;
862 spin_unlock(&lp->lock);
863 enable_irq(lp->spi->irq);
864
865 if (lp->tx_aret)
866 return at86rf230_async_state_change(lp, &lp->irq,
867 STATE_FORCE_TX_ON,
868 at86rf230_tx_trac_status);
869 else
870 return at86rf230_async_state_change(lp, &lp->irq,
871 STATE_RX_AACK_ON,
872 at86rf230_tx_complete);
873 } else {
874 spin_unlock(&lp->lock);
875 return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
876 at86rf230_rx_trac_check);
877 }
878}
879
880static void
881at86rf230_irq_status(void *context)
882{
883 struct at86rf230_state_change *ctx = context;
884 struct at86rf230_local *lp = ctx->lp;
885 const u8 *buf = lp->irq.buf;
886 const u8 irq = buf[1];
887 int rc;
888
889 if (irq & IRQ_TRX_END) {
890 rc = at86rf230_irq_trx_end(lp);
891 if (rc)
892 at86rf230_async_error(lp, ctx, rc);
893 } else {
894 enable_irq(lp->spi->irq);
895 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
896 irq);
897 }
898}
899
900static irqreturn_t at86rf230_isr(int irq, void *data)
901{
902 struct at86rf230_local *lp = data;
903 struct at86rf230_state_change *ctx = &lp->irq;
904 u8 *buf = ctx->buf;
905 int rc;
906
907 disable_irq_nosync(lp->spi->irq);
908
909 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
910 ctx->trx.len = 2;
911 ctx->msg.complete = at86rf230_irq_status;
912 rc = spi_async(lp->spi, &ctx->msg);
913 if (rc) {
914 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;
966 int rc;
967
968 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
969 at86rf230_write_frame);
970 if (rc)
971 at86rf230_async_error(lp, ctx, rc);
972}
973
974static int
975at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
976{
977 struct at86rf230_local *lp = dev->priv;
978 struct at86rf230_state_change *ctx = &lp->tx;
979
980 void (*tx_complete)(void *context) = at86rf230_write_frame;
981 int rc;
982
983 lp->tx_skb = skb;
984
985 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
986 * are in STATE_TX_ON. The pfad differs here, so we change
987 * the complete handler.
988 */
989 if (lp->tx_aret)
990 tx_complete = at86rf230_xmit_tx_on;
991
992 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
993 tx_complete);
994 if (rc) {
995 at86rf230_async_error(lp, ctx, rc);
996 return rc;
997 }
998 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
999 msecs_to_jiffies(lp->data->t_tx_timeout));
1000 if (!rc) {
Alexander Aring464f0292014-10-07 10:38:25 +02001001 at86rf230_async_error(lp, ctx, -ETIMEDOUT);
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001002 return -ETIMEDOUT;
1003 }
1004
Alexander Aring850f43a2014-10-07 10:38:27 +02001005 if (lp->max_frame_retries > 0)
1006 return 0;
1007
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001008 /* Interfame spacing time, which is phy depend.
1009 * TODO
1010 * Move this handling in MAC 802.15.4 layer.
1011 * This is currently a workaround to avoid fragmenation issues.
1012 */
1013 if (skb->len > 18)
1014 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
1015 else
1016 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
1017
1018 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001019}
1020
1021static int
1022at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
1023{
1024 might_sleep();
1025 BUG_ON(!level);
1026 *level = 0xbe;
1027 return 0;
1028}
1029
1030static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001031at86rf230_start(struct ieee802154_dev *dev)
1032{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001033 return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001034}
1035
1036static void
1037at86rf230_stop(struct ieee802154_dev *dev)
1038{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001039 at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001040}
1041
1042static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001043at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001044{
1045 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1046}
1047
1048static int
1049at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1050{
1051 int rc;
1052
1053 if (channel == 0)
1054 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1055 else
1056 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1057 if (rc < 0)
1058 return rc;
1059
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001060 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001061 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001062 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001063 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001064 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001065 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001066 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001067 if (rc < 0)
1068 return rc;
1069
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001070 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1071}
1072
1073static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001074at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1075{
1076 struct at86rf230_local *lp = dev->priv;
1077 int rc;
1078
1079 might_sleep();
1080
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001081 if (page < 0 || page > 31 ||
1082 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001083 WARN_ON(1);
1084 return -EINVAL;
1085 }
1086
Alexander Aringa53d1f72014-07-03 00:20:46 +02001087 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001088 if (rc < 0)
1089 return rc;
1090
Alexander Aring984e0c62014-07-03 00:20:53 +02001091 /* Wait for PLL */
1092 usleep_range(lp->data->t_channel_switch,
1093 lp->data->t_channel_switch + 10);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001094 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001095 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001096
1097 return 0;
1098}
1099
1100static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001101at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1102 struct ieee802154_hw_addr_filt *filt,
1103 unsigned long changed)
1104{
1105 struct at86rf230_local *lp = dev->priv;
1106
1107 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001108 u16 addr = le16_to_cpu(filt->short_addr);
1109
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001110 dev_vdbg(&lp->spi->dev,
1111 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001112 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1113 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001114 }
1115
1116 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001117 u16 pan = le16_to_cpu(filt->pan_id);
1118
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001119 dev_vdbg(&lp->spi->dev,
1120 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001121 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1122 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001123 }
1124
1125 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001126 u8 i, addr[8];
1127
1128 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001129 dev_vdbg(&lp->spi->dev,
1130 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001131 for (i = 0; i < 8; i++)
1132 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001133 }
1134
1135 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
1136 dev_vdbg(&lp->spi->dev,
1137 "at86rf230_set_hw_addr_filt called for panc change\n");
1138 if (filt->pan_coord)
1139 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1140 else
1141 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1142 }
1143
1144 return 0;
1145}
1146
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001147static int
Alexander Aring640985e2014-07-03 00:20:43 +02001148at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001149{
1150 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001151
1152 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1153 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1154 * 0dB.
1155 * thus, supported values for db range from -26 to 5, for 31dB of
1156 * reduction to 0dB of reduction.
1157 */
1158 if (db > 5 || db < -26)
1159 return -EINVAL;
1160
1161 db = -(db - 5);
1162
Jean Sacren677676c2014-03-01 15:54:36 -07001163 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001164}
1165
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001166static int
Alexander Aring640985e2014-07-03 00:20:43 +02001167at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001168{
1169 struct at86rf230_local *lp = dev->priv;
1170
1171 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1172}
1173
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001174static int
Alexander Aring640985e2014-07-03 00:20:43 +02001175at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001176{
1177 struct at86rf230_local *lp = dev->priv;
1178
1179 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1180}
1181
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001182static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001183at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1184{
1185 return (level - lp->data->rssi_base_val) * 100 / 207;
1186}
1187
1188static int
1189at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1190{
1191 return (level - lp->data->rssi_base_val) / 2;
1192}
1193
1194static int
Alexander Aring640985e2014-07-03 00:20:43 +02001195at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001196{
1197 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001198
Alexander Aringa53d1f72014-07-03 00:20:46 +02001199 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001200 return -EINVAL;
1201
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001202 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1203 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001204}
1205
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001206static int
Alexander Aring640985e2014-07-03 00:20:43 +02001207at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001208 u8 retries)
1209{
1210 struct at86rf230_local *lp = dev->priv;
1211 int rc;
1212
1213 if (min_be > max_be || max_be > 8 || retries > 5)
1214 return -EINVAL;
1215
1216 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1217 if (rc)
1218 return rc;
1219
1220 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1221 if (rc)
1222 return rc;
1223
Alexander Aring39d7f322014-04-05 13:49:26 +02001224 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001225}
1226
1227static int
Alexander Aring640985e2014-07-03 00:20:43 +02001228at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001229{
1230 struct at86rf230_local *lp = dev->priv;
1231 int rc = 0;
1232
1233 if (retries < -1 || retries > 15)
1234 return -EINVAL;
1235
1236 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001237 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001238
1239 if (retries >= 0)
1240 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1241
1242 return rc;
1243}
1244
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001245static struct ieee802154_ops at86rf230_ops = {
1246 .owner = THIS_MODULE,
1247 .xmit = at86rf230_xmit,
1248 .ed = at86rf230_ed,
1249 .set_channel = at86rf230_channel,
1250 .start = at86rf230_start,
1251 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001252 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001253 .set_txpower = at86rf230_set_txpower,
1254 .set_lbt = at86rf230_set_lbt,
1255 .set_cca_mode = at86rf230_set_cca_mode,
1256 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1257 .set_csma_params = at86rf230_set_csma_params,
1258 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001259};
1260
Alexander Aringa53d1f72014-07-03 00:20:46 +02001261static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001262 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001263 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001264 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001265 .t_off_to_aack = 80,
1266 .t_off_to_tx_on = 80,
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 = -91,
1273 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001274 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001275};
1276
1277static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001278 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001279 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001280 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001281 .t_off_to_aack = 110,
1282 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001283 .t_frame = 4096,
1284 .t_p_ack = 545,
1285 .t_sifs = 192,
1286 .t_lifs = 480,
1287 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001288 .rssi_base_val = -91,
1289 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001290 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001291};
1292
1293static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001294 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001295 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001296 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001297 .t_off_to_aack = 200,
1298 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001299 .t_frame = 4096,
1300 .t_p_ack = 545,
1301 .t_sifs = 192,
1302 .t_lifs = 480,
1303 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001304 .rssi_base_val = -100,
1305 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001306 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001307};
1308
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001309static int at86rf230_hw_init(struct at86rf230_local *lp)
1310{
Alexander Aring1db05582014-07-03 00:20:50 +02001311 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001312 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001313 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001314
Alexander Aring09e536c2014-07-03 00:20:52 +02001315 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001316 if (rc)
1317 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001318
Alexander Aring4af619a2014-04-24 19:09:05 +02001319 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001320 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001321 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001322
Alexander Aring18c65042014-04-24 19:09:18 +02001323 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001324 if (rc)
1325 return rc;
1326
Alexander Aring6bd2b132014-07-03 00:20:49 +02001327 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1328 if (rc)
1329 return rc;
1330
Sascha Herrmann057dad62013-04-14 22:33:29 +00001331 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001332 if (rc)
1333 return rc;
1334
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001335 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1336 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1337 if (rc)
1338 return rc;
1339 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1340 if (rc)
1341 return rc;
1342
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001343 /* CLKM changes are applied immediately */
1344 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1345 if (rc)
1346 return rc;
1347
1348 /* Turn CLKM Off */
1349 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1350 if (rc)
1351 return rc;
1352 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001353 usleep_range(lp->data->t_sleep_cycle,
1354 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001355
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001356 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001357 if (rc)
1358 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001359 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001360 dev_err(&lp->spi->dev, "DVDD error\n");
1361 return -EINVAL;
1362 }
1363
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001364 return 0;
1365}
1366
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001367static struct at86rf230_platform_data *
1368at86rf230_get_pdata(struct spi_device *spi)
1369{
1370 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001371
1372 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1373 return spi->dev.platform_data;
1374
1375 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1376 if (!pdata)
1377 goto done;
1378
1379 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1380 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1381
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001382 spi->dev.platform_data = pdata;
1383done:
1384 return pdata;
1385}
1386
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001387static int
1388at86rf230_detect_device(struct at86rf230_local *lp)
1389{
1390 unsigned int part, version, val;
1391 u16 man_id = 0;
1392 const char *chip;
1393 int rc;
1394
1395 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1396 if (rc)
1397 return rc;
1398 man_id |= val;
1399
1400 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1401 if (rc)
1402 return rc;
1403 man_id |= (val << 8);
1404
1405 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1406 if (rc)
1407 return rc;
1408
1409 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1410 if (rc)
1411 return rc;
1412
1413 if (man_id != 0x001f) {
1414 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1415 man_id >> 8, man_id & 0xFF);
1416 return -EINVAL;
1417 }
1418
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001419 lp->dev->extra_tx_headroom = 0;
1420 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1421 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1422
1423 switch (part) {
1424 case 2:
1425 chip = "at86rf230";
1426 rc = -ENOTSUPP;
1427 break;
1428 case 3:
1429 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001430 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001431 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1432 break;
1433 case 7:
1434 chip = "at86rf212";
1435 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001436 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001437 lp->dev->flags |= IEEE802154_HW_LBT;
1438 lp->dev->phy->channels_supported[0] = 0x00007FF;
1439 lp->dev->phy->channels_supported[2] = 0x00007FF;
1440 } else {
1441 rc = -ENOTSUPP;
1442 }
1443 break;
1444 case 11:
1445 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001446 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001447 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1448 break;
1449 default:
1450 chip = "unkown";
1451 rc = -ENOTSUPP;
1452 break;
1453 }
1454
1455 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1456
1457 return rc;
1458}
1459
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001460static void
1461at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1462{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001463 lp->state.lp = lp;
1464 spi_message_init(&lp->state.msg);
1465 lp->state.msg.context = &lp->state;
1466 lp->state.trx.tx_buf = lp->state.buf;
1467 lp->state.trx.rx_buf = lp->state.buf;
1468 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1469
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001470 lp->irq.lp = lp;
1471 spi_message_init(&lp->irq.msg);
1472 lp->irq.msg.context = &lp->irq;
1473 lp->irq.trx.tx_buf = lp->irq.buf;
1474 lp->irq.trx.rx_buf = lp->irq.buf;
1475 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1476
1477 lp->tx.lp = lp;
1478 spi_message_init(&lp->tx.msg);
1479 lp->tx.msg.context = &lp->tx;
1480 lp->tx.trx.tx_buf = lp->tx.buf;
1481 lp->tx.trx.rx_buf = lp->tx.buf;
1482 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1483}
1484
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001485static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001486{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001487 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001488 struct ieee802154_dev *dev;
1489 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001490 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001491 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001492
1493 if (!spi->irq) {
1494 dev_err(&spi->dev, "no IRQ specified\n");
1495 return -EINVAL;
1496 }
1497
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001498 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001499 if (!pdata) {
1500 dev_err(&spi->dev, "no platform_data\n");
1501 return -EINVAL;
1502 }
1503
Alexander Aring3fa27572014-03-15 09:29:06 +01001504 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001505 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1506 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001507 if (rc)
1508 return rc;
1509 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001510
1511 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001512 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1513 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001514 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001515 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001516 }
1517
1518 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001519 if (gpio_is_valid(pdata->rstn)) {
1520 udelay(1);
1521 gpio_set_value(pdata->rstn, 0);
1522 udelay(1);
1523 gpio_set_value(pdata->rstn, 1);
1524 usleep_range(120, 240);
1525 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001526
Alexander Aring640985e2014-07-03 00:20:43 +02001527 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001528 if (!dev)
1529 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001530
1531 lp = dev->priv;
1532 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001533 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001534 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001535
Alexander Aringf76014f772014-07-03 00:20:44 +02001536 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1537 if (IS_ERR(lp->regmap)) {
1538 rc = PTR_ERR(lp->regmap);
1539 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1540 rc);
1541 goto free_dev;
1542 }
1543
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001544 at86rf230_setup_spi_messages(lp);
1545
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001546 rc = at86rf230_detect_device(lp);
1547 if (rc < 0)
1548 goto free_dev;
1549
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001550 spin_lock_init(&lp->lock);
1551 init_completion(&lp->tx_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001552 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001553
1554 spi_set_drvdata(spi, lp);
1555
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001556 rc = at86rf230_hw_init(lp);
1557 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001558 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001559
Alexander Aring19626942014-04-24 19:09:15 +02001560 /* Read irq status register to reset irq line */
1561 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001562 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001563 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001564
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001565 irq_type = irq_get_trigger_type(spi->irq);
1566 if (!irq_type)
1567 irq_type = IRQF_TRIGGER_RISING;
1568
1569 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1570 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001571 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001572 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001573
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001574 rc = ieee802154_register_device(lp->dev);
1575 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001576 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001577
1578 return rc;
1579
Alexander Aring640985e2014-07-03 00:20:43 +02001580free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001581 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001582
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001583 return rc;
1584}
1585
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001586static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001587{
1588 struct at86rf230_local *lp = spi_get_drvdata(spi);
1589
Alexander Aring17e84a92014-03-31 03:26:51 +02001590 /* mask all at86rf230 irq's */
1591 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001592 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001593 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001594 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001595
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001596 return 0;
1597}
1598
Alexander Aring1086b4f2014-04-24 19:09:11 +02001599static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001600 { .compatible = "atmel,at86rf230", },
1601 { .compatible = "atmel,at86rf231", },
1602 { .compatible = "atmel,at86rf233", },
1603 { .compatible = "atmel,at86rf212", },
1604 { },
1605};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001606MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001607
Alexander Aring90b15522014-04-24 19:09:12 +02001608static const struct spi_device_id at86rf230_device_id[] = {
1609 { .name = "at86rf230", },
1610 { .name = "at86rf231", },
1611 { .name = "at86rf233", },
1612 { .name = "at86rf212", },
1613 { },
1614};
1615MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1616
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001617static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001618 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001619 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001620 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001621 .name = "at86rf230",
1622 .owner = THIS_MODULE,
1623 },
1624 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001625 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001626};
1627
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001628module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001629
1630MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1631MODULE_LICENSE("GPL v2");