blob: 1278dd5ee18726653a0121078e00106a93a1a924 [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/spi/spi.h>
29#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020030#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000031#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010032#include <linux/of_gpio.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020033#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000034
35#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020036#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000037
Alexander Aringa53d1f72014-07-03 00:20:46 +020038struct at86rf230_local;
39/* at86rf2xx chip depend data.
40 * All timings are in us.
41 */
42struct at86rf2xx_chip_data {
Alexander Aring7a4ef912014-07-03 00:20:54 +020043 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020044 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020045 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020046 u16 t_off_to_aack;
47 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020048 u16 t_frame;
49 u16 t_p_ack;
Alexander Aringa53d1f72014-07-03 00:20:46 +020050 int rssi_base_val;
51
Alexander Aringe37d2ec2014-10-28 18:21:19 +010052 int (*set_channel)(struct at86rf230_local *, u8, u8);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020053 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020054};
55
Alexander Aringba6d2232015-03-01 21:55:28 +010056#define AT86RF2XX_MAX_BUF (127 + 3)
57/* tx retries to access the TX_ON state
58 * if it's above then force change will be started.
59 *
60 * We assume the max_frame_retries (7) value of 802.15.4 here.
61 */
62#define AT86RF2XX_MAX_TX_RETRIES 7
Alexander Aringdce481e2015-03-09 13:56:11 +010063/* We use the recommended 5 minutes timeout to recalibrate */
64#define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
Alexander Aring1d15d6b2014-07-03 00:20:48 +020065
66struct at86rf230_state_change {
67 struct at86rf230_local *lp;
Alexander Aringcca990c2015-03-01 21:55:31 +010068 int irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020069
Alexander Aringeb3b4352015-03-09 13:56:10 +010070 struct hrtimer timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020071 struct spi_message msg;
72 struct spi_transfer trx;
73 u8 buf[AT86RF2XX_MAX_BUF];
74
75 void (*complete)(void *context);
76 u8 from_state;
77 u8 to_state;
Alexander Aring97fed792014-10-07 10:38:32 +020078
79 bool irq_enable;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020080};
81
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000082struct at86rf230_local {
83 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000084
Alexander Aring5a504392014-10-25 17:16:34 +020085 struct ieee802154_hw *hw;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020086 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020087 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000088
Alexander Aring2e0571c2014-07-03 00:20:51 +020089 struct completion state_complete;
90 struct at86rf230_state_change state;
91
Alexander Aring1d15d6b2014-07-03 00:20:48 +020092 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020093
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010094 bool tx_aret;
Alexander Aringdce481e2015-03-09 13:56:11 +010095 unsigned long cal_timeout;
Alexander Aring850f43a2014-10-07 10:38:27 +020096 s8 max_frame_retries;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020097 bool is_tx;
Alexander Aringba6d2232015-03-01 21:55:28 +010098 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020099 struct sk_buff *tx_skb;
100 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000101};
102
103#define RG_TRX_STATUS (0x01)
104#define SR_TRX_STATUS 0x01, 0x1f, 0
105#define SR_RESERVED_01_3 0x01, 0x20, 5
106#define SR_CCA_STATUS 0x01, 0x40, 6
107#define SR_CCA_DONE 0x01, 0x80, 7
108#define RG_TRX_STATE (0x02)
109#define SR_TRX_CMD 0x02, 0x1f, 0
110#define SR_TRAC_STATUS 0x02, 0xe0, 5
111#define RG_TRX_CTRL_0 (0x03)
112#define SR_CLKM_CTRL 0x03, 0x07, 0
113#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
114#define SR_PAD_IO_CLKM 0x03, 0x30, 4
115#define SR_PAD_IO 0x03, 0xc0, 6
116#define RG_TRX_CTRL_1 (0x04)
117#define SR_IRQ_POLARITY 0x04, 0x01, 0
118#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
119#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
120#define SR_RX_BL_CTRL 0x04, 0x10, 4
121#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
122#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
123#define SR_PA_EXT_EN 0x04, 0x80, 7
124#define RG_PHY_TX_PWR (0x05)
125#define SR_TX_PWR 0x05, 0x0f, 0
126#define SR_PA_LT 0x05, 0x30, 4
127#define SR_PA_BUF_LT 0x05, 0xc0, 6
128#define RG_PHY_RSSI (0x06)
129#define SR_RSSI 0x06, 0x1f, 0
130#define SR_RND_VALUE 0x06, 0x60, 5
131#define SR_RX_CRC_VALID 0x06, 0x80, 7
132#define RG_PHY_ED_LEVEL (0x07)
133#define SR_ED_LEVEL 0x07, 0xff, 0
134#define RG_PHY_CC_CCA (0x08)
135#define SR_CHANNEL 0x08, 0x1f, 0
136#define SR_CCA_MODE 0x08, 0x60, 5
137#define SR_CCA_REQUEST 0x08, 0x80, 7
138#define RG_CCA_THRES (0x09)
139#define SR_CCA_ED_THRES 0x09, 0x0f, 0
140#define SR_RESERVED_09_1 0x09, 0xf0, 4
141#define RG_RX_CTRL (0x0a)
142#define SR_PDT_THRES 0x0a, 0x0f, 0
143#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
144#define RG_SFD_VALUE (0x0b)
145#define SR_SFD_VALUE 0x0b, 0xff, 0
146#define RG_TRX_CTRL_2 (0x0c)
147#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100148#define SR_SUB_MODE 0x0c, 0x04, 2
149#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100150#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
151#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000152#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
153#define RG_ANT_DIV (0x0d)
154#define SR_ANT_CTRL 0x0d, 0x03, 0
155#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
156#define SR_ANT_DIV_EN 0x0d, 0x08, 3
157#define SR_RESERVED_0d_2 0x0d, 0x70, 4
158#define SR_ANT_SEL 0x0d, 0x80, 7
159#define RG_IRQ_MASK (0x0e)
160#define SR_IRQ_MASK 0x0e, 0xff, 0
161#define RG_IRQ_STATUS (0x0f)
162#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
163#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
164#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
165#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
166#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
167#define SR_IRQ_5_AMI 0x0f, 0x20, 5
168#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
169#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
170#define RG_VREG_CTRL (0x10)
171#define SR_RESERVED_10_6 0x10, 0x03, 0
172#define SR_DVDD_OK 0x10, 0x04, 2
173#define SR_DVREG_EXT 0x10, 0x08, 3
174#define SR_RESERVED_10_3 0x10, 0x30, 4
175#define SR_AVDD_OK 0x10, 0x40, 6
176#define SR_AVREG_EXT 0x10, 0x80, 7
177#define RG_BATMON (0x11)
178#define SR_BATMON_VTH 0x11, 0x0f, 0
179#define SR_BATMON_HR 0x11, 0x10, 4
180#define SR_BATMON_OK 0x11, 0x20, 5
181#define SR_RESERVED_11_1 0x11, 0xc0, 6
182#define RG_XOSC_CTRL (0x12)
183#define SR_XTAL_TRIM 0x12, 0x0f, 0
184#define SR_XTAL_MODE 0x12, 0xf0, 4
185#define RG_RX_SYN (0x15)
186#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
187#define SR_RESERVED_15_2 0x15, 0x70, 4
188#define SR_RX_PDT_DIS 0x15, 0x80, 7
189#define RG_XAH_CTRL_1 (0x17)
190#define SR_RESERVED_17_8 0x17, 0x01, 0
191#define SR_AACK_PROM_MODE 0x17, 0x02, 1
192#define SR_AACK_ACK_TIME 0x17, 0x04, 2
193#define SR_RESERVED_17_5 0x17, 0x08, 3
194#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
195#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100196#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000197#define SR_RESERVED_17_1 0x17, 0x80, 7
198#define RG_FTN_CTRL (0x18)
199#define SR_RESERVED_18_2 0x18, 0x7f, 0
200#define SR_FTN_START 0x18, 0x80, 7
201#define RG_PLL_CF (0x1a)
202#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
203#define SR_PLL_CF_START 0x1a, 0x80, 7
204#define RG_PLL_DCU (0x1b)
205#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
206#define SR_RESERVED_1b_2 0x1b, 0x40, 6
207#define SR_PLL_DCU_START 0x1b, 0x80, 7
208#define RG_PART_NUM (0x1c)
209#define SR_PART_NUM 0x1c, 0xff, 0
210#define RG_VERSION_NUM (0x1d)
211#define SR_VERSION_NUM 0x1d, 0xff, 0
212#define RG_MAN_ID_0 (0x1e)
213#define SR_MAN_ID_0 0x1e, 0xff, 0
214#define RG_MAN_ID_1 (0x1f)
215#define SR_MAN_ID_1 0x1f, 0xff, 0
216#define RG_SHORT_ADDR_0 (0x20)
217#define SR_SHORT_ADDR_0 0x20, 0xff, 0
218#define RG_SHORT_ADDR_1 (0x21)
219#define SR_SHORT_ADDR_1 0x21, 0xff, 0
220#define RG_PAN_ID_0 (0x22)
221#define SR_PAN_ID_0 0x22, 0xff, 0
222#define RG_PAN_ID_1 (0x23)
223#define SR_PAN_ID_1 0x23, 0xff, 0
224#define RG_IEEE_ADDR_0 (0x24)
225#define SR_IEEE_ADDR_0 0x24, 0xff, 0
226#define RG_IEEE_ADDR_1 (0x25)
227#define SR_IEEE_ADDR_1 0x25, 0xff, 0
228#define RG_IEEE_ADDR_2 (0x26)
229#define SR_IEEE_ADDR_2 0x26, 0xff, 0
230#define RG_IEEE_ADDR_3 (0x27)
231#define SR_IEEE_ADDR_3 0x27, 0xff, 0
232#define RG_IEEE_ADDR_4 (0x28)
233#define SR_IEEE_ADDR_4 0x28, 0xff, 0
234#define RG_IEEE_ADDR_5 (0x29)
235#define SR_IEEE_ADDR_5 0x29, 0xff, 0
236#define RG_IEEE_ADDR_6 (0x2a)
237#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
238#define RG_IEEE_ADDR_7 (0x2b)
239#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
240#define RG_XAH_CTRL_0 (0x2c)
241#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
242#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
243#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
244#define RG_CSMA_SEED_0 (0x2d)
245#define SR_CSMA_SEED_0 0x2d, 0xff, 0
246#define RG_CSMA_SEED_1 (0x2e)
247#define SR_CSMA_SEED_1 0x2e, 0x07, 0
248#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
249#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
250#define SR_AACK_SET_PD 0x2e, 0x20, 5
251#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
252#define RG_CSMA_BE (0x2f)
253#define SR_MIN_BE 0x2f, 0x0f, 0
254#define SR_MAX_BE 0x2f, 0xf0, 4
255
256#define CMD_REG 0x80
257#define CMD_REG_MASK 0x3f
258#define CMD_WRITE 0x40
259#define CMD_FB 0x20
260
261#define IRQ_BAT_LOW (1 << 7)
262#define IRQ_TRX_UR (1 << 6)
263#define IRQ_AMI (1 << 5)
264#define IRQ_CCA_ED (1 << 4)
265#define IRQ_TRX_END (1 << 3)
266#define IRQ_RX_START (1 << 2)
267#define IRQ_PLL_UNL (1 << 1)
268#define IRQ_PLL_LOCK (1 << 0)
269
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000270#define IRQ_ACTIVE_HIGH 0
271#define IRQ_ACTIVE_LOW 1
272
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000273#define STATE_P_ON 0x00 /* BUSY */
274#define STATE_BUSY_RX 0x01
275#define STATE_BUSY_TX 0x02
276#define STATE_FORCE_TRX_OFF 0x03
277#define STATE_FORCE_TX_ON 0x04 /* IDLE */
278/* 0x05 */ /* INVALID_PARAMETER */
279#define STATE_RX_ON 0x06
280/* 0x07 */ /* SUCCESS */
281#define STATE_TRX_OFF 0x08
282#define STATE_TX_ON 0x09
283/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
284#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500285#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000286#define STATE_BUSY_RX_AACK 0x11
287#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000288#define STATE_RX_AACK_ON 0x16
289#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000290#define STATE_RX_ON_NOCLK 0x1C
291#define STATE_RX_AACK_ON_NOCLK 0x1D
292#define STATE_BUSY_RX_AACK_NOCLK 0x1E
293#define STATE_TRANSITION_IN_PROGRESS 0x1F
294
Alexander Aringf76014f772014-07-03 00:20:44 +0200295#define AT86RF2XX_NUMREGS 0x3F
296
Alexander Aring97fed792014-10-07 10:38:32 +0200297static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200298at86rf230_async_state_change(struct at86rf230_local *lp,
299 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200300 const u8 state, void (*complete)(void *context),
301 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200302
Alexander Aringf76014f772014-07-03 00:20:44 +0200303static inline int
304__at86rf230_write(struct at86rf230_local *lp,
305 unsigned int addr, unsigned int data)
306{
307 return regmap_write(lp->regmap, addr, data);
308}
309
310static inline int
311__at86rf230_read(struct at86rf230_local *lp,
312 unsigned int addr, unsigned int *data)
313{
314 return regmap_read(lp->regmap, addr, data);
315}
316
317static inline int
318at86rf230_read_subreg(struct at86rf230_local *lp,
319 unsigned int addr, unsigned int mask,
320 unsigned int shift, unsigned int *data)
321{
322 int rc;
323
324 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100325 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200326 *data = (*data & mask) >> shift;
327
328 return rc;
329}
330
331static inline int
332at86rf230_write_subreg(struct at86rf230_local *lp,
333 unsigned int addr, unsigned int mask,
334 unsigned int shift, unsigned int data)
335{
336 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
337}
338
339static bool
340at86rf230_reg_writeable(struct device *dev, unsigned int reg)
341{
342 switch (reg) {
343 case RG_TRX_STATE:
344 case RG_TRX_CTRL_0:
345 case RG_TRX_CTRL_1:
346 case RG_PHY_TX_PWR:
347 case RG_PHY_ED_LEVEL:
348 case RG_PHY_CC_CCA:
349 case RG_CCA_THRES:
350 case RG_RX_CTRL:
351 case RG_SFD_VALUE:
352 case RG_TRX_CTRL_2:
353 case RG_ANT_DIV:
354 case RG_IRQ_MASK:
355 case RG_VREG_CTRL:
356 case RG_BATMON:
357 case RG_XOSC_CTRL:
358 case RG_RX_SYN:
359 case RG_XAH_CTRL_1:
360 case RG_FTN_CTRL:
361 case RG_PLL_CF:
362 case RG_PLL_DCU:
363 case RG_SHORT_ADDR_0:
364 case RG_SHORT_ADDR_1:
365 case RG_PAN_ID_0:
366 case RG_PAN_ID_1:
367 case RG_IEEE_ADDR_0:
368 case RG_IEEE_ADDR_1:
369 case RG_IEEE_ADDR_2:
370 case RG_IEEE_ADDR_3:
371 case RG_IEEE_ADDR_4:
372 case RG_IEEE_ADDR_5:
373 case RG_IEEE_ADDR_6:
374 case RG_IEEE_ADDR_7:
375 case RG_XAH_CTRL_0:
376 case RG_CSMA_SEED_0:
377 case RG_CSMA_SEED_1:
378 case RG_CSMA_BE:
379 return true;
380 default:
381 return false;
382 }
383}
384
385static bool
386at86rf230_reg_readable(struct device *dev, unsigned int reg)
387{
388 bool rc;
389
390 /* all writeable are also readable */
391 rc = at86rf230_reg_writeable(dev, reg);
392 if (rc)
393 return rc;
394
395 /* readonly regs */
396 switch (reg) {
397 case RG_TRX_STATUS:
398 case RG_PHY_RSSI:
399 case RG_IRQ_STATUS:
400 case RG_PART_NUM:
401 case RG_VERSION_NUM:
402 case RG_MAN_ID_1:
403 case RG_MAN_ID_0:
404 return true;
405 default:
406 return false;
407 }
408}
409
410static bool
411at86rf230_reg_volatile(struct device *dev, unsigned int reg)
412{
413 /* can be changed during runtime */
414 switch (reg) {
415 case RG_TRX_STATUS:
416 case RG_TRX_STATE:
417 case RG_PHY_RSSI:
418 case RG_PHY_ED_LEVEL:
419 case RG_IRQ_STATUS:
420 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100421 case RG_PLL_CF:
422 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200423 return true;
424 default:
425 return false;
426 }
427}
428
429static bool
430at86rf230_reg_precious(struct device *dev, unsigned int reg)
431{
432 /* don't clear irq line on read */
433 switch (reg) {
434 case RG_IRQ_STATUS:
435 return true;
436 default:
437 return false;
438 }
439}
440
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100441static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200442 .reg_bits = 8,
443 .val_bits = 8,
444 .write_flag_mask = CMD_REG | CMD_WRITE,
445 .read_flag_mask = CMD_REG,
446 .cache_type = REGCACHE_RBTREE,
447 .max_register = AT86RF2XX_NUMREGS,
448 .writeable_reg = at86rf230_reg_writeable,
449 .readable_reg = at86rf230_reg_readable,
450 .volatile_reg = at86rf230_reg_volatile,
451 .precious_reg = at86rf230_reg_precious,
452};
453
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200454static void
455at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000456{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200457 struct at86rf230_state_change *ctx = context;
458 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000459
Alexander Aring97fed792014-10-07 10:38:32 +0200460 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100461 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200462}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000463
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100464static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465at86rf230_async_error(struct at86rf230_local *lp,
466 struct at86rf230_state_change *ctx, int rc)
467{
468 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000469
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200470 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200471 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200472}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000473
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200474/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200475static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200476at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
477 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200478 void (*complete)(void *context),
479 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200480{
Alexander Aring97fed792014-10-07 10:38:32 +0200481 int rc;
482
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200483 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000484
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200485 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200486 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200487 ctx->irq_enable = irq_enable;
488 rc = spi_async(lp->spi, &ctx->msg);
489 if (rc) {
490 if (irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100491 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200492
493 at86rf230_async_error(lp, ctx, rc);
494 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200495}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000496
Alexander Aringdce481e2015-03-09 13:56:11 +0100497static inline u8 at86rf230_state_to_force(u8 state)
498{
499 if (state == STATE_TX_ON)
500 return STATE_FORCE_TX_ON;
501 else
502 return STATE_FORCE_TRX_OFF;
503}
504
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200505static void
506at86rf230_async_state_assert(void *context)
507{
508 struct at86rf230_state_change *ctx = context;
509 struct at86rf230_local *lp = ctx->lp;
510 const u8 *buf = ctx->buf;
511 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000512
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200513 /* Assert state change */
514 if (trx_state != ctx->to_state) {
515 /* Special handling if transceiver state is in
516 * STATE_BUSY_RX_AACK and a SHR was detected.
517 */
518 if (trx_state == STATE_BUSY_RX_AACK) {
519 /* Undocumented race condition. If we send a state
520 * change to STATE_RX_AACK_ON the transceiver could
521 * change his state automatically to STATE_BUSY_RX_AACK
522 * if a SHR was detected. This is not an error, but we
523 * can't assert this.
524 */
525 if (ctx->to_state == STATE_RX_AACK_ON)
526 goto done;
527
528 /* If we change to STATE_TX_ON without forcing and
529 * transceiver state is STATE_BUSY_RX_AACK, we wait
530 * 'tFrame + tPAck' receiving time. In this time the
531 * PDU should be received. If the transceiver is still
532 * in STATE_BUSY_RX_AACK, we run a force state change
533 * to STATE_TX_ON. This is a timeout handling, if the
534 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100535 *
536 * Additional we do several retries to try to get into
537 * TX_ON state without forcing. If the retries are
538 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
539 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200540 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100541 if (ctx->to_state == STATE_TX_ON ||
542 ctx->to_state == STATE_TRX_OFF) {
543 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100544
545 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringdce481e2015-03-09 13:56:11 +0100546 state = at86rf230_state_to_force(state);
Alexander Aringba6d2232015-03-01 21:55:28 +0100547 lp->tx_retry++;
548
549 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200550 ctx->complete,
551 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200552 return;
553 }
554 }
555
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200556 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
557 ctx->from_state, ctx->to_state, trx_state);
558 }
559
560done:
561 if (ctx->complete)
562 ctx->complete(context);
563}
564
Alexander Aringeb3b4352015-03-09 13:56:10 +0100565static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
566{
567 struct at86rf230_state_change *ctx =
568 container_of(timer, struct at86rf230_state_change, timer);
569 struct at86rf230_local *lp = ctx->lp;
570
571 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
572 at86rf230_async_state_assert,
573 ctx->irq_enable);
574
575 return HRTIMER_NORESTART;
576}
577
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200578/* Do state change timing delay. */
579static void
580at86rf230_async_state_delay(void *context)
581{
582 struct at86rf230_state_change *ctx = context;
583 struct at86rf230_local *lp = ctx->lp;
584 struct at86rf2xx_chip_data *c = lp->data;
585 bool force = false;
Alexander Aringeb3b4352015-03-09 13:56:10 +0100586 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200587
588 /* The force state changes are will show as normal states in the
589 * state status subregister. We change the to_state to the
590 * corresponding one and remember if it was a force change, this
591 * differs if we do a state change from STATE_BUSY_RX_AACK.
592 */
593 switch (ctx->to_state) {
594 case STATE_FORCE_TX_ON:
595 ctx->to_state = STATE_TX_ON;
596 force = true;
597 break;
598 case STATE_FORCE_TRX_OFF:
599 ctx->to_state = STATE_TRX_OFF;
600 force = true;
601 break;
602 default:
603 break;
604 }
605
606 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200607 case STATE_TRX_OFF:
608 switch (ctx->to_state) {
609 case STATE_RX_AACK_ON:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100610 tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200611 goto change;
612 case STATE_TX_ON:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100613 tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
Alexander Aringdce481e2015-03-09 13:56:11 +0100614 /* state change from TRX_OFF to TX_ON to do a
615 * calibration, we need to reset the timeout for the
616 * next one.
617 */
618 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200619 goto change;
620 default:
621 break;
622 }
623 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200624 case STATE_BUSY_RX_AACK:
625 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100626 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200627 case STATE_TX_ON:
628 /* Wait for worst case receiving time if we
629 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100630 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200631 */
632 if (!force) {
Alexander Aringeb3b4352015-03-09 13:56:10 +0100633 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
634 NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200635 goto change;
636 }
637 break;
638 default:
639 break;
640 }
641 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200642 /* Default value, means RESET state */
643 case STATE_P_ON:
644 switch (ctx->to_state) {
645 case STATE_TRX_OFF:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100646 tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
Alexander Aring09e536c2014-07-03 00:20:52 +0200647 goto change;
648 default:
649 break;
650 }
651 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200652 default:
653 break;
654 }
655
656 /* Default delay is 1us in the most cases */
Alexander Aringeb3b4352015-03-09 13:56:10 +0100657 tim = ktime_set(0, NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200658
659change:
Alexander Aringeb3b4352015-03-09 13:56:10 +0100660 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200661}
662
663static void
664at86rf230_async_state_change_start(void *context)
665{
666 struct at86rf230_state_change *ctx = context;
667 struct at86rf230_local *lp = ctx->lp;
668 u8 *buf = ctx->buf;
669 const u8 trx_state = buf[1] & 0x1f;
670 int rc;
671
672 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
673 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
674 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200675 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
676 at86rf230_async_state_change_start,
677 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200678 return;
679 }
680
681 /* Check if we already are in the state which we change in */
682 if (trx_state == ctx->to_state) {
683 if (ctx->complete)
684 ctx->complete(context);
685 return;
686 }
687
688 /* Set current state to the context of state change */
689 ctx->from_state = trx_state;
690
691 /* Going into the next step for a state change which do a timing
692 * relevant delay.
693 */
694 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
695 buf[1] = ctx->to_state;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200696 ctx->msg.complete = at86rf230_async_state_delay;
697 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200698 if (rc) {
699 if (ctx->irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100700 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200701
Alexander Aring4fef7d32014-12-15 10:25:55 +0100702 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200703 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000704}
705
Alexander Aring97fed792014-10-07 10:38:32 +0200706static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200707at86rf230_async_state_change(struct at86rf230_local *lp,
708 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200709 const u8 state, void (*complete)(void *context),
710 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000711{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200712 /* Initialization for the state change context */
713 ctx->to_state = state;
714 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200715 ctx->irq_enable = irq_enable;
716 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
717 at86rf230_async_state_change_start,
718 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200719}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000720
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200721static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200722at86rf230_sync_state_change_complete(void *context)
723{
724 struct at86rf230_state_change *ctx = context;
725 struct at86rf230_local *lp = ctx->lp;
726
727 complete(&lp->state_complete);
728}
729
730/* This function do a sync framework above the async state change.
731 * Some callbacks of the IEEE 802.15.4 driver interface need to be
732 * handled synchronously.
733 */
734static int
735at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
736{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100737 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200738
Alexander Aring97fed792014-10-07 10:38:32 +0200739 at86rf230_async_state_change(lp, &lp->state, state,
740 at86rf230_sync_state_change_complete,
741 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200742
743 rc = wait_for_completion_timeout(&lp->state_complete,
744 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200745 if (!rc) {
746 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200747 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200748 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200749
750 return 0;
751}
752
753static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200754at86rf230_tx_complete(void *context)
755{
756 struct at86rf230_state_change *ctx = context;
757 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000758
Alexander Aringcca990c2015-03-01 21:55:31 +0100759 enable_irq(ctx->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100760
Alexander Aringef5428a2015-03-01 21:55:29 +0100761 ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200762}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000763
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200764static void
765at86rf230_tx_on(void *context)
766{
767 struct at86rf230_state_change *ctx = context;
768 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000769
Alexander Aring31fa7432015-03-01 21:55:32 +0100770 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200771 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200772}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000773
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200774static void
775at86rf230_tx_trac_error(void *context)
776{
777 struct at86rf230_state_change *ctx = context;
778 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000779
Alexander Aring97fed792014-10-07 10:38:32 +0200780 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
781 at86rf230_tx_on, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200782}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000783
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200784static void
785at86rf230_tx_trac_check(void *context)
786{
787 struct at86rf230_state_change *ctx = context;
788 struct at86rf230_local *lp = ctx->lp;
789 const u8 *buf = ctx->buf;
790 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000791
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200792 /* If trac status is different than zero we need to do a state change
793 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
794 * state to TX_ON.
795 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100796 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200797 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
798 at86rf230_tx_trac_error, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100799 else
800 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200801}
802
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200803static void
804at86rf230_tx_trac_status(void *context)
805{
806 struct at86rf230_state_change *ctx = context;
807 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200808
Alexander Aring97fed792014-10-07 10:38:32 +0200809 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
810 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200811}
812
813static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100814at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200815{
Alexander Aring74de4c82015-03-01 21:55:30 +0100816 struct at86rf230_state_change *ctx = context;
817 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200818 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
Alexander Aring31fa7432015-03-01 21:55:32 +0100819 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100820 struct sk_buff *skb;
821 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200822
Alexander Aring74de4c82015-03-01 21:55:30 +0100823 len = buf[1];
824 if (!ieee802154_is_valid_psdu_len(len)) {
825 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
826 len = IEEE802154_MTU;
827 }
828 lqi = buf[2 + len];
829
830 memcpy(rx_local_buf, buf + 2, len);
Alexander Aring263be332015-03-01 21:55:33 +0100831 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100832 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200833
Alexander Aring61a22812014-10-27 17:13:29 +0100834 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200835 if (!skb) {
836 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
837 return;
838 }
839
840 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100841 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200842}
843
844static void
Alexander Aringcca990c2015-03-01 21:55:31 +0100845at86rf230_rx_read_frame(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200846{
Alexander Aringcca990c2015-03-01 21:55:31 +0100847 struct at86rf230_state_change *ctx = context;
848 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100849 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200850 int rc;
851
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200852 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100853 ctx->trx.len = AT86RF2XX_MAX_BUF;
854 ctx->msg.complete = at86rf230_rx_read_frame_complete;
855 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200856 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100857 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100858 enable_irq(ctx->irq);
Alexander Aring31fa7432015-03-01 21:55:32 +0100859 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200860 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200861}
862
863static void
864at86rf230_rx_trac_check(void *context)
865{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200866 /* Possible check on trac status here. This could be useful to make
867 * some stats why receive is failed. Not used at the moment, but it's
868 * maybe timing relevant. Datasheet doesn't say anything about this.
869 * The programming guide say do it so.
870 */
871
Alexander Aringcca990c2015-03-01 21:55:31 +0100872 at86rf230_rx_read_frame(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200873}
874
Alexander Aring97fed792014-10-07 10:38:32 +0200875static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200876at86rf230_irq_trx_end(struct at86rf230_local *lp)
877{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200878 if (lp->is_tx) {
879 lp->is_tx = 0;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200880
881 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200882 at86rf230_async_state_change(lp, &lp->irq,
883 STATE_FORCE_TX_ON,
884 at86rf230_tx_trac_status,
885 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200886 else
Alexander Aring97fed792014-10-07 10:38:32 +0200887 at86rf230_async_state_change(lp, &lp->irq,
888 STATE_RX_AACK_ON,
889 at86rf230_tx_complete,
890 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200891 } else {
Alexander Aring97fed792014-10-07 10:38:32 +0200892 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
893 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200894 }
895}
896
897static void
898at86rf230_irq_status(void *context)
899{
900 struct at86rf230_state_change *ctx = context;
901 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100902 const u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200903 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200904
905 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200906 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200907 } else {
Alexander Aringcca990c2015-03-01 21:55:31 +0100908 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200909 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
910 irq);
911 }
912}
913
914static irqreturn_t at86rf230_isr(int irq, void *data)
915{
916 struct at86rf230_local *lp = data;
917 struct at86rf230_state_change *ctx = &lp->irq;
918 u8 *buf = ctx->buf;
919 int rc;
920
Alexander Aring90566362014-10-07 10:38:29 +0200921 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200922
923 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200924 ctx->msg.complete = at86rf230_irq_status;
925 rc = spi_async(lp->spi, &ctx->msg);
926 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200927 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200928 at86rf230_async_error(lp, ctx, rc);
929 return IRQ_NONE;
930 }
931
932 return IRQ_HANDLED;
933}
934
935static void
936at86rf230_write_frame_complete(void *context)
937{
938 struct at86rf230_state_change *ctx = context;
939 struct at86rf230_local *lp = ctx->lp;
940 u8 *buf = ctx->buf;
941 int rc;
942
943 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
944 buf[1] = STATE_BUSY_TX;
945 ctx->trx.len = 2;
946 ctx->msg.complete = NULL;
947 rc = spi_async(lp->spi, &ctx->msg);
948 if (rc)
949 at86rf230_async_error(lp, ctx, rc);
950}
951
952static void
953at86rf230_write_frame(void *context)
954{
955 struct at86rf230_state_change *ctx = context;
956 struct at86rf230_local *lp = ctx->lp;
957 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100958 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200959 int rc;
960
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200961 lp->is_tx = 1;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200962
963 buf[0] = CMD_FB | CMD_WRITE;
964 buf[1] = skb->len + 2;
965 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100966 ctx->trx.len = skb->len + 2;
967 ctx->msg.complete = at86rf230_write_frame_complete;
968 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100969 if (rc) {
970 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200971 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100972 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200973}
974
975static void
976at86rf230_xmit_tx_on(void *context)
977{
978 struct at86rf230_state_change *ctx = context;
979 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200980
Alexander Aring97fed792014-10-07 10:38:32 +0200981 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
982 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200983}
984
Alexander Aringdce481e2015-03-09 13:56:11 +0100985static void
986at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200987{
Alexander Aringdce481e2015-03-09 13:56:11 +0100988 struct at86rf230_state_change *ctx = context;
989 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200990
991 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
992 * are in STATE_TX_ON. The pfad differs here, so we change
993 * the complete handler.
994 */
995 if (lp->tx_aret)
Alexander Aringdce481e2015-03-09 13:56:11 +0100996 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
997 at86rf230_xmit_tx_on, false);
998 else
999 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
1000 at86rf230_write_frame, false);
1001}
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001002
Alexander Aringdce481e2015-03-09 13:56:11 +01001003static int
1004at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
1005{
1006 struct at86rf230_local *lp = hw->priv;
1007 struct at86rf230_state_change *ctx = &lp->tx;
1008
1009 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +01001010 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +01001011
1012 /* After 5 minutes in PLL and the same frequency we run again the
1013 * calibration loops which is recommended by at86rf2xx datasheets.
1014 *
1015 * The calibration is initiate by a state change from TRX_OFF
1016 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
1017 * function then to start in the next 5 minutes.
1018 */
1019 if (time_is_before_jiffies(lp->cal_timeout))
1020 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
1021 at86rf230_xmit_start, false);
1022 else
1023 at86rf230_xmit_start(ctx);
Alexander Aring97fed792014-10-07 10:38:32 +02001024
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001025 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001026}
1027
1028static int
Alexander Aring5a504392014-10-25 17:16:34 +02001029at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001030{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001031 BUG_ON(!level);
1032 *level = 0xbe;
1033 return 0;
1034}
1035
1036static int
Alexander Aring5a504392014-10-25 17:16:34 +02001037at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001038{
Alexander Aringdce481e2015-03-09 13:56:11 +01001039 struct at86rf230_local *lp = hw->priv;
1040
1041 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring5a504392014-10-25 17:16:34 +02001042 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001043}
1044
1045static void
Alexander Aring5a504392014-10-25 17:16:34 +02001046at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001047{
Alexander Aring5a504392014-10-25 17:16:34 +02001048 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001049}
1050
1051static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001052at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001053{
1054 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1055}
1056
1057static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001058at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001059{
1060 int rc;
1061
1062 if (channel == 0)
1063 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1064 else
1065 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1066 if (rc < 0)
1067 return rc;
1068
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001069 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001070 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001071 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001072 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001073 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001074 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001075 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001076 if (rc < 0)
1077 return rc;
1078
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001079 /* This sets the symbol_duration according frequency on the 212.
1080 * TODO move this handling while set channel and page in cfg802154.
1081 * We can do that, this timings are according 802.15.4 standard.
1082 * If we do that in cfg802154, this is a more generic calculation.
1083 *
1084 * This should also protected from ifs_timer. Means cancel timer and
1085 * init with a new value. For now, this is okay.
1086 */
1087 if (channel == 0) {
1088 if (page == 0) {
1089 /* SUB:0 and BPSK:0 -> BPSK-20 */
1090 lp->hw->phy->symbol_duration = 50;
1091 } else {
1092 /* SUB:1 and BPSK:0 -> BPSK-40 */
1093 lp->hw->phy->symbol_duration = 25;
1094 }
1095 } else {
1096 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001097 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001098 lp->hw->phy->symbol_duration = 40;
1099 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001100 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001101 lp->hw->phy->symbol_duration = 16;
1102 }
1103
1104 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1105 lp->hw->phy->symbol_duration;
1106 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1107 lp->hw->phy->symbol_duration;
1108
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001109 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1110}
1111
1112static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001113at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001114{
Alexander Aring5a504392014-10-25 17:16:34 +02001115 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001116 int rc;
1117
Alexander Aringa53d1f72014-07-03 00:20:46 +02001118 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001119 /* Wait for PLL */
1120 usleep_range(lp->data->t_channel_switch,
1121 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001122
1123 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001124 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001125}
1126
1127static int
Alexander Aring5a504392014-10-25 17:16:34 +02001128at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001129 struct ieee802154_hw_addr_filt *filt,
1130 unsigned long changed)
1131{
Alexander Aring5a504392014-10-25 17:16:34 +02001132 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001133
Alexander Aring57205c12014-10-25 05:25:09 +02001134 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001135 u16 addr = le16_to_cpu(filt->short_addr);
1136
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001137 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001138 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001139 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1140 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001141 }
1142
Alexander Aring57205c12014-10-25 05:25:09 +02001143 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001144 u16 pan = le16_to_cpu(filt->pan_id);
1145
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001146 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001147 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001148 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1149 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001150 }
1151
Alexander Aring57205c12014-10-25 05:25:09 +02001152 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001153 u8 i, addr[8];
1154
1155 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001156 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001157 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001158 for (i = 0; i < 8; i++)
1159 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001160 }
1161
Alexander Aring57205c12014-10-25 05:25:09 +02001162 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001163 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001164 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001165 if (filt->pan_coord)
1166 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1167 else
1168 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1169 }
1170
1171 return 0;
1172}
1173
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001174static int
Alexander Aring5a504392014-10-25 17:16:34 +02001175at86rf230_set_txpower(struct ieee802154_hw *hw, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001176{
Alexander Aring5a504392014-10-25 17:16:34 +02001177 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001178
1179 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1180 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1181 * 0dB.
1182 * thus, supported values for db range from -26 to 5, for 31dB of
1183 * reduction to 0dB of reduction.
1184 */
1185 if (db > 5 || db < -26)
1186 return -EINVAL;
1187
1188 db = -(db - 5);
1189
Jean Sacren677676c2014-03-01 15:54:36 -07001190 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001191}
1192
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001193static int
Alexander Aring5a504392014-10-25 17:16:34 +02001194at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001195{
Alexander Aring5a504392014-10-25 17:16:34 +02001196 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001197
1198 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1199}
1200
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001201static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001202at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1203 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001204{
Alexander Aring5a504392014-10-25 17:16:34 +02001205 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001206 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001207
Alexander Aring7fe9a382014-12-10 15:33:12 +01001208 /* mapping 802.15.4 to driver spec */
1209 switch (cca->mode) {
1210 case NL802154_CCA_ENERGY:
1211 val = 1;
1212 break;
1213 case NL802154_CCA_CARRIER:
1214 val = 2;
1215 break;
1216 case NL802154_CCA_ENERGY_CARRIER:
1217 switch (cca->opt) {
1218 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1219 val = 3;
1220 break;
1221 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1222 val = 0;
1223 break;
1224 default:
1225 return -EINVAL;
1226 }
1227 break;
1228 default:
1229 return -EINVAL;
1230 }
1231
1232 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001233}
1234
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001235static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001236at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1237{
1238 return (level - lp->data->rssi_base_val) * 100 / 207;
1239}
1240
1241static int
1242at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1243{
1244 return (level - lp->data->rssi_base_val) / 2;
1245}
1246
1247static int
Alexander Aring5a504392014-10-25 17:16:34 +02001248at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001249{
Alexander Aring5a504392014-10-25 17:16:34 +02001250 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001251
Alexander Aringa53d1f72014-07-03 00:20:46 +02001252 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001253 return -EINVAL;
1254
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001255 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1256 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001257}
1258
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001259static int
Alexander Aring5a504392014-10-25 17:16:34 +02001260at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001261 u8 retries)
1262{
Alexander Aring5a504392014-10-25 17:16:34 +02001263 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001264 int rc;
1265
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001266 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1267 if (rc)
1268 return rc;
1269
1270 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1271 if (rc)
1272 return rc;
1273
Alexander Aring39d7f322014-04-05 13:49:26 +02001274 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001275}
1276
1277static int
Alexander Aring5a504392014-10-25 17:16:34 +02001278at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001279{
Alexander Aring5a504392014-10-25 17:16:34 +02001280 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001281 int rc = 0;
1282
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001283 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001284 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001285
1286 if (retries >= 0)
1287 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1288
1289 return rc;
1290}
1291
Alexander Aring92f45f52014-10-29 21:34:33 +01001292static int
1293at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1294{
1295 struct at86rf230_local *lp = hw->priv;
1296 int rc;
1297
1298 if (on) {
1299 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1300 if (rc < 0)
1301 return rc;
1302
1303 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1304 if (rc < 0)
1305 return rc;
1306 } else {
1307 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1308 if (rc < 0)
1309 return rc;
1310
1311 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1312 if (rc < 0)
1313 return rc;
1314 }
1315
1316 return 0;
1317}
1318
Alexander Aring16301862014-10-28 18:21:18 +01001319static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001320 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001321 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001322 .ed = at86rf230_ed,
1323 .set_channel = at86rf230_channel,
1324 .start = at86rf230_start,
1325 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001326 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001327 .set_txpower = at86rf230_set_txpower,
1328 .set_lbt = at86rf230_set_lbt,
1329 .set_cca_mode = at86rf230_set_cca_mode,
1330 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1331 .set_csma_params = at86rf230_set_csma_params,
1332 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f52014-10-29 21:34:33 +01001333 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001334};
1335
Alexander Aringa53d1f72014-07-03 00:20:46 +02001336static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001337 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001338 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001339 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001340 .t_off_to_aack = 80,
1341 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001342 .t_frame = 4096,
1343 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001344 .rssi_base_val = -91,
1345 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001346 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001347};
1348
1349static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001350 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001351 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001352 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001353 .t_off_to_aack = 110,
1354 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001355 .t_frame = 4096,
1356 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001357 .rssi_base_val = -91,
1358 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001359 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001360};
1361
1362static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001363 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001364 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001365 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001366 .t_off_to_aack = 200,
1367 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001368 .t_frame = 4096,
1369 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001370 .rssi_base_val = -100,
1371 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001372 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001373};
1374
Alexander Aringccdaeb22015-02-27 09:58:26 +01001375static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001376{
Alexander Aring1db05582014-07-03 00:20:50 +02001377 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001378 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001379 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001380
Alexander Aring09e536c2014-07-03 00:20:52 +02001381 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001382 if (rc)
1383 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001384
Alexander Aring4af619a2014-04-24 19:09:05 +02001385 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001386 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1387 irq_type == IRQ_TYPE_EDGE_FALLING)
1388 dev_warn(&lp->spi->dev,
1389 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001390 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1391 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001392 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001393
Alexander Aring18c65042014-04-24 19:09:18 +02001394 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001395 if (rc)
1396 return rc;
1397
Alexander Aring6bd2b132014-07-03 00:20:49 +02001398 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1399 if (rc)
1400 return rc;
1401
Sascha Herrmann057dad62013-04-14 22:33:29 +00001402 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001403 if (rc)
1404 return rc;
1405
Alexander Aringbe64f072015-02-27 09:58:28 +01001406 /* reset values differs in at86rf231 and at86rf233 */
1407 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1408 if (rc)
1409 return rc;
1410
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001411 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1412 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1413 if (rc)
1414 return rc;
1415 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1416 if (rc)
1417 return rc;
1418
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001419 /* CLKM changes are applied immediately */
1420 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1421 if (rc)
1422 return rc;
1423
1424 /* Turn CLKM Off */
1425 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1426 if (rc)
1427 return rc;
1428 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001429 usleep_range(lp->data->t_sleep_cycle,
1430 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001431
Alexander Aringccdaeb22015-02-27 09:58:26 +01001432 /* xtal_trim value is calculated by:
1433 * CL = 0.5 * (CX + CTRIM + CPAR)
1434 *
1435 * whereas:
1436 * CL = capacitor of used crystal
1437 * CX = connected capacitors at xtal pins
1438 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1439 * but this is different on each board setup. You need to fine
1440 * tuning this value via CTRIM.
1441 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1442 * 0 pF upto 4.5 pF.
1443 *
1444 * Examples:
1445 * atben transceiver:
1446 *
1447 * CL = 8 pF
1448 * CX = 12 pF
1449 * CPAR = 3 pF (We assume the magic constant from datasheet)
1450 * CTRIM = 0.9 pF
1451 *
1452 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1453 *
1454 * xtal_trim = 0x3
1455 *
1456 * openlabs transceiver:
1457 *
1458 * CL = 16 pF
1459 * CX = 22 pF
1460 * CPAR = 3 pF (We assume the magic constant from datasheet)
1461 * CTRIM = 4.5 pF
1462 *
1463 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1464 *
1465 * xtal_trim = 0xf
1466 */
1467 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1468 if (rc)
1469 return rc;
1470
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001471 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001472 if (rc)
1473 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001474 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001475 dev_err(&lp->spi->dev, "DVDD error\n");
1476 return -EINVAL;
1477 }
1478
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001479 /* Force setting slotted operation bit to 0. Sometimes the atben
1480 * sets this bit and I don't know why. We set this always force
1481 * to zero while probing.
1482 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001483 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001484}
1485
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001486static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001487at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1488 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001489{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001490 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001491 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001492
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001493 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1494 if (!pdata)
1495 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001496
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001497 *rstn = pdata->rstn;
1498 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001499 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001500 return 0;
1501 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001502
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001503 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1504 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001505 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1506 if (ret < 0 && ret != -EINVAL)
1507 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001508
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001509 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001510}
1511
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001512static int
1513at86rf230_detect_device(struct at86rf230_local *lp)
1514{
1515 unsigned int part, version, val;
1516 u16 man_id = 0;
1517 const char *chip;
1518 int rc;
1519
1520 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1521 if (rc)
1522 return rc;
1523 man_id |= val;
1524
1525 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1526 if (rc)
1527 return rc;
1528 man_id |= (val << 8);
1529
1530 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1531 if (rc)
1532 return rc;
1533
Andrey Yurovsky75989682014-12-17 13:14:42 -08001534 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001535 if (rc)
1536 return rc;
1537
1538 if (man_id != 0x001f) {
1539 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1540 man_id >> 8, man_id & 0xFF);
1541 return -EINVAL;
1542 }
1543
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001544 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringc8fc84e2014-10-29 21:34:31 +01001545 IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
Alexander Aring92f45f52014-10-29 21:34:33 +01001546 IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001547
Alexander Aringb48a7c12014-12-10 15:33:14 +01001548 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1549
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001550 switch (part) {
1551 case 2:
1552 chip = "at86rf230";
1553 rc = -ENOTSUPP;
1554 break;
1555 case 3:
1556 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001557 lp->data = &at86rf231_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001558 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001559 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001560 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001561 break;
1562 case 7:
1563 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001564 lp->data = &at86rf212_data;
1565 lp->hw->flags |= IEEE802154_HW_LBT;
1566 lp->hw->phy->channels_supported[0] = 0x00007FF;
1567 lp->hw->phy->channels_supported[2] = 0x00007FF;
1568 lp->hw->phy->current_channel = 5;
1569 lp->hw->phy->symbol_duration = 25;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001570 break;
1571 case 11:
1572 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001573 lp->data = &at86rf233_data;
Alexander Aring5a504392014-10-25 17:16:34 +02001574 lp->hw->phy->channels_supported[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001575 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001576 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001577 break;
1578 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001579 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001580 rc = -ENOTSUPP;
1581 break;
1582 }
1583
1584 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1585
1586 return rc;
1587}
1588
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001589static void
1590at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1591{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001592 lp->state.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001593 lp->state.irq = lp->spi->irq;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001594 spi_message_init(&lp->state.msg);
1595 lp->state.msg.context = &lp->state;
Alexander Aring263be332015-03-01 21:55:33 +01001596 lp->state.trx.len = 2;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001597 lp->state.trx.tx_buf = lp->state.buf;
1598 lp->state.trx.rx_buf = lp->state.buf;
1599 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001600 hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1601 lp->state.timer.function = at86rf230_async_state_timer;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001602
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001603 lp->irq.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001604 lp->irq.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001605 spi_message_init(&lp->irq.msg);
1606 lp->irq.msg.context = &lp->irq;
Alexander Aring263be332015-03-01 21:55:33 +01001607 lp->irq.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001608 lp->irq.trx.tx_buf = lp->irq.buf;
1609 lp->irq.trx.rx_buf = lp->irq.buf;
1610 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001611 hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1612 lp->irq.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001613
1614 lp->tx.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001615 lp->tx.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001616 spi_message_init(&lp->tx.msg);
1617 lp->tx.msg.context = &lp->tx;
Alexander Aring263be332015-03-01 21:55:33 +01001618 lp->tx.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001619 lp->tx.trx.tx_buf = lp->tx.buf;
1620 lp->tx.trx.rx_buf = lp->tx.buf;
1621 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
Alexander Aringeb3b4352015-03-09 13:56:10 +01001622 hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1623 lp->tx.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001624}
1625
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001626static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001627{
Alexander Aring5a504392014-10-25 17:16:34 +02001628 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001629 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001630 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001631 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001632 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001633
1634 if (!spi->irq) {
1635 dev_err(&spi->dev, "no IRQ specified\n");
1636 return -EINVAL;
1637 }
1638
Alexander Aringccdaeb22015-02-27 09:58:26 +01001639 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001640 if (rc < 0) {
1641 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1642 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001643 }
1644
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001645 if (gpio_is_valid(rstn)) {
1646 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001647 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001648 if (rc)
1649 return rc;
1650 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001651
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001652 if (gpio_is_valid(slp_tr)) {
1653 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001654 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001655 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001656 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001657 }
1658
1659 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001660 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001661 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001662 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001663 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001664 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001665 usleep_range(120, 240);
1666 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001667
Alexander Aring5a504392014-10-25 17:16:34 +02001668 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1669 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001670 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001671
Alexander Aring5a504392014-10-25 17:16:34 +02001672 lp = hw->priv;
1673 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001674 lp->spi = spi;
Alexander Aring5a504392014-10-25 17:16:34 +02001675 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001676 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001677 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001678
Alexander Aringf76014f772014-07-03 00:20:44 +02001679 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1680 if (IS_ERR(lp->regmap)) {
1681 rc = PTR_ERR(lp->regmap);
1682 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1683 rc);
1684 goto free_dev;
1685 }
1686
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001687 at86rf230_setup_spi_messages(lp);
1688
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001689 rc = at86rf230_detect_device(lp);
1690 if (rc < 0)
1691 goto free_dev;
1692
Alexander Aring2e0571c2014-07-03 00:20:51 +02001693 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001694
1695 spi_set_drvdata(spi, lp);
1696
Alexander Aringccdaeb22015-02-27 09:58:26 +01001697 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001698 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001699 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001700
Alexander Aring19626942014-04-24 19:09:15 +02001701 /* Read irq status register to reset irq line */
1702 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001703 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001704 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001705
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001706 irq_type = irq_get_trigger_type(spi->irq);
1707 if (!irq_type)
1708 irq_type = IRQF_TRIGGER_RISING;
1709
1710 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1711 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001712 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001713 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001714
Alexander Aring5a504392014-10-25 17:16:34 +02001715 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001716 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001717 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001718
1719 return rc;
1720
Alexander Aring640985e2014-07-03 00:20:43 +02001721free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001722 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001723
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001724 return rc;
1725}
1726
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001727static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001728{
1729 struct at86rf230_local *lp = spi_get_drvdata(spi);
1730
Alexander Aring17e84a92014-03-31 03:26:51 +02001731 /* mask all at86rf230 irq's */
1732 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001733 ieee802154_unregister_hw(lp->hw);
1734 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001735 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001736
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001737 return 0;
1738}
1739
Alexander Aring1086b4f2014-04-24 19:09:11 +02001740static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001741 { .compatible = "atmel,at86rf230", },
1742 { .compatible = "atmel,at86rf231", },
1743 { .compatible = "atmel,at86rf233", },
1744 { .compatible = "atmel,at86rf212", },
1745 { },
1746};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001747MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001748
Alexander Aring90b15522014-04-24 19:09:12 +02001749static const struct spi_device_id at86rf230_device_id[] = {
1750 { .name = "at86rf230", },
1751 { .name = "at86rf231", },
1752 { .name = "at86rf233", },
1753 { .name = "at86rf212", },
1754 { },
1755};
1756MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1757
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001758static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001759 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001760 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001761 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001762 .name = "at86rf230",
1763 .owner = THIS_MODULE,
1764 },
1765 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001766 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001767};
1768
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001769module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001770
1771MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1772MODULE_LICENSE("GPL v2");