blob: cc5efa149da181b031ae7b1be504d297922657dc [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>
Alexander Aringeb3b4352015-03-09 13:56:10 +010022#include <linux/hrtimer.h>
Alexander Aringdce481e2015-03-09 13:56:11 +010023#include <linux/jiffies.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000024#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020025#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000026#include <linux/gpio.h>
27#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000028#include <linux/spinlock.h>
29#include <linux/spi/spi.h>
30#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020031#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000032#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010033#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020034#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000035
36#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020037#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000038
Alexander Aringa53d1f72014-07-03 00:20:46 +020039struct at86rf230_local;
40/* at86rf2xx chip depend data.
41 * All timings are in us.
42 */
43struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020044 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020045 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020046 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020047 u16 t_off_to_aack;
48 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020049 u16 t_frame;
50 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020051 int rssi_base_val;
52
Alexander Aringe37d2ec2014-10-28 18:21:19 +010053 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020054 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020055};
56
Alexander Aringba6d2232015-03-01 21:55:28 +010057#define AT86RF2XX_MAX_BUF (127 + 3)
58/* tx retries to access the TX_ON state
59 * if it's above then force change will be started.
60 *
61 * We assume the max_frame_retries (7) value of 802.15.4 here.
62 */
63#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aringdce481e2015-03-09 13:56:11 +010064/* We use the recommended 5 minutes timeout to recalibrate */
65#define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
Alexander Aring1d15d6b2014-07-03 00:20:48 +020066
67struct at86rf230_state_change {
68 struct at86rf230_local *lp;
Alexander Aringcca990c2015-03-01 21:55:31 +010069 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020070
Alexander Aringeb3b4352015-03-09 13:56:10 +010071 struct hrtimer timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020072 struct spi_message msg;
73 struct spi_transfer trx;
74 u8 buf[AT86RF2XX_MAX_BUF];
75
76 void (*complete)(void *context);
77 u8 from_state;
78 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020079
80 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020081};
82
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000083struct at86rf230_local {
84 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000085
Alexander Aring5a504392014-10-25 17:16:34 +020086 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020088 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000089
Alexander Aring2e0571c2014-07-03 00:20:51 +020090 struct completion state_complete;
91 struct at86rf230_state_change state;
92
Alexander Aring1d15d6b2014-07-03 00:20:48 +020093 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020094
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010095 bool tx_aret;
Alexander Aringdce481e2015-03-09 13:56:11 +010096 unsigned long cal_timeout;
Alexander Aring850f43a2014-10-07 10:38:27 +020097 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020098 bool is_tx;
99 /* spinlock for is_tx protection */
100 spinlock_t lock;
Alexander Aringba6d2232015-03-01 21:55:28 +0100101 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200102 struct sk_buff *tx_skb;
103 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000104};
105
106#define RG_TRX_STATUS (0x01)
107#define SR_TRX_STATUS 0x01, 0x1f, 0
108#define SR_RESERVED_01_3 0x01, 0x20, 5
109#define SR_CCA_STATUS 0x01, 0x40, 6
110#define SR_CCA_DONE 0x01, 0x80, 7
111#define RG_TRX_STATE (0x02)
112#define SR_TRX_CMD 0x02, 0x1f, 0
113#define SR_TRAC_STATUS 0x02, 0xe0, 5
114#define RG_TRX_CTRL_0 (0x03)
115#define SR_CLKM_CTRL 0x03, 0x07, 0
116#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
117#define SR_PAD_IO_CLKM 0x03, 0x30, 4
118#define SR_PAD_IO 0x03, 0xc0, 6
119#define RG_TRX_CTRL_1 (0x04)
120#define SR_IRQ_POLARITY 0x04, 0x01, 0
121#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
122#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
123#define SR_RX_BL_CTRL 0x04, 0x10, 4
124#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
125#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
126#define SR_PA_EXT_EN 0x04, 0x80, 7
127#define RG_PHY_TX_PWR (0x05)
128#define SR_TX_PWR 0x05, 0x0f, 0
129#define SR_PA_LT 0x05, 0x30, 4
130#define SR_PA_BUF_LT 0x05, 0xc0, 6
131#define RG_PHY_RSSI (0x06)
132#define SR_RSSI 0x06, 0x1f, 0
133#define SR_RND_VALUE 0x06, 0x60, 5
134#define SR_RX_CRC_VALID 0x06, 0x80, 7
135#define RG_PHY_ED_LEVEL (0x07)
136#define SR_ED_LEVEL 0x07, 0xff, 0
137#define RG_PHY_CC_CCA (0x08)
138#define SR_CHANNEL 0x08, 0x1f, 0
139#define SR_CCA_MODE 0x08, 0x60, 5
140#define SR_CCA_REQUEST 0x08, 0x80, 7
141#define RG_CCA_THRES (0x09)
142#define SR_CCA_ED_THRES 0x09, 0x0f, 0
143#define SR_RESERVED_09_1 0x09, 0xf0, 4
144#define RG_RX_CTRL (0x0a)
145#define SR_PDT_THRES 0x0a, 0x0f, 0
146#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
147#define RG_SFD_VALUE (0x0b)
148#define SR_SFD_VALUE 0x0b, 0xff, 0
149#define RG_TRX_CTRL_2 (0x0c)
150#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100151#define SR_SUB_MODE 0x0c, 0x04, 2
152#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100153#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
154#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000155#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
156#define RG_ANT_DIV (0x0d)
157#define SR_ANT_CTRL 0x0d, 0x03, 0
158#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
159#define SR_ANT_DIV_EN 0x0d, 0x08, 3
160#define SR_RESERVED_0d_2 0x0d, 0x70, 4
161#define SR_ANT_SEL 0x0d, 0x80, 7
162#define RG_IRQ_MASK (0x0e)
163#define SR_IRQ_MASK 0x0e, 0xff, 0
164#define RG_IRQ_STATUS (0x0f)
165#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
166#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
167#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
168#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
169#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
170#define SR_IRQ_5_AMI 0x0f, 0x20, 5
171#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
172#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
173#define RG_VREG_CTRL (0x10)
174#define SR_RESERVED_10_6 0x10, 0x03, 0
175#define SR_DVDD_OK 0x10, 0x04, 2
176#define SR_DVREG_EXT 0x10, 0x08, 3
177#define SR_RESERVED_10_3 0x10, 0x30, 4
178#define SR_AVDD_OK 0x10, 0x40, 6
179#define SR_AVREG_EXT 0x10, 0x80, 7
180#define RG_BATMON (0x11)
181#define SR_BATMON_VTH 0x11, 0x0f, 0
182#define SR_BATMON_HR 0x11, 0x10, 4
183#define SR_BATMON_OK 0x11, 0x20, 5
184#define SR_RESERVED_11_1 0x11, 0xc0, 6
185#define RG_XOSC_CTRL (0x12)
186#define SR_XTAL_TRIM 0x12, 0x0f, 0
187#define SR_XTAL_MODE 0x12, 0xf0, 4
188#define RG_RX_SYN (0x15)
189#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
190#define SR_RESERVED_15_2 0x15, 0x70, 4
191#define SR_RX_PDT_DIS 0x15, 0x80, 7
192#define RG_XAH_CTRL_1 (0x17)
193#define SR_RESERVED_17_8 0x17, 0x01, 0
194#define SR_AACK_PROM_MODE 0x17, 0x02, 1
195#define SR_AACK_ACK_TIME 0x17, 0x04, 2
196#define SR_RESERVED_17_5 0x17, 0x08, 3
197#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
198#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100199#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000200#define SR_RESERVED_17_1 0x17, 0x80, 7
201#define RG_FTN_CTRL (0x18)
202#define SR_RESERVED_18_2 0x18, 0x7f, 0
203#define SR_FTN_START 0x18, 0x80, 7
204#define RG_PLL_CF (0x1a)
205#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
206#define SR_PLL_CF_START 0x1a, 0x80, 7
207#define RG_PLL_DCU (0x1b)
208#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
209#define SR_RESERVED_1b_2 0x1b, 0x40, 6
210#define SR_PLL_DCU_START 0x1b, 0x80, 7
211#define RG_PART_NUM (0x1c)
212#define SR_PART_NUM 0x1c, 0xff, 0
213#define RG_VERSION_NUM (0x1d)
214#define SR_VERSION_NUM 0x1d, 0xff, 0
215#define RG_MAN_ID_0 (0x1e)
216#define SR_MAN_ID_0 0x1e, 0xff, 0
217#define RG_MAN_ID_1 (0x1f)
218#define SR_MAN_ID_1 0x1f, 0xff, 0
219#define RG_SHORT_ADDR_0 (0x20)
220#define SR_SHORT_ADDR_0 0x20, 0xff, 0
221#define RG_SHORT_ADDR_1 (0x21)
222#define SR_SHORT_ADDR_1 0x21, 0xff, 0
223#define RG_PAN_ID_0 (0x22)
224#define SR_PAN_ID_0 0x22, 0xff, 0
225#define RG_PAN_ID_1 (0x23)
226#define SR_PAN_ID_1 0x23, 0xff, 0
227#define RG_IEEE_ADDR_0 (0x24)
228#define SR_IEEE_ADDR_0 0x24, 0xff, 0
229#define RG_IEEE_ADDR_1 (0x25)
230#define SR_IEEE_ADDR_1 0x25, 0xff, 0
231#define RG_IEEE_ADDR_2 (0x26)
232#define SR_IEEE_ADDR_2 0x26, 0xff, 0
233#define RG_IEEE_ADDR_3 (0x27)
234#define SR_IEEE_ADDR_3 0x27, 0xff, 0
235#define RG_IEEE_ADDR_4 (0x28)
236#define SR_IEEE_ADDR_4 0x28, 0xff, 0
237#define RG_IEEE_ADDR_5 (0x29)
238#define SR_IEEE_ADDR_5 0x29, 0xff, 0
239#define RG_IEEE_ADDR_6 (0x2a)
240#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
241#define RG_IEEE_ADDR_7 (0x2b)
242#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
243#define RG_XAH_CTRL_0 (0x2c)
244#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
245#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
246#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
247#define RG_CSMA_SEED_0 (0x2d)
248#define SR_CSMA_SEED_0 0x2d, 0xff, 0
249#define RG_CSMA_SEED_1 (0x2e)
250#define SR_CSMA_SEED_1 0x2e, 0x07, 0
251#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
252#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
253#define SR_AACK_SET_PD 0x2e, 0x20, 5
254#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
255#define RG_CSMA_BE (0x2f)
256#define SR_MIN_BE 0x2f, 0x0f, 0
257#define SR_MAX_BE 0x2f, 0xf0, 4
258
259#define CMD_REG 0x80
260#define CMD_REG_MASK 0x3f
261#define CMD_WRITE 0x40
262#define CMD_FB 0x20
263
264#define IRQ_BAT_LOW (1 << 7)
265#define IRQ_TRX_UR (1 << 6)
266#define IRQ_AMI (1 << 5)
267#define IRQ_CCA_ED (1 << 4)
268#define IRQ_TRX_END (1 << 3)
269#define IRQ_RX_START (1 << 2)
270#define IRQ_PLL_UNL (1 << 1)
271#define IRQ_PLL_LOCK (1 << 0)
272
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000273#define IRQ_ACTIVE_HIGH 0
274#define IRQ_ACTIVE_LOW 1
275
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000276#define STATE_P_ON 0x00 /* BUSY */
277#define STATE_BUSY_RX 0x01
278#define STATE_BUSY_TX 0x02
279#define STATE_FORCE_TRX_OFF 0x03
280#define STATE_FORCE_TX_ON 0x04 /* IDLE */
281/* 0x05 */ /* INVALID_PARAMETER */
282#define STATE_RX_ON 0x06
283/* 0x07 */ /* SUCCESS */
284#define STATE_TRX_OFF 0x08
285#define STATE_TX_ON 0x09
286/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
287#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500288#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000289#define STATE_BUSY_RX_AACK 0x11
290#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000291#define STATE_RX_AACK_ON 0x16
292#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000293#define STATE_RX_ON_NOCLK 0x1C
294#define STATE_RX_AACK_ON_NOCLK 0x1D
295#define STATE_BUSY_RX_AACK_NOCLK 0x1E
296#define STATE_TRANSITION_IN_PROGRESS 0x1F
297
Alexander Aringf76014f772014-07-03 00:20:44 +0200298#define AT86RF2XX_NUMREGS 0x3F
299
Alexander Aring97fed792014-10-07 10:38:32 +0200300static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200301at86rf230_async_state_change(struct at86rf230_local *lp,
302 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200303 const u8 state, void (*complete)(void *context),
304 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200305
Alexander Aringf76014f772014-07-03 00:20:44 +0200306static inline int
307__at86rf230_write(struct at86rf230_local *lp,
308 unsigned int addr, unsigned int data)
309{
310 return regmap_write(lp->regmap, addr, data);
311}
312
313static inline int
314__at86rf230_read(struct at86rf230_local *lp,
315 unsigned int addr, unsigned int *data)
316{
317 return regmap_read(lp->regmap, addr, data);
318}
319
320static inline int
321at86rf230_read_subreg(struct at86rf230_local *lp,
322 unsigned int addr, unsigned int mask,
323 unsigned int shift, unsigned int *data)
324{
325 int rc;
326
327 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100328 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200329 *data = (*data & mask) >> shift;
330
331 return rc;
332}
333
334static inline int
335at86rf230_write_subreg(struct at86rf230_local *lp,
336 unsigned int addr, unsigned int mask,
337 unsigned int shift, unsigned int data)
338{
339 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
340}
341
342static bool
343at86rf230_reg_writeable(struct device *dev, unsigned int reg)
344{
345 switch (reg) {
346 case RG_TRX_STATE:
347 case RG_TRX_CTRL_0:
348 case RG_TRX_CTRL_1:
349 case RG_PHY_TX_PWR:
350 case RG_PHY_ED_LEVEL:
351 case RG_PHY_CC_CCA:
352 case RG_CCA_THRES:
353 case RG_RX_CTRL:
354 case RG_SFD_VALUE:
355 case RG_TRX_CTRL_2:
356 case RG_ANT_DIV:
357 case RG_IRQ_MASK:
358 case RG_VREG_CTRL:
359 case RG_BATMON:
360 case RG_XOSC_CTRL:
361 case RG_RX_SYN:
362 case RG_XAH_CTRL_1:
363 case RG_FTN_CTRL:
364 case RG_PLL_CF:
365 case RG_PLL_DCU:
366 case RG_SHORT_ADDR_0:
367 case RG_SHORT_ADDR_1:
368 case RG_PAN_ID_0:
369 case RG_PAN_ID_1:
370 case RG_IEEE_ADDR_0:
371 case RG_IEEE_ADDR_1:
372 case RG_IEEE_ADDR_2:
373 case RG_IEEE_ADDR_3:
374 case RG_IEEE_ADDR_4:
375 case RG_IEEE_ADDR_5:
376 case RG_IEEE_ADDR_6:
377 case RG_IEEE_ADDR_7:
378 case RG_XAH_CTRL_0:
379 case RG_CSMA_SEED_0:
380 case RG_CSMA_SEED_1:
381 case RG_CSMA_BE:
382 return true;
383 default:
384 return false;
385 }
386}
387
388static bool
389at86rf230_reg_readable(struct device *dev, unsigned int reg)
390{
391 bool rc;
392
393 /* all writeable are also readable */
394 rc = at86rf230_reg_writeable(dev, reg);
395 if (rc)
396 return rc;
397
398 /* readonly regs */
399 switch (reg) {
400 case RG_TRX_STATUS:
401 case RG_PHY_RSSI:
402 case RG_IRQ_STATUS:
403 case RG_PART_NUM:
404 case RG_VERSION_NUM:
405 case RG_MAN_ID_1:
406 case RG_MAN_ID_0:
407 return true;
408 default:
409 return false;
410 }
411}
412
413static bool
414at86rf230_reg_volatile(struct device *dev, unsigned int reg)
415{
416 /* can be changed during runtime */
417 switch (reg) {
418 case RG_TRX_STATUS:
419 case RG_TRX_STATE:
420 case RG_PHY_RSSI:
421 case RG_PHY_ED_LEVEL:
422 case RG_IRQ_STATUS:
423 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100424 case RG_PLL_CF:
425 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200426 return true;
427 default:
428 return false;
429 }
430}
431
432static bool
433at86rf230_reg_precious(struct device *dev, unsigned int reg)
434{
435 /* don't clear irq line on read */
436 switch (reg) {
437 case RG_IRQ_STATUS:
438 return true;
439 default:
440 return false;
441 }
442}
443
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100444static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200445 .reg_bits = 8,
446 .val_bits = 8,
447 .write_flag_mask = CMD_REG | CMD_WRITE,
448 .read_flag_mask = CMD_REG,
449 .cache_type = REGCACHE_RBTREE,
450 .max_register = AT86RF2XX_NUMREGS,
451 .writeable_reg = at86rf230_reg_writeable,
452 .readable_reg = at86rf230_reg_readable,
453 .volatile_reg = at86rf230_reg_volatile,
454 .precious_reg = at86rf230_reg_precious,
455};
456
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200457static void
458at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000459{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200460 struct at86rf230_state_change *ctx = context;
461 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000462
Alexander Aring97fed792014-10-07 10:38:32 +0200463 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100464 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000466
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100467static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200468at86rf230_async_error(struct at86rf230_local *lp,
469 struct at86rf230_state_change *ctx, int rc)
470{
471 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000472
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200473 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200474 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200475}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000476
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200477/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200478static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200479at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
480 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200481 void (*complete)(void *context),
482 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200483{
Alexander Aring97fed792014-10-07 10:38:32 +0200484 int rc;
485
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200486 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000487
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200488 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200489 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200490 ctx->irq_enable = irq_enable;
491 rc = spi_async(lp->spi, &ctx->msg);
492 if (rc) {
493 if (irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100494 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200495
496 at86rf230_async_error(lp, ctx, rc);
497 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200498}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000499
Alexander Aringdce481e2015-03-09 13:56:11 +0100500static inline u8 at86rf230_state_to_force(u8 state)
501{
502 if (state == STATE_TX_ON)
503 return STATE_FORCE_TX_ON;
504 else
505 return STATE_FORCE_TRX_OFF;
506}
507
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200508static void
509at86rf230_async_state_assert(void *context)
510{
511 struct at86rf230_state_change *ctx = context;
512 struct at86rf230_local *lp = ctx->lp;
513 const u8 *buf = ctx->buf;
514 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000515
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200516 /* Assert state change */
517 if (trx_state != ctx->to_state) {
518 /* Special handling if transceiver state is in
519 * STATE_BUSY_RX_AACK and a SHR was detected.
520 */
521 if (trx_state == STATE_BUSY_RX_AACK) {
522 /* Undocumented race condition. If we send a state
523 * change to STATE_RX_AACK_ON the transceiver could
524 * change his state automatically to STATE_BUSY_RX_AACK
525 * if a SHR was detected. This is not an error, but we
526 * can't assert this.
527 */
528 if (ctx->to_state == STATE_RX_AACK_ON)
529 goto done;
530
531 /* If we change to STATE_TX_ON without forcing and
532 * transceiver state is STATE_BUSY_RX_AACK, we wait
533 * 'tFrame + tPAck' receiving time. In this time the
534 * PDU should be received. If the transceiver is still
535 * in STATE_BUSY_RX_AACK, we run a force state change
536 * to STATE_TX_ON. This is a timeout handling, if the
537 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100538 *
539 * Additional we do several retries to try to get into
540 * TX_ON state without forcing. If the retries are
541 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
542 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200543 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100544 if (ctx->to_state == STATE_TX_ON ||
545 ctx->to_state == STATE_TRX_OFF) {
546 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100547
548 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringdce481e2015-03-09 13:56:11 +0100549 state = at86rf230_state_to_force(state);
Alexander Aringba6d2232015-03-01 21:55:28 +0100550 lp->tx_retry++;
551
552 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200553 ctx->complete,
554 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200555 return;
556 }
557 }
558
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200559 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
560 ctx->from_state, ctx->to_state, trx_state);
561 }
562
563done:
564 if (ctx->complete)
565 ctx->complete(context);
566}
567
Alexander Aringeb3b4352015-03-09 13:56:10 +0100568static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
569{
570 struct at86rf230_state_change *ctx =
571 container_of(timer, struct at86rf230_state_change, timer);
572 struct at86rf230_local *lp = ctx->lp;
573
574 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
575 at86rf230_async_state_assert,
576 ctx->irq_enable);
577
578 return HRTIMER_NORESTART;
579}
580
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200581/* Do state change timing delay. */
582static void
583at86rf230_async_state_delay(void *context)
584{
585 struct at86rf230_state_change *ctx = context;
586 struct at86rf230_local *lp = ctx->lp;
587 struct at86rf2xx_chip_data *c = lp->data;
588 bool force = false;
Alexander Aringeb3b4352015-03-09 13:56:10 +0100589 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200590
591 /* The force state changes are will show as normal states in the
592 * state status subregister. We change the to_state to the
593 * corresponding one and remember if it was a force change, this
594 * differs if we do a state change from STATE_BUSY_RX_AACK.
595 */
596 switch (ctx->to_state) {
597 case STATE_FORCE_TX_ON:
598 ctx->to_state = STATE_TX_ON;
599 force = true;
600 break;
601 case STATE_FORCE_TRX_OFF:
602 ctx->to_state = STATE_TRX_OFF;
603 force = true;
604 break;
605 default:
606 break;
607 }
608
609 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200610 case STATE_TRX_OFF:
611 switch (ctx->to_state) {
612 case STATE_RX_AACK_ON:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100613 tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200614 goto change;
615 case STATE_TX_ON:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100616 tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
Alexander Aringdce481e2015-03-09 13:56:11 +0100617 /* state change from TRX_OFF to TX_ON to do a
618 * calibration, we need to reset the timeout for the
619 * next one.
620 */
621 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200622 goto change;
623 default:
624 break;
625 }
626 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200627 case STATE_BUSY_RX_AACK:
628 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100629 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200630 case STATE_TX_ON:
631 /* Wait for worst case receiving time if we
632 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100633 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200634 */
635 if (!force) {
Alexander Aringeb3b4352015-03-09 13:56:10 +0100636 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
637 NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200638 goto change;
639 }
640 break;
641 default:
642 break;
643 }
644 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200645 /* Default value, means RESET state */
646 case STATE_P_ON:
647 switch (ctx->to_state) {
648 case STATE_TRX_OFF:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100649 tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
Alexander Aring09e536c2014-07-03 00:20:52 +0200650 goto change;
651 default:
652 break;
653 }
654 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200655 default:
656 break;
657 }
658
659 /* Default delay is 1us in the most cases */
Alexander Aringeb3b4352015-03-09 13:56:10 +0100660 tim = ktime_set(0, NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200661
662change:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100663 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200664}
665
666static void
667at86rf230_async_state_change_start(void *context)
668{
669 struct at86rf230_state_change *ctx = context;
670 struct at86rf230_local *lp = ctx->lp;
671 u8 *buf = ctx->buf;
672 const u8 trx_state = buf[1] & 0x1f;
673 int rc;
674
675 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
676 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
677 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200678 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
679 at86rf230_async_state_change_start,
680 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200681 return;
682 }
683
684 /* Check if we already are in the state which we change in */
685 if (trx_state == ctx->to_state) {
686 if (ctx->complete)
687 ctx->complete(context);
688 return;
689 }
690
691 /* Set current state to the context of state change */
692 ctx->from_state = trx_state;
693
694 /* Going into the next step for a state change which do a timing
695 * relevant delay.
696 */
697 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
698 buf[1] = ctx->to_state;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200699 ctx->msg.complete = at86rf230_async_state_delay;
700 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200701 if (rc) {
702 if (ctx->irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100703 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200704
Alexander Aring4fef7d32014-12-15 10:25:55 +0100705 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200706 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000707}
708
Alexander Aring97fed792014-10-07 10:38:32 +0200709static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200710at86rf230_async_state_change(struct at86rf230_local *lp,
711 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200712 const u8 state, void (*complete)(void *context),
713 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000714{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200715 /* Initialization for the state change context */
716 ctx->to_state = state;
717 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200718 ctx->irq_enable = irq_enable;
719 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
720 at86rf230_async_state_change_start,
721 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200722}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000723
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200724static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200725at86rf230_sync_state_change_complete(void *context)
726{
727 struct at86rf230_state_change *ctx = context;
728 struct at86rf230_local *lp = ctx->lp;
729
730 complete(&lp->state_complete);
731}
732
733/* This function do a sync framework above the async state change.
734 * Some callbacks of the IEEE 802.15.4 driver interface need to be
735 * handled synchronously.
736 */
737static int
738at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
739{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100740 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200741
Alexander Aring97fed792014-10-07 10:38:32 +0200742 at86rf230_async_state_change(lp, &lp->state, state,
743 at86rf230_sync_state_change_complete,
744 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200745
746 rc = wait_for_completion_timeout(&lp->state_complete,
747 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200748 if (!rc) {
749 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200750 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200751 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200752
753 return 0;
754}
755
756static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200757at86rf230_tx_complete(void *context)
758{
759 struct at86rf230_state_change *ctx = context;
760 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000761
Alexander Aringcca990c2015-03-01 21:55:31 +0100762 enable_irq(ctx->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100763
Alexander Aringef5428a2015-03-01 21:55:29 +0100764 ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200765}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000766
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200767static void
768at86rf230_tx_on(void *context)
769{
770 struct at86rf230_state_change *ctx = context;
771 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000772
Alexander Aring31fa7432015-03-01 21:55:32 +0100773 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200774 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200775}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000776
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200777static void
778at86rf230_tx_trac_error(void *context)
779{
780 struct at86rf230_state_change *ctx = context;
781 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000782
Alexander Aring97fed792014-10-07 10:38:32 +0200783 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
784 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200785}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000786
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200787static void
788at86rf230_tx_trac_check(void *context)
789{
790 struct at86rf230_state_change *ctx = context;
791 struct at86rf230_local *lp = ctx->lp;
792 const u8 *buf = ctx->buf;
793 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000794
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200795 /* If trac status is different than zero we need to do a state change
796 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
797 * state to TX_ON.
798 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100799 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200800 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
801 at86rf230_tx_trac_error, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100802 else
803 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200804}
805
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200806static void
807at86rf230_tx_trac_status(void *context)
808{
809 struct at86rf230_state_change *ctx = context;
810 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200811
Alexander Aring97fed792014-10-07 10:38:32 +0200812 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
813 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200814}
815
816static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100817at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200818{
Alexander Aring74de4c82015-03-01 21:55:30 +0100819 struct at86rf230_state_change *ctx = context;
820 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200821 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
Alexander Aring31fa7432015-03-01 21:55:32 +0100822 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100823 struct sk_buff *skb;
824 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200825
Alexander Aring74de4c82015-03-01 21:55:30 +0100826 len = buf[1];
827 if (!ieee802154_is_valid_psdu_len(len)) {
828 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
829 len = IEEE802154_MTU;
830 }
831 lqi = buf[2 + len];
832
833 memcpy(rx_local_buf, buf + 2, len);
Alexander Aring263be332015-03-01 21:55:33 +0100834 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100835 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200836
Alexander Aring61a22812014-10-27 17:13:29 +0100837 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200838 if (!skb) {
839 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
840 return;
841 }
842
843 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100844 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200845}
846
847static void
Alexander Aringcca990c2015-03-01 21:55:31 +0100848at86rf230_rx_read_frame(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200849{
Alexander Aringcca990c2015-03-01 21:55:31 +0100850 struct at86rf230_state_change *ctx = context;
851 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100852 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200853 int rc;
854
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100856 ctx->trx.len = AT86RF2XX_MAX_BUF;
857 ctx->msg.complete = at86rf230_rx_read_frame_complete;
858 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200859 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100860 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100861 enable_irq(ctx->irq);
Alexander Aring31fa7432015-03-01 21:55:32 +0100862 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200863 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200864}
865
866static void
867at86rf230_rx_trac_check(void *context)
868{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200869 /* Possible check on trac status here. This could be useful to make
870 * some stats why receive is failed. Not used at the moment, but it's
871 * maybe timing relevant. Datasheet doesn't say anything about this.
872 * The programming guide say do it so.
873 */
874
Alexander Aringcca990c2015-03-01 21:55:31 +0100875 at86rf230_rx_read_frame(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200876}
877
Alexander Aring97fed792014-10-07 10:38:32 +0200878static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200879at86rf230_irq_trx_end(struct at86rf230_local *lp)
880{
881 spin_lock(&lp->lock);
882 if (lp->is_tx) {
883 lp->is_tx = 0;
884 spin_unlock(&lp->lock);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200885
886 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200887 at86rf230_async_state_change(lp, &lp->irq,
888 STATE_FORCE_TX_ON,
889 at86rf230_tx_trac_status,
890 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200891 else
Alexander Aring97fed792014-10-07 10:38:32 +0200892 at86rf230_async_state_change(lp, &lp->irq,
893 STATE_RX_AACK_ON,
894 at86rf230_tx_complete,
895 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200896 } else {
897 spin_unlock(&lp->lock);
Alexander Aring97fed792014-10-07 10:38:32 +0200898 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
899 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200900 }
901}
902
903static void
904at86rf230_irq_status(void *context)
905{
906 struct at86rf230_state_change *ctx = context;
907 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100908 const u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200909 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200910
911 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200912 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200913 } else {
Alexander Aringcca990c2015-03-01 21:55:31 +0100914 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200915 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
916 irq);
917 }
918}
919
920static irqreturn_t at86rf230_isr(int irq, void *data)
921{
922 struct at86rf230_local *lp = data;
923 struct at86rf230_state_change *ctx = &lp->irq;
924 u8 *buf = ctx->buf;
925 int rc;
926
Alexander Aring90566362014-10-07 10:38:29 +0200927 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200928
929 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200930 ctx->msg.complete = at86rf230_irq_status;
931 rc = spi_async(lp->spi, &ctx->msg);
932 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200933 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200934 at86rf230_async_error(lp, ctx, rc);
935 return IRQ_NONE;
936 }
937
938 return IRQ_HANDLED;
939}
940
941static void
942at86rf230_write_frame_complete(void *context)
943{
944 struct at86rf230_state_change *ctx = context;
945 struct at86rf230_local *lp = ctx->lp;
946 u8 *buf = ctx->buf;
947 int rc;
948
949 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
950 buf[1] = STATE_BUSY_TX;
951 ctx->trx.len = 2;
952 ctx->msg.complete = NULL;
953 rc = spi_async(lp->spi, &ctx->msg);
954 if (rc)
955 at86rf230_async_error(lp, ctx, rc);
956}
957
958static void
959at86rf230_write_frame(void *context)
960{
961 struct at86rf230_state_change *ctx = context;
962 struct at86rf230_local *lp = ctx->lp;
963 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100964 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200965 int rc;
966
967 spin_lock(&lp->lock);
968 lp->is_tx = 1;
969 spin_unlock(&lp->lock);
970
971 buf[0] = CMD_FB | CMD_WRITE;
972 buf[1] = skb->len + 2;
973 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100974 ctx->trx.len = skb->len + 2;
975 ctx->msg.complete = at86rf230_write_frame_complete;
976 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100977 if (rc) {
978 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200979 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100980 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200981}
982
983static void
984at86rf230_xmit_tx_on(void *context)
985{
986 struct at86rf230_state_change *ctx = context;
987 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200988
Alexander Aring97fed792014-10-07 10:38:32 +0200989 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
990 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200991}
992
Alexander Aringdce481e2015-03-09 13:56:11 +0100993static void
994at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200995{
Alexander Aringdce481e2015-03-09 13:56:11 +0100996 struct at86rf230_state_change *ctx = context;
997 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200998
999 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
1000 * are in STATE_TX_ON. The pfad differs here, so we change
1001 * the complete handler.
1002 */
1003 if (lp->tx_aret)
Alexander Aringdce481e2015-03-09 13:56:11 +01001004 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
1005 at86rf230_xmit_tx_on, false);
1006 else
1007 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
1008 at86rf230_write_frame, false);
1009}
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001010
Alexander Aringdce481e2015-03-09 13:56:11 +01001011static int
1012at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
1013{
1014 struct at86rf230_local *lp = hw->priv;
1015 struct at86rf230_state_change *ctx = &lp->tx;
1016
1017 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +01001018 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +01001019
1020 /* After 5 minutes in PLL and the same frequency we run again the
1021 * calibration loops which is recommended by at86rf2xx datasheets.
1022 *
1023 * The calibration is initiate by a state change from TRX_OFF
1024 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
1025 * function then to start in the next 5 minutes.
1026 */
1027 if (time_is_before_jiffies(lp->cal_timeout))
1028 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
1029 at86rf230_xmit_start, false);
1030 else
1031 at86rf230_xmit_start(ctx);
Alexander Aring97fed792014-10-07 10:38:32 +02001032
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001033 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001034}
1035
1036static int
Alexander Aring5a504392014-10-25 17:16:34 +02001037at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001038{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001039 BUG_ON(!level);
1040 *level = 0xbe;
1041 return 0;
1042}
1043
1044static int
Alexander Aring5a504392014-10-25 17:16:34 +02001045at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001046{
Alexander Aringdce481e2015-03-09 13:56:11 +01001047 struct at86rf230_local *lp = hw->priv;
1048
1049 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring5a504392014-10-25 17:16:34 +02001050 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001051}
1052
1053static void
Alexander Aring5a504392014-10-25 17:16:34 +02001054at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001055{
Alexander Aring5a504392014-10-25 17:16:34 +02001056 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001057}
1058
1059static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001060at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001061{
1062 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1063}
1064
1065static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001066at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001067{
1068 int rc;
1069
1070 if (channel == 0)
1071 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1072 else
1073 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1074 if (rc < 0)
1075 return rc;
1076
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001077 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001078 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001079 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001080 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001081 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001082 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001083 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001084 if (rc < 0)
1085 return rc;
1086
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001087 /* This sets the symbol_duration according frequency on the 212.
1088 * TODO move this handling while set channel and page in cfg802154.
1089 * We can do that, this timings are according 802.15.4 standard.
1090 * If we do that in cfg802154, this is a more generic calculation.
1091 *
1092 * This should also protected from ifs_timer. Means cancel timer and
1093 * init with a new value. For now, this is okay.
1094 */
1095 if (channel == 0) {
1096 if (page == 0) {
1097 /* SUB:0 and BPSK:0 -> BPSK-20 */
1098 lp->hw->phy->symbol_duration = 50;
1099 } else {
1100 /* SUB:1 and BPSK:0 -> BPSK-40 */
1101 lp->hw->phy->symbol_duration = 25;
1102 }
1103 } else {
1104 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001105 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001106 lp->hw->phy->symbol_duration = 40;
1107 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001108 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001109 lp->hw->phy->symbol_duration = 16;
1110 }
1111
1112 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1113 lp->hw->phy->symbol_duration;
1114 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1115 lp->hw->phy->symbol_duration;
1116
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001117 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1118}
1119
1120static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001121at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001122{
Alexander Aring5a504392014-10-25 17:16:34 +02001123 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001124 int rc;
1125
Alexander Aringa53d1f72014-07-03 00:20:46 +02001126 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001127 /* Wait for PLL */
1128 usleep_range(lp->data->t_channel_switch,
1129 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001130
1131 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001132 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001133}
1134
1135static int
Alexander Aring5a504392014-10-25 17:16:34 +02001136at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001137 struct ieee802154_hw_addr_filt *filt,
1138 unsigned long changed)
1139{
Alexander Aring5a504392014-10-25 17:16:34 +02001140 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001141
Alexander Aring57205c12014-10-25 05:25:09 +02001142 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001143 u16 addr = le16_to_cpu(filt->short_addr);
1144
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001145 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001146 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001147 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1148 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001149 }
1150
Alexander Aring57205c12014-10-25 05:25:09 +02001151 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001152 u16 pan = le16_to_cpu(filt->pan_id);
1153
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001154 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001155 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001156 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1157 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001158 }
1159
Alexander Aring57205c12014-10-25 05:25:09 +02001160 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001161 u8 i, addr[8];
1162
1163 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001164 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001165 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001166 for (i = 0; i < 8; i++)
1167 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001168 }
1169
Alexander Aring57205c12014-10-25 05:25:09 +02001170 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001171 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001172 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001173 if (filt->pan_coord)
1174 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1175 else
1176 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1177 }
1178
1179 return 0;
1180}
1181
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001182static int
Alexander Aring5a504392014-10-25 17:16:34 +02001183at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001184{
Alexander Aring5a504392014-10-25 17:16:34 +02001185 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001186
1187 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1188 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1189 * 0dB.
1190 * thus, supported values for db range from -26 to 5, for 31dB of
1191 * reduction to 0dB of reduction.
1192 */
1193 if (db > 5 || db < -26)
1194 return -EINVAL;
1195
1196 db = -(db - 5);
1197
Jean Sacren677676c2014-03-01 15:54:36 -07001198 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001199}
1200
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001201static int
Alexander Aring5a504392014-10-25 17:16:34 +02001202at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001203{
Alexander Aring5a504392014-10-25 17:16:34 +02001204 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001205
1206 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1207}
1208
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001209static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001210at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1211 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001212{
Alexander Aring5a504392014-10-25 17:16:34 +02001213 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001214 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001215
Alexander Aring7fe9a382014-12-10 15:33:12 +01001216 /* mapping 802.15.4 to driver spec */
1217 switch (cca->mode) {
1218 case NL802154_CCA_ENERGY:
1219 val = 1;
1220 break;
1221 case NL802154_CCA_CARRIER:
1222 val = 2;
1223 break;
1224 case NL802154_CCA_ENERGY_CARRIER:
1225 switch (cca->opt) {
1226 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1227 val = 3;
1228 break;
1229 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1230 val = 0;
1231 break;
1232 default:
1233 return -EINVAL;
1234 }
1235 break;
1236 default:
1237 return -EINVAL;
1238 }
1239
1240 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001241}
1242
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001243static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001244at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1245{
1246 return (level - lp->data->rssi_base_val) * 100 / 207;
1247}
1248
1249static int
1250at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1251{
1252 return (level - lp->data->rssi_base_val) / 2;
1253}
1254
1255static int
Alexander Aring5a504392014-10-25 17:16:34 +02001256at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001257{
Alexander Aring5a504392014-10-25 17:16:34 +02001258 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001259
Alexander Aringa53d1f72014-07-03 00:20:46 +02001260 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001261 return -EINVAL;
1262
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001263 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1264 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001265}
1266
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001267static int
Alexander Aring5a504392014-10-25 17:16:34 +02001268at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001269 u8 retries)
1270{
Alexander Aring5a504392014-10-25 17:16:34 +02001271 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001272 int rc;
1273
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001274 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1275 if (rc)
1276 return rc;
1277
1278 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1279 if (rc)
1280 return rc;
1281
Alexander Aring39d7f322014-04-05 13:49:26 +02001282 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001283}
1284
1285static int
Alexander Aring5a504392014-10-25 17:16:34 +02001286at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001287{
Alexander Aring5a504392014-10-25 17:16:34 +02001288 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001289 int rc = 0;
1290
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001291 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001292 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001293
1294 if (retries >= 0)
1295 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1296
1297 return rc;
1298}
1299
Alexander Aring92f45f52014-10-29 21:34:33 +01001300static int
1301at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1302{
1303 struct at86rf230_local *lp = hw->priv;
1304 int rc;
1305
1306 if (on) {
1307 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1308 if (rc < 0)
1309 return rc;
1310
1311 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1312 if (rc < 0)
1313 return rc;
1314 } else {
1315 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1316 if (rc < 0)
1317 return rc;
1318
1319 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1320 if (rc < 0)
1321 return rc;
1322 }
1323
1324 return 0;
1325}
1326
Alexander Aring16301862014-10-28 18:21:18 +01001327static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001328 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001329 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001330 .ed = at86rf230_ed,
1331 .set_channel = at86rf230_channel,
1332 .start = at86rf230_start,
1333 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001334 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001335 .set_txpower = at86rf230_set_txpower,
1336 .set_lbt = at86rf230_set_lbt,
1337 .set_cca_mode = at86rf230_set_cca_mode,
1338 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1339 .set_csma_params = at86rf230_set_csma_params,
1340 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f52014-10-29 21:34:33 +01001341 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001342};
1343
Alexander Aringa53d1f72014-07-03 00:20:46 +02001344static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001345 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001346 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001347 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001348 .t_off_to_aack = 80,
1349 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001350 .t_frame = 4096,
1351 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001352 .rssi_base_val = -91,
1353 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001354 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001355};
1356
1357static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001358 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001359 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001360 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001361 .t_off_to_aack = 110,
1362 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001363 .t_frame = 4096,
1364 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001365 .rssi_base_val = -91,
1366 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001367 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001368};
1369
1370static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001371 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001372 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001373 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001374 .t_off_to_aack = 200,
1375 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001376 .t_frame = 4096,
1377 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001378 .rssi_base_val = -100,
1379 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001380 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001381};
1382
Alexander Aringccdaeb22015-02-27 09:58:26 +01001383static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001384{
Alexander Aring1db05582014-07-03 00:20:50 +02001385 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001386 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001387 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001388
Alexander Aring09e536c2014-07-03 00:20:52 +02001389 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001390 if (rc)
1391 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001392
Alexander Aring4af619a2014-04-24 19:09:05 +02001393 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001394 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1395 irq_type == IRQ_TYPE_EDGE_FALLING)
1396 dev_warn(&lp->spi->dev,
1397 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001398 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1399 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001400 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001401
Alexander Aring18c65042014-04-24 19:09:18 +02001402 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001403 if (rc)
1404 return rc;
1405
Alexander Aring6bd2b132014-07-03 00:20:49 +02001406 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1407 if (rc)
1408 return rc;
1409
Sascha Herrmann057dad62013-04-14 22:33:29 +00001410 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001411 if (rc)
1412 return rc;
1413
Alexander Aringbe64f072015-02-27 09:58:28 +01001414 /* reset values differs in at86rf231 and at86rf233 */
1415 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1416 if (rc)
1417 return rc;
1418
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001419 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1420 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1421 if (rc)
1422 return rc;
1423 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1424 if (rc)
1425 return rc;
1426
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001427 /* CLKM changes are applied immediately */
1428 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1429 if (rc)
1430 return rc;
1431
1432 /* Turn CLKM Off */
1433 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1434 if (rc)
1435 return rc;
1436 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001437 usleep_range(lp->data->t_sleep_cycle,
1438 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001439
Alexander Aringccdaeb22015-02-27 09:58:26 +01001440 /* xtal_trim value is calculated by:
1441 * CL = 0.5 * (CX + CTRIM + CPAR)
1442 *
1443 * whereas:
1444 * CL = capacitor of used crystal
1445 * CX = connected capacitors at xtal pins
1446 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1447 * but this is different on each board setup. You need to fine
1448 * tuning this value via CTRIM.
1449 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1450 * 0 pF upto 4.5 pF.
1451 *
1452 * Examples:
1453 * atben transceiver:
1454 *
1455 * CL = 8 pF
1456 * CX = 12 pF
1457 * CPAR = 3 pF (We assume the magic constant from datasheet)
1458 * CTRIM = 0.9 pF
1459 *
1460 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1461 *
1462 * xtal_trim = 0x3
1463 *
1464 * openlabs transceiver:
1465 *
1466 * CL = 16 pF
1467 * CX = 22 pF
1468 * CPAR = 3 pF (We assume the magic constant from datasheet)
1469 * CTRIM = 4.5 pF
1470 *
1471 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1472 *
1473 * xtal_trim = 0xf
1474 */
1475 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1476 if (rc)
1477 return rc;
1478
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001479 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001480 if (rc)
1481 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001482 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001483 dev_err(&lp->spi->dev, "DVDD error\n");
1484 return -EINVAL;
1485 }
1486
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001487 /* Force setting slotted operation bit to 0. Sometimes the atben
1488 * sets this bit and I don't know why. We set this always force
1489 * to zero while probing.
1490 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001491 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001492}
1493
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001494static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001495at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1496 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001497{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001498 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001499 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001500
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001501 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1502 if (!pdata)
1503 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001504
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001505 *rstn = pdata->rstn;
1506 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001507 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001508 return 0;
1509 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001510
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001511 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1512 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001513 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1514 if (ret < 0 && ret != -EINVAL)
1515 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001516
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001517 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001518}
1519
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001520static int
1521at86rf230_detect_device(struct at86rf230_local *lp)
1522{
1523 unsigned int part, version, val;
1524 u16 man_id = 0;
1525 const char *chip;
1526 int rc;
1527
1528 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1529 if (rc)
1530 return rc;
1531 man_id |= val;
1532
1533 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1534 if (rc)
1535 return rc;
1536 man_id |= (val << 8);
1537
1538 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1539 if (rc)
1540 return rc;
1541
Andrey Yurovsky75989682014-12-17 13:14:42 -08001542 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001543 if (rc)
1544 return rc;
1545
1546 if (man_id != 0x001f) {
1547 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1548 man_id >> 8, man_id & 0xFF);
1549 return -EINVAL;
1550 }
1551
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001552 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8fc84e2014-10-29 21:34:31 +01001553 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
Alexander Aring92f45f52014-10-29 21:34:33 +01001554 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001555
Alexander Aringb48a7c12014-12-10 15:33:14 +01001556 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1557
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001558 switch (part) {
1559 case 2:
1560 chip = "at86rf230";
1561 rc = -ENOTSUPP;
1562 break;
1563 case 3:
1564 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001565 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001566 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001567 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001568 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001569 break;
1570 case 7:
1571 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001572 lp->data = &at86rf212_data;
1573 lp->hw->flags |= IEEE802154_HW_LBT;
1574 lp->hw->phy->channels_supported[0] = 0x00007FF;
1575 lp->hw->phy->channels_supported[2] = 0x00007FF;
1576 lp->hw->phy->current_channel = 5;
1577 lp->hw->phy->symbol_duration = 25;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001578 break;
1579 case 11:
1580 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001581 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001582 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001583 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001584 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001585 break;
1586 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001587 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001588 rc = -ENOTSUPP;
1589 break;
1590 }
1591
1592 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1593
1594 return rc;
1595}
1596
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001597static void
1598at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1599{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001600 lp->state.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001601 lp->state.irq = lp->spi->irq;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001602 spi_message_init(&lp->state.msg);
1603 lp->state.msg.context = &lp->state;
Alexander Aring263be332015-03-01 21:55:33 +01001604 lp->state.trx.len = 2;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001605 lp->state.trx.tx_buf = lp->state.buf;
1606 lp->state.trx.rx_buf = lp->state.buf;
1607 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001608 hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1609 lp->state.timer.function = at86rf230_async_state_timer;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001610
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001611 lp->irq.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001612 lp->irq.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001613 spi_message_init(&lp->irq.msg);
1614 lp->irq.msg.context = &lp->irq;
Alexander Aring263be332015-03-01 21:55:33 +01001615 lp->irq.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001616 lp->irq.trx.tx_buf = lp->irq.buf;
1617 lp->irq.trx.rx_buf = lp->irq.buf;
1618 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001619 hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1620 lp->irq.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001621
1622 lp->tx.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001623 lp->tx.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001624 spi_message_init(&lp->tx.msg);
1625 lp->tx.msg.context = &lp->tx;
Alexander Aring263be332015-03-01 21:55:33 +01001626 lp->tx.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001627 lp->tx.trx.tx_buf = lp->tx.buf;
1628 lp->tx.trx.rx_buf = lp->tx.buf;
1629 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001630 hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1631 lp->tx.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001632}
1633
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001634static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001635{
Alexander Aring5a504392014-10-25 17:16:34 +02001636 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001637 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001638 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001639 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001640 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001641
1642 if (!spi->irq) {
1643 dev_err(&spi->dev, "no IRQ specified\n");
1644 return -EINVAL;
1645 }
1646
Alexander Aringccdaeb22015-02-27 09:58:26 +01001647 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001648 if (rc < 0) {
1649 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1650 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001651 }
1652
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001653 if (gpio_is_valid(rstn)) {
1654 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001655 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001656 if (rc)
1657 return rc;
1658 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001659
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001660 if (gpio_is_valid(slp_tr)) {
1661 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001662 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001663 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001664 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001665 }
1666
1667 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001668 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001669 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001670 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001671 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001672 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001673 usleep_range(120, 240);
1674 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001675
Alexander Aring5a504392014-10-25 17:16:34 +02001676 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1677 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001678 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001679
Alexander Aring5a504392014-10-25 17:16:34 +02001680 lp = hw->priv;
1681 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001682 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001683 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001684 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001685 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001686
Alexander Aringf76014f772014-07-03 00:20:44 +02001687 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1688 if (IS_ERR(lp->regmap)) {
1689 rc = PTR_ERR(lp->regmap);
1690 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1691 rc);
1692 goto free_dev;
1693 }
1694
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001695 at86rf230_setup_spi_messages(lp);
1696
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001697 rc = at86rf230_detect_device(lp);
1698 if (rc < 0)
1699 goto free_dev;
1700
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001701 spin_lock_init(&lp->lock);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001702 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001703
1704 spi_set_drvdata(spi, lp);
1705
Alexander Aringccdaeb22015-02-27 09:58:26 +01001706 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001707 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001708 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001709
Alexander Aring19626942014-04-24 19:09:15 +02001710 /* Read irq status register to reset irq line */
1711 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001712 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001713 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001714
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001715 irq_type = irq_get_trigger_type(spi->irq);
1716 if (!irq_type)
1717 irq_type = IRQF_TRIGGER_RISING;
1718
1719 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1720 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001721 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001722 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001723
Alexander Aring5a504392014-10-25 17:16:34 +02001724 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001725 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001726 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001727
1728 return rc;
1729
Alexander Aring640985e2014-07-03 00:20:43 +02001730free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001731 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001732
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001733 return rc;
1734}
1735
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001736static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001737{
1738 struct at86rf230_local *lp = spi_get_drvdata(spi);
1739
Alexander Aring17e84a92014-03-31 03:26:51 +02001740 /* mask all at86rf230 irq's */
1741 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001742 ieee802154_unregister_hw(lp->hw);
1743 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001744 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001745
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001746 return 0;
1747}
1748
Alexander Aring1086b4f2014-04-24 19:09:11 +02001749static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001750 { .compatible = "atmel,at86rf230", },
1751 { .compatible = "atmel,at86rf231", },
1752 { .compatible = "atmel,at86rf233", },
1753 { .compatible = "atmel,at86rf212", },
1754 { },
1755};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001756MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001757
Alexander Aring90b15522014-04-24 19:09:12 +02001758static const struct spi_device_id at86rf230_device_id[] = {
1759 { .name = "at86rf230", },
1760 { .name = "at86rf231", },
1761 { .name = "at86rf233", },
1762 { .name = "at86rf212", },
1763 { },
1764};
1765MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1766
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001767static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001768 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001769 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001770 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001771 .name = "at86rf230",
1772 .owner = THIS_MODULE,
1773 },
1774 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001775 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001776};
1777
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001778module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001779
1780MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1781MODULE_LICENSE("GPL v2");