blob: fabde3ac49b3dfad5e37d63841915fa1675a6f79 [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 Aringeb3b435e2015-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 Aringeb3b435e2015-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;
Alexander Aringd2c8bf52015-04-30 17:45:03 +020088 int slp_tr;
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;
Alexander Aring85009202015-04-30 17:45:02 +020099 bool is_tx_from_off;
Alexander Aringba6d2232015-03-01 21:55:28 +0100100 u8 tx_retry;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200101 struct sk_buff *tx_skb;
102 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000103};
104
Alexander Aring5e8e01e2015-04-30 17:44:58 +0200105#define RG_TRX_STATUS (0x01)
106#define SR_TRX_STATUS 0x01, 0x1f, 0
107#define SR_RESERVED_01_3 0x01, 0x20, 5
108#define SR_CCA_STATUS 0x01, 0x40, 6
109#define SR_CCA_DONE 0x01, 0x80, 7
110#define RG_TRX_STATE (0x02)
111#define SR_TRX_CMD 0x02, 0x1f, 0
112#define SR_TRAC_STATUS 0x02, 0xe0, 5
113#define RG_TRX_CTRL_0 (0x03)
114#define SR_CLKM_CTRL 0x03, 0x07, 0
115#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
116#define SR_PAD_IO_CLKM 0x03, 0x30, 4
117#define SR_PAD_IO 0x03, 0xc0, 6
118#define RG_TRX_CTRL_1 (0x04)
119#define SR_IRQ_POLARITY 0x04, 0x01, 0
120#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
121#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
122#define SR_RX_BL_CTRL 0x04, 0x10, 4
123#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
124#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
125#define SR_PA_EXT_EN 0x04, 0x80, 7
126#define RG_PHY_TX_PWR (0x05)
127#define SR_TX_PWR 0x05, 0x0f, 0
128#define SR_PA_LT 0x05, 0x30, 4
129#define SR_PA_BUF_LT 0x05, 0xc0, 6
130#define RG_PHY_RSSI (0x06)
131#define SR_RSSI 0x06, 0x1f, 0
132#define SR_RND_VALUE 0x06, 0x60, 5
133#define SR_RX_CRC_VALID 0x06, 0x80, 7
134#define RG_PHY_ED_LEVEL (0x07)
135#define SR_ED_LEVEL 0x07, 0xff, 0
136#define RG_PHY_CC_CCA (0x08)
137#define SR_CHANNEL 0x08, 0x1f, 0
138#define SR_CCA_MODE 0x08, 0x60, 5
139#define SR_CCA_REQUEST 0x08, 0x80, 7
140#define RG_CCA_THRES (0x09)
141#define SR_CCA_ED_THRES 0x09, 0x0f, 0
142#define SR_RESERVED_09_1 0x09, 0xf0, 4
143#define RG_RX_CTRL (0x0a)
144#define SR_PDT_THRES 0x0a, 0x0f, 0
145#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
146#define RG_SFD_VALUE (0x0b)
147#define SR_SFD_VALUE 0x0b, 0xff, 0
148#define RG_TRX_CTRL_2 (0x0c)
149#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
150#define SR_SUB_MODE 0x0c, 0x04, 2
151#define SR_BPSK_QPSK 0x0c, 0x08, 3
152#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
153#define SR_RESERVED_0c_5 0x0c, 0x60, 5
154#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
155#define RG_ANT_DIV (0x0d)
156#define SR_ANT_CTRL 0x0d, 0x03, 0
157#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
158#define SR_ANT_DIV_EN 0x0d, 0x08, 3
159#define SR_RESERVED_0d_2 0x0d, 0x70, 4
160#define SR_ANT_SEL 0x0d, 0x80, 7
161#define RG_IRQ_MASK (0x0e)
162#define SR_IRQ_MASK 0x0e, 0xff, 0
163#define RG_IRQ_STATUS (0x0f)
164#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
165#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
166#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
167#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
168#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
169#define SR_IRQ_5_AMI 0x0f, 0x20, 5
170#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
171#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
172#define RG_VREG_CTRL (0x10)
173#define SR_RESERVED_10_6 0x10, 0x03, 0
174#define SR_DVDD_OK 0x10, 0x04, 2
175#define SR_DVREG_EXT 0x10, 0x08, 3
176#define SR_RESERVED_10_3 0x10, 0x30, 4
177#define SR_AVDD_OK 0x10, 0x40, 6
178#define SR_AVREG_EXT 0x10, 0x80, 7
179#define RG_BATMON (0x11)
180#define SR_BATMON_VTH 0x11, 0x0f, 0
181#define SR_BATMON_HR 0x11, 0x10, 4
182#define SR_BATMON_OK 0x11, 0x20, 5
183#define SR_RESERVED_11_1 0x11, 0xc0, 6
184#define RG_XOSC_CTRL (0x12)
185#define SR_XTAL_TRIM 0x12, 0x0f, 0
186#define SR_XTAL_MODE 0x12, 0xf0, 4
187#define RG_RX_SYN (0x15)
188#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
189#define SR_RESERVED_15_2 0x15, 0x70, 4
190#define SR_RX_PDT_DIS 0x15, 0x80, 7
191#define RG_XAH_CTRL_1 (0x17)
192#define SR_RESERVED_17_8 0x17, 0x01, 0
193#define SR_AACK_PROM_MODE 0x17, 0x02, 1
194#define SR_AACK_ACK_TIME 0x17, 0x04, 2
195#define SR_RESERVED_17_5 0x17, 0x08, 3
196#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
197#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
198#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
199#define SR_RESERVED_17_1 0x17, 0x80, 7
200#define RG_FTN_CTRL (0x18)
201#define SR_RESERVED_18_2 0x18, 0x7f, 0
202#define SR_FTN_START 0x18, 0x80, 7
203#define RG_PLL_CF (0x1a)
204#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
205#define SR_PLL_CF_START 0x1a, 0x80, 7
206#define RG_PLL_DCU (0x1b)
207#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
208#define SR_RESERVED_1b_2 0x1b, 0x40, 6
209#define SR_PLL_DCU_START 0x1b, 0x80, 7
210#define RG_PART_NUM (0x1c)
211#define SR_PART_NUM 0x1c, 0xff, 0
212#define RG_VERSION_NUM (0x1d)
213#define SR_VERSION_NUM 0x1d, 0xff, 0
214#define RG_MAN_ID_0 (0x1e)
215#define SR_MAN_ID_0 0x1e, 0xff, 0
216#define RG_MAN_ID_1 (0x1f)
217#define SR_MAN_ID_1 0x1f, 0xff, 0
218#define RG_SHORT_ADDR_0 (0x20)
219#define SR_SHORT_ADDR_0 0x20, 0xff, 0
220#define RG_SHORT_ADDR_1 (0x21)
221#define SR_SHORT_ADDR_1 0x21, 0xff, 0
222#define RG_PAN_ID_0 (0x22)
223#define SR_PAN_ID_0 0x22, 0xff, 0
224#define RG_PAN_ID_1 (0x23)
225#define SR_PAN_ID_1 0x23, 0xff, 0
226#define RG_IEEE_ADDR_0 (0x24)
227#define SR_IEEE_ADDR_0 0x24, 0xff, 0
228#define RG_IEEE_ADDR_1 (0x25)
229#define SR_IEEE_ADDR_1 0x25, 0xff, 0
230#define RG_IEEE_ADDR_2 (0x26)
231#define SR_IEEE_ADDR_2 0x26, 0xff, 0
232#define RG_IEEE_ADDR_3 (0x27)
233#define SR_IEEE_ADDR_3 0x27, 0xff, 0
234#define RG_IEEE_ADDR_4 (0x28)
235#define SR_IEEE_ADDR_4 0x28, 0xff, 0
236#define RG_IEEE_ADDR_5 (0x29)
237#define SR_IEEE_ADDR_5 0x29, 0xff, 0
238#define RG_IEEE_ADDR_6 (0x2a)
239#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
240#define RG_IEEE_ADDR_7 (0x2b)
241#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
242#define RG_XAH_CTRL_0 (0x2c)
243#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
244#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
245#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
246#define RG_CSMA_SEED_0 (0x2d)
247#define SR_CSMA_SEED_0 0x2d, 0xff, 0
248#define RG_CSMA_SEED_1 (0x2e)
249#define SR_CSMA_SEED_1 0x2e, 0x07, 0
250#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
251#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
252#define SR_AACK_SET_PD 0x2e, 0x20, 5
253#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
254#define RG_CSMA_BE (0x2f)
255#define SR_MIN_BE 0x2f, 0x0f, 0
256#define SR_MAX_BE 0x2f, 0xf0, 4
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000257
258#define CMD_REG 0x80
259#define CMD_REG_MASK 0x3f
260#define CMD_WRITE 0x40
261#define CMD_FB 0x20
262
263#define IRQ_BAT_LOW (1 << 7)
264#define IRQ_TRX_UR (1 << 6)
265#define IRQ_AMI (1 << 5)
266#define IRQ_CCA_ED (1 << 4)
267#define IRQ_TRX_END (1 << 3)
268#define IRQ_RX_START (1 << 2)
269#define IRQ_PLL_UNL (1 << 1)
270#define IRQ_PLL_LOCK (1 << 0)
271
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000272#define IRQ_ACTIVE_HIGH 0
273#define IRQ_ACTIVE_LOW 1
274
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000275#define STATE_P_ON 0x00 /* BUSY */
276#define STATE_BUSY_RX 0x01
277#define STATE_BUSY_TX 0x02
278#define STATE_FORCE_TRX_OFF 0x03
279#define STATE_FORCE_TX_ON 0x04 /* IDLE */
280/* 0x05 */ /* INVALID_PARAMETER */
281#define STATE_RX_ON 0x06
282/* 0x07 */ /* SUCCESS */
283#define STATE_TRX_OFF 0x08
284#define STATE_TX_ON 0x09
285/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
286#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500287#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000288#define STATE_BUSY_RX_AACK 0x11
289#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000290#define STATE_RX_AACK_ON 0x16
291#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000292#define STATE_RX_ON_NOCLK 0x1C
293#define STATE_RX_AACK_ON_NOCLK 0x1D
294#define STATE_BUSY_RX_AACK_NOCLK 0x1E
295#define STATE_TRANSITION_IN_PROGRESS 0x1F
296
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200297#define TRX_STATE_MASK (0x1F)
298
Alexander Aringf76014f772014-07-03 00:20:44 +0200299#define AT86RF2XX_NUMREGS 0x3F
300
Alexander Aring97fed792014-10-07 10:38:32 +0200301static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200302at86rf230_async_state_change(struct at86rf230_local *lp,
303 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200304 const u8 state, void (*complete)(void *context),
305 const bool irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200306
Alexander Aringf76014f772014-07-03 00:20:44 +0200307static inline int
308__at86rf230_write(struct at86rf230_local *lp,
309 unsigned int addr, unsigned int data)
310{
311 return regmap_write(lp->regmap, addr, data);
312}
313
314static inline int
315__at86rf230_read(struct at86rf230_local *lp,
316 unsigned int addr, unsigned int *data)
317{
318 return regmap_read(lp->regmap, addr, data);
319}
320
321static inline int
322at86rf230_read_subreg(struct at86rf230_local *lp,
323 unsigned int addr, unsigned int mask,
324 unsigned int shift, unsigned int *data)
325{
326 int rc;
327
328 rc = __at86rf230_read(lp, addr, data);
Alexander Aringd907c4f2015-03-17 10:32:39 +0100329 if (!rc)
Alexander Aringf76014f772014-07-03 00:20:44 +0200330 *data = (*data & mask) >> shift;
331
332 return rc;
333}
334
335static inline int
336at86rf230_write_subreg(struct at86rf230_local *lp,
337 unsigned int addr, unsigned int mask,
338 unsigned int shift, unsigned int data)
339{
340 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
341}
342
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200343static inline void
344at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
345{
346 gpio_set_value(lp->slp_tr, 1);
347 udelay(1);
348 gpio_set_value(lp->slp_tr, 0);
349}
350
Alexander Aringf76014f772014-07-03 00:20:44 +0200351static bool
352at86rf230_reg_writeable(struct device *dev, unsigned int reg)
353{
354 switch (reg) {
355 case RG_TRX_STATE:
356 case RG_TRX_CTRL_0:
357 case RG_TRX_CTRL_1:
358 case RG_PHY_TX_PWR:
359 case RG_PHY_ED_LEVEL:
360 case RG_PHY_CC_CCA:
361 case RG_CCA_THRES:
362 case RG_RX_CTRL:
363 case RG_SFD_VALUE:
364 case RG_TRX_CTRL_2:
365 case RG_ANT_DIV:
366 case RG_IRQ_MASK:
367 case RG_VREG_CTRL:
368 case RG_BATMON:
369 case RG_XOSC_CTRL:
370 case RG_RX_SYN:
371 case RG_XAH_CTRL_1:
372 case RG_FTN_CTRL:
373 case RG_PLL_CF:
374 case RG_PLL_DCU:
375 case RG_SHORT_ADDR_0:
376 case RG_SHORT_ADDR_1:
377 case RG_PAN_ID_0:
378 case RG_PAN_ID_1:
379 case RG_IEEE_ADDR_0:
380 case RG_IEEE_ADDR_1:
381 case RG_IEEE_ADDR_2:
382 case RG_IEEE_ADDR_3:
383 case RG_IEEE_ADDR_4:
384 case RG_IEEE_ADDR_5:
385 case RG_IEEE_ADDR_6:
386 case RG_IEEE_ADDR_7:
387 case RG_XAH_CTRL_0:
388 case RG_CSMA_SEED_0:
389 case RG_CSMA_SEED_1:
390 case RG_CSMA_BE:
391 return true;
392 default:
393 return false;
394 }
395}
396
397static bool
398at86rf230_reg_readable(struct device *dev, unsigned int reg)
399{
400 bool rc;
401
402 /* all writeable are also readable */
403 rc = at86rf230_reg_writeable(dev, reg);
404 if (rc)
405 return rc;
406
407 /* readonly regs */
408 switch (reg) {
409 case RG_TRX_STATUS:
410 case RG_PHY_RSSI:
411 case RG_IRQ_STATUS:
412 case RG_PART_NUM:
413 case RG_VERSION_NUM:
414 case RG_MAN_ID_1:
415 case RG_MAN_ID_0:
416 return true;
417 default:
418 return false;
419 }
420}
421
422static bool
423at86rf230_reg_volatile(struct device *dev, unsigned int reg)
424{
425 /* can be changed during runtime */
426 switch (reg) {
427 case RG_TRX_STATUS:
428 case RG_TRX_STATE:
429 case RG_PHY_RSSI:
430 case RG_PHY_ED_LEVEL:
431 case RG_IRQ_STATUS:
432 case RG_VREG_CTRL:
Alexander Aring51b3b2c2015-03-09 13:56:12 +0100433 case RG_PLL_CF:
434 case RG_PLL_DCU:
Alexander Aringf76014f772014-07-03 00:20:44 +0200435 return true;
436 default:
437 return false;
438 }
439}
440
441static bool
442at86rf230_reg_precious(struct device *dev, unsigned int reg)
443{
444 /* don't clear irq line on read */
445 switch (reg) {
446 case RG_IRQ_STATUS:
447 return true;
448 default:
449 return false;
450 }
451}
452
Krzysztof Kozlowski889ee2c2015-01-05 10:02:31 +0100453static const struct regmap_config at86rf230_regmap_spi_config = {
Alexander Aringf76014f772014-07-03 00:20:44 +0200454 .reg_bits = 8,
455 .val_bits = 8,
456 .write_flag_mask = CMD_REG | CMD_WRITE,
457 .read_flag_mask = CMD_REG,
458 .cache_type = REGCACHE_RBTREE,
459 .max_register = AT86RF2XX_NUMREGS,
460 .writeable_reg = at86rf230_reg_writeable,
461 .readable_reg = at86rf230_reg_readable,
462 .volatile_reg = at86rf230_reg_volatile,
463 .precious_reg = at86rf230_reg_precious,
464};
465
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200466static void
467at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000468{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200469 struct at86rf230_state_change *ctx = context;
470 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000471
Alexander Aringa7a484b2015-03-26 12:46:30 +0100472 lp->is_tx = 0;
Alexander Aring97fed792014-10-07 10:38:32 +0200473 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
Alexander Aring955aee82014-10-26 09:37:15 +0100474 ieee802154_wake_queue(lp->hw);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200475}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000476
Alexander Aringfc50c6e2014-12-15 10:25:54 +0100477static inline void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200478at86rf230_async_error(struct at86rf230_local *lp,
479 struct at86rf230_state_change *ctx, int rc)
480{
481 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000482
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200483 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring97fed792014-10-07 10:38:32 +0200484 at86rf230_async_error_recover, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200485}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000486
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200487/* Generic function to get some register value in async mode */
Alexander Aring97fed792014-10-07 10:38:32 +0200488static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200489at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
490 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200491 void (*complete)(void *context),
492 const bool irq_enable)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200493{
Alexander Aring97fed792014-10-07 10:38:32 +0200494 int rc;
495
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200496 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000497
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200498 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200499 ctx->msg.complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200500 ctx->irq_enable = irq_enable;
501 rc = spi_async(lp->spi, &ctx->msg);
502 if (rc) {
503 if (irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100504 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200505
506 at86rf230_async_error(lp, ctx, rc);
507 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200508}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000509
Alexander Aringdce481e2015-03-09 13:56:11 +0100510static inline u8 at86rf230_state_to_force(u8 state)
511{
512 if (state == STATE_TX_ON)
513 return STATE_FORCE_TX_ON;
514 else
515 return STATE_FORCE_TRX_OFF;
516}
517
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200518static void
519at86rf230_async_state_assert(void *context)
520{
521 struct at86rf230_state_change *ctx = context;
522 struct at86rf230_local *lp = ctx->lp;
523 const u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200524 const u8 trx_state = buf[1] & TRX_STATE_MASK;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000525
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200526 /* Assert state change */
527 if (trx_state != ctx->to_state) {
528 /* Special handling if transceiver state is in
529 * STATE_BUSY_RX_AACK and a SHR was detected.
530 */
531 if (trx_state == STATE_BUSY_RX_AACK) {
532 /* Undocumented race condition. If we send a state
533 * change to STATE_RX_AACK_ON the transceiver could
534 * change his state automatically to STATE_BUSY_RX_AACK
535 * if a SHR was detected. This is not an error, but we
536 * can't assert this.
537 */
538 if (ctx->to_state == STATE_RX_AACK_ON)
539 goto done;
540
541 /* If we change to STATE_TX_ON without forcing and
542 * transceiver state is STATE_BUSY_RX_AACK, we wait
543 * 'tFrame + tPAck' receiving time. In this time the
544 * PDU should be received. If the transceiver is still
545 * in STATE_BUSY_RX_AACK, we run a force state change
546 * to STATE_TX_ON. This is a timeout handling, if the
547 * transceiver stucks in STATE_BUSY_RX_AACK.
Alexander Aringba6d2232015-03-01 21:55:28 +0100548 *
549 * Additional we do several retries to try to get into
550 * TX_ON state without forcing. If the retries are
551 * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
552 * will do a force change.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200553 */
Alexander Aringdce481e2015-03-09 13:56:11 +0100554 if (ctx->to_state == STATE_TX_ON ||
555 ctx->to_state == STATE_TRX_OFF) {
556 u8 state = ctx->to_state;
Alexander Aringba6d2232015-03-01 21:55:28 +0100557
558 if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
Alexander Aringdce481e2015-03-09 13:56:11 +0100559 state = at86rf230_state_to_force(state);
Alexander Aringba6d2232015-03-01 21:55:28 +0100560 lp->tx_retry++;
561
562 at86rf230_async_state_change(lp, ctx, state,
Alexander Aring97fed792014-10-07 10:38:32 +0200563 ctx->complete,
564 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200565 return;
566 }
567 }
568
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200569 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
570 ctx->from_state, ctx->to_state, trx_state);
571 }
572
573done:
574 if (ctx->complete)
575 ctx->complete(context);
576}
577
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100578static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
579{
580 struct at86rf230_state_change *ctx =
581 container_of(timer, struct at86rf230_state_change, timer);
582 struct at86rf230_local *lp = ctx->lp;
583
584 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
585 at86rf230_async_state_assert,
586 ctx->irq_enable);
587
588 return HRTIMER_NORESTART;
589}
590
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200591/* Do state change timing delay. */
592static void
593at86rf230_async_state_delay(void *context)
594{
595 struct at86rf230_state_change *ctx = context;
596 struct at86rf230_local *lp = ctx->lp;
597 struct at86rf2xx_chip_data *c = lp->data;
598 bool force = false;
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100599 ktime_t tim;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200600
601 /* The force state changes are will show as normal states in the
602 * state status subregister. We change the to_state to the
603 * corresponding one and remember if it was a force change, this
604 * differs if we do a state change from STATE_BUSY_RX_AACK.
605 */
606 switch (ctx->to_state) {
607 case STATE_FORCE_TX_ON:
608 ctx->to_state = STATE_TX_ON;
609 force = true;
610 break;
611 case STATE_FORCE_TRX_OFF:
612 ctx->to_state = STATE_TRX_OFF;
613 force = true;
614 break;
615 default:
616 break;
617 }
618
619 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200620 case STATE_TRX_OFF:
621 switch (ctx->to_state) {
622 case STATE_RX_AACK_ON:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100623 tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
Alexander Aring2ad33242015-04-30 17:44:59 +0200624 /* state change from TRX_OFF to RX_AACK_ON to do a
625 * calibration, we need to reset the timeout for the
626 * next one.
627 */
628 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200629 goto change;
Alexander Aring3b951ca2015-04-30 17:45:00 +0200630 case STATE_TX_ARET_ON:
Alexander Aring2e0571c2014-07-03 00:20:51 +0200631 case STATE_TX_ON:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100632 tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
Alexander Aring3b951ca2015-04-30 17:45:00 +0200633 /* state change from TRX_OFF to TX_ON or ARET_ON to do
634 * a calibration, we need to reset the timeout for the
Alexander Aringdce481e2015-03-09 13:56:11 +0100635 * next one.
636 */
637 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200638 goto change;
639 default:
640 break;
641 }
642 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200643 case STATE_BUSY_RX_AACK:
644 switch (ctx->to_state) {
Alexander Aringdce481e2015-03-09 13:56:11 +0100645 case STATE_TRX_OFF:
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200646 case STATE_TX_ON:
647 /* Wait for worst case receiving time if we
648 * didn't make a force change from BUSY_RX_AACK
Alexander Aringdce481e2015-03-09 13:56:11 +0100649 * to TX_ON or TRX_OFF.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200650 */
651 if (!force) {
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100652 tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
653 NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200654 goto change;
655 }
656 break;
657 default:
658 break;
659 }
660 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200661 /* Default value, means RESET state */
662 case STATE_P_ON:
663 switch (ctx->to_state) {
664 case STATE_TRX_OFF:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100665 tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
Alexander Aring09e536c2014-07-03 00:20:52 +0200666 goto change;
667 default:
668 break;
669 }
670 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200671 default:
672 break;
673 }
674
675 /* Default delay is 1us in the most cases */
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100676 tim = ktime_set(0, NSEC_PER_USEC);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200677
678change:
Alexander Aringeb3b435e2015-03-09 13:56:10 +0100679 hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200680}
681
682static void
683at86rf230_async_state_change_start(void *context)
684{
685 struct at86rf230_state_change *ctx = context;
686 struct at86rf230_local *lp = ctx->lp;
687 u8 *buf = ctx->buf;
Christoffer Holmstedt4748e862015-04-30 17:44:56 +0200688 const u8 trx_state = buf[1] & TRX_STATE_MASK;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200689 int rc;
690
691 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
692 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
693 udelay(1);
Alexander Aring97fed792014-10-07 10:38:32 +0200694 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
695 at86rf230_async_state_change_start,
696 ctx->irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200697 return;
698 }
699
700 /* Check if we already are in the state which we change in */
701 if (trx_state == ctx->to_state) {
702 if (ctx->complete)
703 ctx->complete(context);
704 return;
705 }
706
707 /* Set current state to the context of state change */
708 ctx->from_state = trx_state;
709
710 /* Going into the next step for a state change which do a timing
711 * relevant delay.
712 */
713 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
714 buf[1] = ctx->to_state;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200715 ctx->msg.complete = at86rf230_async_state_delay;
716 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200717 if (rc) {
718 if (ctx->irq_enable)
Alexander Aringcca990c2015-03-01 21:55:31 +0100719 enable_irq(ctx->irq);
Alexander Aring97fed792014-10-07 10:38:32 +0200720
Alexander Aring4fef7d32014-12-15 10:25:55 +0100721 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200722 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000723}
724
Alexander Aring97fed792014-10-07 10:38:32 +0200725static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200726at86rf230_async_state_change(struct at86rf230_local *lp,
727 struct at86rf230_state_change *ctx,
Alexander Aring97fed792014-10-07 10:38:32 +0200728 const u8 state, void (*complete)(void *context),
729 const bool irq_enable)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000730{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200731 /* Initialization for the state change context */
732 ctx->to_state = state;
733 ctx->complete = complete;
Alexander Aring97fed792014-10-07 10:38:32 +0200734 ctx->irq_enable = irq_enable;
735 at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
736 at86rf230_async_state_change_start,
737 irq_enable);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200738}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000739
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200740static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200741at86rf230_sync_state_change_complete(void *context)
742{
743 struct at86rf230_state_change *ctx = context;
744 struct at86rf230_local *lp = ctx->lp;
745
746 complete(&lp->state_complete);
747}
748
749/* This function do a sync framework above the async state change.
750 * Some callbacks of the IEEE 802.15.4 driver interface need to be
751 * handled synchronously.
752 */
753static int
754at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
755{
Nicholas Mc Guire3e544ef2015-02-14 23:57:48 +0100756 unsigned long rc;
Alexander Aring2e0571c2014-07-03 00:20:51 +0200757
Alexander Aring97fed792014-10-07 10:38:32 +0200758 at86rf230_async_state_change(lp, &lp->state, state,
759 at86rf230_sync_state_change_complete,
760 false);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200761
762 rc = wait_for_completion_timeout(&lp->state_complete,
763 msecs_to_jiffies(100));
Alexander Aringd06c2192014-10-07 10:38:26 +0200764 if (!rc) {
765 at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
Alexander Aring2e0571c2014-07-03 00:20:51 +0200766 return -ETIMEDOUT;
Alexander Aringd06c2192014-10-07 10:38:26 +0200767 }
Alexander Aring2e0571c2014-07-03 00:20:51 +0200768
769 return 0;
770}
771
772static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200773at86rf230_tx_complete(void *context)
774{
775 struct at86rf230_state_change *ctx = context;
776 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000777
Alexander Aringcca990c2015-03-01 21:55:31 +0100778 enable_irq(ctx->irq);
Alexander Aring955aee82014-10-26 09:37:15 +0100779
Alexander Aringef5428a2015-03-01 21:55:29 +0100780 ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200781}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000782
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200783static void
784at86rf230_tx_on(void *context)
785{
786 struct at86rf230_state_change *ctx = context;
787 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000788
Alexander Aring31fa7432015-03-01 21:55:32 +0100789 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
Alexander Aring97fed792014-10-07 10:38:32 +0200790 at86rf230_tx_complete, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200791}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000792
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200793static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200794at86rf230_tx_trac_check(void *context)
795{
796 struct at86rf230_state_change *ctx = context;
797 struct at86rf230_local *lp = ctx->lp;
798 const u8 *buf = ctx->buf;
799 const u8 trac = (buf[1] & 0xe0) >> 5;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000800
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200801 /* If trac status is different than zero we need to do a state change
Alexander Aring2f8cdd92015-04-30 17:45:01 +0200802 * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the
803 * transceiver.
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200804 */
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100805 if (trac)
Alexander Aring97fed792014-10-07 10:38:32 +0200806 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
Alexander Aring2f8cdd92015-04-30 17:45:01 +0200807 at86rf230_tx_on, true);
Alexander Aringc8c7e3d2014-12-19 10:36:50 +0100808 else
809 at86rf230_tx_on(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200810}
811
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200812static void
813at86rf230_tx_trac_status(void *context)
814{
815 struct at86rf230_state_change *ctx = context;
816 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200817
Alexander Aring97fed792014-10-07 10:38:32 +0200818 at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
819 at86rf230_tx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200820}
821
822static void
Alexander Aring74de4c82015-03-01 21:55:30 +0100823at86rf230_rx_read_frame_complete(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200824{
Alexander Aring74de4c82015-03-01 21:55:30 +0100825 struct at86rf230_state_change *ctx = context;
826 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200827 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
Alexander Aring31fa7432015-03-01 21:55:32 +0100828 const u8 *buf = ctx->buf;
Alexander Aring74de4c82015-03-01 21:55:30 +0100829 struct sk_buff *skb;
830 u8 len, lqi;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200831
Alexander Aring74de4c82015-03-01 21:55:30 +0100832 len = buf[1];
833 if (!ieee802154_is_valid_psdu_len(len)) {
834 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
835 len = IEEE802154_MTU;
836 }
837 lqi = buf[2 + len];
838
839 memcpy(rx_local_buf, buf + 2, len);
Alexander Aring263be332015-03-01 21:55:33 +0100840 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100841 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200842
Alexander Aring61a22812014-10-27 17:13:29 +0100843 skb = dev_alloc_skb(IEEE802154_MTU);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200844 if (!skb) {
845 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
846 return;
847 }
848
849 memcpy(skb_put(skb, len), rx_local_buf, len);
Alexander Aringb89c3342014-10-27 17:13:42 +0100850 ieee802154_rx_irqsafe(lp->hw, skb, lqi);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200851}
852
853static void
Alexander Aringcca990c2015-03-01 21:55:31 +0100854at86rf230_rx_read_frame(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200855{
Alexander Aringcca990c2015-03-01 21:55:31 +0100856 struct at86rf230_state_change *ctx = context;
857 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100858 u8 *buf = ctx->buf;
Alexander Aring97fed792014-10-07 10:38:32 +0200859 int rc;
860
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200861 buf[0] = CMD_FB;
Alexander Aring31fa7432015-03-01 21:55:32 +0100862 ctx->trx.len = AT86RF2XX_MAX_BUF;
863 ctx->msg.complete = at86rf230_rx_read_frame_complete;
864 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring97fed792014-10-07 10:38:32 +0200865 if (rc) {
Alexander Aring263be332015-03-01 21:55:33 +0100866 ctx->trx.len = 2;
Alexander Aringcca990c2015-03-01 21:55:31 +0100867 enable_irq(ctx->irq);
Alexander Aring31fa7432015-03-01 21:55:32 +0100868 at86rf230_async_error(lp, ctx, rc);
Alexander Aring97fed792014-10-07 10:38:32 +0200869 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200870}
871
872static void
873at86rf230_rx_trac_check(void *context)
874{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200875 /* Possible check on trac status here. This could be useful to make
876 * some stats why receive is failed. Not used at the moment, but it's
877 * maybe timing relevant. Datasheet doesn't say anything about this.
878 * The programming guide say do it so.
879 */
880
Alexander Aringcca990c2015-03-01 21:55:31 +0100881 at86rf230_rx_read_frame(context);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200882}
883
Alexander Aring97fed792014-10-07 10:38:32 +0200884static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200885at86rf230_irq_trx_end(struct at86rf230_local *lp)
886{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200887 if (lp->is_tx) {
888 lp->is_tx = 0;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200889
890 if (lp->tx_aret)
Alexander Aring97fed792014-10-07 10:38:32 +0200891 at86rf230_async_state_change(lp, &lp->irq,
892 STATE_FORCE_TX_ON,
893 at86rf230_tx_trac_status,
894 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200895 else
Alexander Aring97fed792014-10-07 10:38:32 +0200896 at86rf230_async_state_change(lp, &lp->irq,
897 STATE_RX_AACK_ON,
898 at86rf230_tx_complete,
899 true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200900 } else {
Alexander Aring97fed792014-10-07 10:38:32 +0200901 at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
902 at86rf230_rx_trac_check, true);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200903 }
904}
905
906static void
907at86rf230_irq_status(void *context)
908{
909 struct at86rf230_state_change *ctx = context;
910 struct at86rf230_local *lp = ctx->lp;
Alexander Aring31fa7432015-03-01 21:55:32 +0100911 const u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200912 const u8 irq = buf[1];
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200913
914 if (irq & IRQ_TRX_END) {
Alexander Aring97fed792014-10-07 10:38:32 +0200915 at86rf230_irq_trx_end(lp);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200916 } else {
Alexander Aringcca990c2015-03-01 21:55:31 +0100917 enable_irq(ctx->irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200918 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
919 irq);
920 }
921}
922
923static irqreturn_t at86rf230_isr(int irq, void *data)
924{
925 struct at86rf230_local *lp = data;
926 struct at86rf230_state_change *ctx = &lp->irq;
927 u8 *buf = ctx->buf;
928 int rc;
929
Alexander Aring90566362014-10-07 10:38:29 +0200930 disable_irq_nosync(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200931
932 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200933 ctx->msg.complete = at86rf230_irq_status;
934 rc = spi_async(lp->spi, &ctx->msg);
935 if (rc) {
Alexander Aringe9310212014-10-07 10:38:30 +0200936 enable_irq(irq);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200937 at86rf230_async_error(lp, ctx, rc);
938 return IRQ_NONE;
939 }
940
941 return IRQ_HANDLED;
942}
943
944static void
945at86rf230_write_frame_complete(void *context)
946{
947 struct at86rf230_state_change *ctx = context;
948 struct at86rf230_local *lp = ctx->lp;
949 u8 *buf = ctx->buf;
950 int rc;
951
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200952 ctx->trx.len = 2;
Alexander Aringd2c8bf52015-04-30 17:45:03 +0200953
954 if (gpio_is_valid(lp->slp_tr)) {
955 at86rf230_slp_tr_rising_edge(lp);
956 } else {
957 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
958 buf[1] = STATE_BUSY_TX;
959 ctx->msg.complete = NULL;
960 rc = spi_async(lp->spi, &ctx->msg);
961 if (rc)
962 at86rf230_async_error(lp, ctx, rc);
963 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200964}
965
966static void
967at86rf230_write_frame(void *context)
968{
969 struct at86rf230_state_change *ctx = context;
970 struct at86rf230_local *lp = ctx->lp;
971 struct sk_buff *skb = lp->tx_skb;
Alexander Aring31fa7432015-03-01 21:55:32 +0100972 u8 *buf = ctx->buf;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200973 int rc;
974
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200975 lp->is_tx = 1;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200976
977 buf[0] = CMD_FB | CMD_WRITE;
978 buf[1] = skb->len + 2;
979 memcpy(buf + 2, skb->data, skb->len);
Alexander Aring31fa7432015-03-01 21:55:32 +0100980 ctx->trx.len = skb->len + 2;
981 ctx->msg.complete = at86rf230_write_frame_complete;
982 rc = spi_async(lp->spi, &ctx->msg);
Alexander Aring263be332015-03-01 21:55:33 +0100983 if (rc) {
984 ctx->trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200985 at86rf230_async_error(lp, ctx, rc);
Alexander Aring263be332015-03-01 21:55:33 +0100986 }
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200987}
988
989static void
990at86rf230_xmit_tx_on(void *context)
991{
992 struct at86rf230_state_change *ctx = context;
993 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200994
Alexander Aring97fed792014-10-07 10:38:32 +0200995 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
996 at86rf230_write_frame, false);
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200997}
998
Alexander Aringdce481e2015-03-09 13:56:11 +0100999static void
1000at86rf230_xmit_start(void *context)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001001{
Alexander Aringdce481e2015-03-09 13:56:11 +01001002 struct at86rf230_state_change *ctx = context;
1003 struct at86rf230_local *lp = ctx->lp;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001004
1005 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
1006 * are in STATE_TX_ON. The pfad differs here, so we change
1007 * the complete handler.
1008 */
Alexander Aring85009202015-04-30 17:45:02 +02001009 if (lp->tx_aret) {
1010 if (lp->is_tx_from_off) {
1011 lp->is_tx_from_off = false;
1012 at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
1013 at86rf230_xmit_tx_on,
1014 false);
1015 } else {
1016 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
1017 at86rf230_xmit_tx_on,
1018 false);
1019 }
1020 } else {
Alexander Aringdce481e2015-03-09 13:56:11 +01001021 at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
1022 at86rf230_write_frame, false);
Alexander Aring85009202015-04-30 17:45:02 +02001023 }
Alexander Aringdce481e2015-03-09 13:56:11 +01001024}
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001025
Alexander Aringdce481e2015-03-09 13:56:11 +01001026static int
1027at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
1028{
1029 struct at86rf230_local *lp = hw->priv;
1030 struct at86rf230_state_change *ctx = &lp->tx;
1031
1032 lp->tx_skb = skb;
Alexander Aringba6d2232015-03-01 21:55:28 +01001033 lp->tx_retry = 0;
Alexander Aringdce481e2015-03-09 13:56:11 +01001034
1035 /* After 5 minutes in PLL and the same frequency we run again the
1036 * calibration loops which is recommended by at86rf2xx datasheets.
1037 *
1038 * The calibration is initiate by a state change from TRX_OFF
1039 * to TX_ON, the lp->cal_timeout should be reinit by state_delay
1040 * function then to start in the next 5 minutes.
1041 */
Alexander Aring85009202015-04-30 17:45:02 +02001042 if (time_is_before_jiffies(lp->cal_timeout)) {
1043 lp->is_tx_from_off = true;
Alexander Aringdce481e2015-03-09 13:56:11 +01001044 at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
1045 at86rf230_xmit_start, false);
Alexander Aring85009202015-04-30 17:45:02 +02001046 } else {
Alexander Aringdce481e2015-03-09 13:56:11 +01001047 at86rf230_xmit_start(ctx);
Alexander Aring85009202015-04-30 17:45:02 +02001048 }
Alexander Aring97fed792014-10-07 10:38:32 +02001049
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001050 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001051}
1052
1053static int
Alexander Aring5a504392014-10-25 17:16:34 +02001054at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001055{
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001056 BUG_ON(!level);
1057 *level = 0xbe;
1058 return 0;
1059}
1060
1061static int
Alexander Aring5a504392014-10-25 17:16:34 +02001062at86rf230_start(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001063{
Alexander Aring5a504392014-10-25 17:16:34 +02001064 return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001065}
1066
1067static void
Alexander Aring5a504392014-10-25 17:16:34 +02001068at86rf230_stop(struct ieee802154_hw *hw)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001069{
Alexander Aring5a504392014-10-25 17:16:34 +02001070 at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001071}
1072
1073static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001074at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001075{
1076 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1077}
1078
1079static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001080at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001081{
1082 int rc;
1083
1084 if (channel == 0)
1085 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1086 else
1087 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1088 if (rc < 0)
1089 return rc;
1090
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001091 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001092 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001093 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001094 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001095 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001096 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001097 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001098 if (rc < 0)
1099 return rc;
1100
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001101 /* This sets the symbol_duration according frequency on the 212.
1102 * TODO move this handling while set channel and page in cfg802154.
1103 * We can do that, this timings are according 802.15.4 standard.
1104 * If we do that in cfg802154, this is a more generic calculation.
1105 *
1106 * This should also protected from ifs_timer. Means cancel timer and
1107 * init with a new value. For now, this is okay.
1108 */
1109 if (channel == 0) {
1110 if (page == 0) {
1111 /* SUB:0 and BPSK:0 -> BPSK-20 */
1112 lp->hw->phy->symbol_duration = 50;
1113 } else {
1114 /* SUB:1 and BPSK:0 -> BPSK-40 */
1115 lp->hw->phy->symbol_duration = 25;
1116 }
1117 } else {
1118 if (page == 0)
Alexander Aring2d6dde22014-11-17 08:20:44 +01001119 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001120 lp->hw->phy->symbol_duration = 40;
1121 else
Alexander Aring2d6dde22014-11-17 08:20:44 +01001122 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001123 lp->hw->phy->symbol_duration = 16;
1124 }
1125
1126 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
1127 lp->hw->phy->symbol_duration;
1128 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
1129 lp->hw->phy->symbol_duration;
1130
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001131 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1132}
1133
1134static int
Alexander Aringe37d2ec2014-10-28 18:21:19 +01001135at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001136{
Alexander Aring5a504392014-10-25 17:16:34 +02001137 struct at86rf230_local *lp = hw->priv;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001138 int rc;
1139
Alexander Aringa53d1f72014-07-03 00:20:46 +02001140 rc = lp->data->set_channel(lp, page, channel);
Alexander Aring984e0c62014-07-03 00:20:53 +02001141 /* Wait for PLL */
1142 usleep_range(lp->data->t_channel_switch,
1143 lp->data->t_channel_switch + 10);
Alexander Aringdce481e2015-03-09 13:56:11 +01001144
1145 lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
Alexander Aring820bd662014-11-12 03:36:56 +01001146 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001147}
1148
1149static int
Alexander Aring5a504392014-10-25 17:16:34 +02001150at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001151 struct ieee802154_hw_addr_filt *filt,
1152 unsigned long changed)
1153{
Alexander Aring5a504392014-10-25 17:16:34 +02001154 struct at86rf230_local *lp = hw->priv;
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001155
Alexander Aring57205c12014-10-25 05:25:09 +02001156 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001157 u16 addr = le16_to_cpu(filt->short_addr);
1158
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001159 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001160 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001161 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1162 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001163 }
1164
Alexander Aring57205c12014-10-25 05:25:09 +02001165 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001166 u16 pan = le16_to_cpu(filt->pan_id);
1167
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001168 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001169 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001170 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1171 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001172 }
1173
Alexander Aring57205c12014-10-25 05:25:09 +02001174 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001175 u8 i, addr[8];
1176
1177 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001178 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001179 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001180 for (i = 0; i < 8; i++)
1181 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001182 }
1183
Alexander Aring57205c12014-10-25 05:25:09 +02001184 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001185 dev_vdbg(&lp->spi->dev,
Stefan Schmidte80fb5e2014-12-12 12:45:29 +01001186 "at86rf230_set_hw_addr_filt called for panc change\n");
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001187 if (filt->pan_coord)
1188 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1189 else
1190 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1191 }
1192
1193 return 0;
1194}
1195
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001196static int
Alexander Aringe2eb1732015-05-17 21:44:40 +02001197at86rf230_set_txpower(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001198{
Alexander Aring5a504392014-10-25 17:16:34 +02001199 struct at86rf230_local *lp = hw->priv;
Alexander Aringe2eb1732015-05-17 21:44:40 +02001200 s8 db = mbm / 100;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001201
1202 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1203 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1204 * 0dB.
1205 * thus, supported values for db range from -26 to 5, for 31dB of
1206 * reduction to 0dB of reduction.
1207 */
1208 if (db > 5 || db < -26)
1209 return -EINVAL;
1210
1211 db = -(db - 5);
1212
Jean Sacren677676c2014-03-01 15:54:36 -07001213 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001214}
1215
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001216static int
Alexander Aring5a504392014-10-25 17:16:34 +02001217at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001218{
Alexander Aring5a504392014-10-25 17:16:34 +02001219 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001220
1221 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1222}
1223
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001224static int
Alexander Aring7fe9a382014-12-10 15:33:12 +01001225at86rf230_set_cca_mode(struct ieee802154_hw *hw,
1226 const struct wpan_phy_cca *cca)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001227{
Alexander Aring5a504392014-10-25 17:16:34 +02001228 struct at86rf230_local *lp = hw->priv;
Alexander Aring7fe9a382014-12-10 15:33:12 +01001229 u8 val;
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001230
Alexander Aring7fe9a382014-12-10 15:33:12 +01001231 /* mapping 802.15.4 to driver spec */
1232 switch (cca->mode) {
1233 case NL802154_CCA_ENERGY:
1234 val = 1;
1235 break;
1236 case NL802154_CCA_CARRIER:
1237 val = 2;
1238 break;
1239 case NL802154_CCA_ENERGY_CARRIER:
1240 switch (cca->opt) {
1241 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
1242 val = 3;
1243 break;
1244 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
1245 val = 0;
1246 break;
1247 default:
1248 return -EINVAL;
1249 }
1250 break;
1251 default:
1252 return -EINVAL;
1253 }
1254
1255 return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001256}
1257
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001258static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001259at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1260{
1261 return (level - lp->data->rssi_base_val) * 100 / 207;
1262}
1263
1264static int
1265at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1266{
1267 return (level - lp->data->rssi_base_val) / 2;
1268}
1269
1270static int
Alexander Aring32b23552015-05-17 21:44:41 +02001271at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001272{
Alexander Aring5a504392014-10-25 17:16:34 +02001273 struct at86rf230_local *lp = hw->priv;
Alexander Aring32b23552015-05-17 21:44:41 +02001274 s32 level = mbm / 100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001275
Alexander Aringa53d1f72014-07-03 00:20:46 +02001276 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001277 return -EINVAL;
1278
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001279 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1280 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001281}
1282
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001283static int
Alexander Aring5a504392014-10-25 17:16:34 +02001284at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001285 u8 retries)
1286{
Alexander Aring5a504392014-10-25 17:16:34 +02001287 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001288 int rc;
1289
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001290 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1291 if (rc)
1292 return rc;
1293
1294 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1295 if (rc)
1296 return rc;
1297
Alexander Aring39d7f322014-04-05 13:49:26 +02001298 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001299}
1300
1301static int
Alexander Aring5a504392014-10-25 17:16:34 +02001302at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001303{
Alexander Aring5a504392014-10-25 17:16:34 +02001304 struct at86rf230_local *lp = hw->priv;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001305 int rc = 0;
1306
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001307 lp->tx_aret = retries >= 0;
Alexander Aring850f43a2014-10-07 10:38:27 +02001308 lp->max_frame_retries = retries;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001309
1310 if (retries >= 0)
1311 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1312
1313 return rc;
1314}
1315
Alexander Aring92f45f542014-10-29 21:34:33 +01001316static int
1317at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
1318{
1319 struct at86rf230_local *lp = hw->priv;
1320 int rc;
1321
1322 if (on) {
1323 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
1324 if (rc < 0)
1325 return rc;
1326
1327 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
1328 if (rc < 0)
1329 return rc;
1330 } else {
1331 rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
1332 if (rc < 0)
1333 return rc;
1334
1335 rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
1336 if (rc < 0)
1337 return rc;
1338 }
1339
1340 return 0;
1341}
1342
Alexander Aring16301862014-10-28 18:21:18 +01001343static const struct ieee802154_ops at86rf230_ops = {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001344 .owner = THIS_MODULE,
Alexander Aring955aee82014-10-26 09:37:15 +01001345 .xmit_async = at86rf230_xmit,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001346 .ed = at86rf230_ed,
1347 .set_channel = at86rf230_channel,
1348 .start = at86rf230_start,
1349 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001350 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001351 .set_txpower = at86rf230_set_txpower,
1352 .set_lbt = at86rf230_set_lbt,
1353 .set_cca_mode = at86rf230_set_cca_mode,
1354 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1355 .set_csma_params = at86rf230_set_csma_params,
1356 .set_frame_retries = at86rf230_set_frame_retries,
Alexander Aring92f45f542014-10-29 21:34:33 +01001357 .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001358};
1359
Alexander Aringa53d1f72014-07-03 00:20:46 +02001360static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001361 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001362 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001363 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001364 .t_off_to_aack = 80,
1365 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001366 .t_frame = 4096,
1367 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001368 .rssi_base_val = -91,
1369 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001370 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001371};
1372
1373static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001374 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001375 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001376 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001377 .t_off_to_aack = 110,
1378 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001379 .t_frame = 4096,
1380 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001381 .rssi_base_val = -91,
1382 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001383 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001384};
1385
1386static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001387 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001388 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001389 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001390 .t_off_to_aack = 200,
1391 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001392 .t_frame = 4096,
1393 .t_p_ack = 545,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001394 .rssi_base_val = -100,
1395 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001396 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001397};
1398
Alexander Aringccdaeb22015-02-27 09:58:26 +01001399static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001400{
Alexander Aring1db05582014-07-03 00:20:50 +02001401 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001402 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001403 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001404
Alexander Aring09e536c2014-07-03 00:20:52 +02001405 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001406 if (rc)
1407 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001408
Alexander Aring4af619a2014-04-24 19:09:05 +02001409 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aringc91799c2015-02-27 09:58:30 +01001410 if (irq_type == IRQ_TYPE_EDGE_RISING ||
1411 irq_type == IRQ_TYPE_EDGE_FALLING)
1412 dev_warn(&lp->spi->dev,
1413 "Using edge triggered irq's are not recommended!\n");
Alexander Aring702d2112015-02-27 09:58:29 +01001414 if (irq_type == IRQ_TYPE_EDGE_FALLING ||
1415 irq_type == IRQ_TYPE_LEVEL_LOW)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001416 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001417
Alexander Aring18c65042014-04-24 19:09:18 +02001418 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001419 if (rc)
1420 return rc;
1421
Alexander Aring6bd2b132014-07-03 00:20:49 +02001422 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1423 if (rc)
1424 return rc;
1425
Sascha Herrmann057dad62013-04-14 22:33:29 +00001426 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001427 if (rc)
1428 return rc;
1429
Alexander Aringbe64f072015-02-27 09:58:28 +01001430 /* reset values differs in at86rf231 and at86rf233 */
1431 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
1432 if (rc)
1433 return rc;
1434
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001435 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1436 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1437 if (rc)
1438 return rc;
1439 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1440 if (rc)
1441 return rc;
1442
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001443 /* CLKM changes are applied immediately */
1444 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1445 if (rc)
1446 return rc;
1447
1448 /* Turn CLKM Off */
1449 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1450 if (rc)
1451 return rc;
1452 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001453 usleep_range(lp->data->t_sleep_cycle,
1454 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001455
Alexander Aringccdaeb22015-02-27 09:58:26 +01001456 /* xtal_trim value is calculated by:
1457 * CL = 0.5 * (CX + CTRIM + CPAR)
1458 *
1459 * whereas:
1460 * CL = capacitor of used crystal
1461 * CX = connected capacitors at xtal pins
1462 * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
1463 * but this is different on each board setup. You need to fine
1464 * tuning this value via CTRIM.
1465 * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
1466 * 0 pF upto 4.5 pF.
1467 *
1468 * Examples:
1469 * atben transceiver:
1470 *
1471 * CL = 8 pF
1472 * CX = 12 pF
1473 * CPAR = 3 pF (We assume the magic constant from datasheet)
1474 * CTRIM = 0.9 pF
1475 *
1476 * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
1477 *
1478 * xtal_trim = 0x3
1479 *
1480 * openlabs transceiver:
1481 *
1482 * CL = 16 pF
1483 * CX = 22 pF
1484 * CPAR = 3 pF (We assume the magic constant from datasheet)
1485 * CTRIM = 4.5 pF
1486 *
1487 * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
1488 *
1489 * xtal_trim = 0xf
1490 */
1491 rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
1492 if (rc)
1493 return rc;
1494
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001495 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001496 if (rc)
1497 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001498 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001499 dev_err(&lp->spi->dev, "DVDD error\n");
1500 return -EINVAL;
1501 }
1502
Alexander Aring05e3f2f2014-11-05 20:51:27 +01001503 /* Force setting slotted operation bit to 0. Sometimes the atben
1504 * sets this bit and I don't know why. We set this always force
1505 * to zero while probing.
1506 */
Fengguang Wu6cc63992014-11-06 15:31:57 +08001507 return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001508}
1509
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001510static int
Alexander Aringccdaeb22015-02-27 09:58:26 +01001511at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
1512 u8 *xtal_trim)
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001513{
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001514 struct at86rf230_platform_data *pdata = spi->dev.platform_data;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001515 int ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001516
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001517 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
1518 if (!pdata)
1519 return -ENOENT;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001520
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001521 *rstn = pdata->rstn;
1522 *slp_tr = pdata->slp_tr;
Alexander Aringccdaeb22015-02-27 09:58:26 +01001523 *xtal_trim = pdata->xtal_trim;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001524 return 0;
1525 }
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001526
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001527 *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1528 *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
Alexander Aringccdaeb22015-02-27 09:58:26 +01001529 ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
1530 if (ret < 0 && ret != -EINVAL)
1531 return ret;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001532
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001533 return 0;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001534}
1535
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001536static int
1537at86rf230_detect_device(struct at86rf230_local *lp)
1538{
1539 unsigned int part, version, val;
1540 u16 man_id = 0;
1541 const char *chip;
1542 int rc;
1543
1544 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1545 if (rc)
1546 return rc;
1547 man_id |= val;
1548
1549 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1550 if (rc)
1551 return rc;
1552 man_id |= (val << 8);
1553
1554 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1555 if (rc)
1556 return rc;
1557
Andrey Yurovsky75989682014-12-17 13:14:42 -08001558 rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001559 if (rc)
1560 return rc;
1561
1562 if (man_id != 0x001f) {
1563 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1564 man_id >> 8, man_id & 0xFF);
1565 return -EINVAL;
1566 }
1567
Alexander Aring2ac0f3a2014-10-29 21:34:43 +01001568 lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
Alexander Aringedea8f72015-05-17 21:44:46 +02001569 IEEE802154_HW_CSMA_PARAMS |
1570 IEEE802154_HW_FRAME_RETRIES | IEEE802154_HW_AFILT |
1571 IEEE802154_HW_PROMISCUOUS;
1572
1573 lp->hw->phy->flags = WPAN_PHY_FLAG_TXPOWER |
1574 WPAN_PHY_FLAG_CCA_ED_LEVEL |
1575 WPAN_PHY_FLAG_CCA_MODE;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001576
Alexander Aring8377d222015-05-17 21:44:48 +02001577 lp->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1578 BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER);
1579 lp->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
1580 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
1581
Alexander Aringb48a7c12014-12-10 15:33:14 +01001582 lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
1583
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001584 switch (part) {
1585 case 2:
1586 chip = "at86rf230";
1587 rc = -ENOTSUPP;
1588 break;
1589 case 3:
1590 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001591 lp->data = &at86rf231_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001592 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001593 lp->hw->phy->current_channel = 11;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001594 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001595 break;
1596 case 7:
1597 chip = "at86rf212";
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001598 lp->data = &at86rf212_data;
1599 lp->hw->flags |= IEEE802154_HW_LBT;
Alexander Aring72f655e2015-05-17 21:44:42 +02001600 lp->hw->phy->supported.channels[0] = 0x00007FF;
1601 lp->hw->phy->supported.channels[2] = 0x00007FF;
Andrey Yurovsky4ecc8a52014-12-18 15:36:18 -08001602 lp->hw->phy->current_channel = 5;
1603 lp->hw->phy->symbol_duration = 25;
Alexander Aring8377d222015-05-17 21:44:48 +02001604 lp->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001605 break;
1606 case 11:
1607 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001608 lp->data = &at86rf233_data;
Alexander Aring72f655e2015-05-17 21:44:42 +02001609 lp->hw->phy->supported.channels[0] = 0x7FFF800;
Alexander Aringfe58d012014-11-02 04:18:34 +01001610 lp->hw->phy->current_channel = 13;
Alexander Aring24ccb9f2014-11-12 19:51:57 +01001611 lp->hw->phy->symbol_duration = 16;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001612 break;
1613 default:
Stefan Schmidt2b8b7e22014-12-12 12:45:30 +01001614 chip = "unknown";
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001615 rc = -ENOTSUPP;
1616 break;
1617 }
1618
1619 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1620
1621 return rc;
1622}
1623
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001624static void
1625at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1626{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001627 lp->state.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001628 lp->state.irq = lp->spi->irq;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001629 spi_message_init(&lp->state.msg);
1630 lp->state.msg.context = &lp->state;
Alexander Aring263be332015-03-01 21:55:33 +01001631 lp->state.trx.len = 2;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001632 lp->state.trx.tx_buf = lp->state.buf;
1633 lp->state.trx.rx_buf = lp->state.buf;
1634 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001635 hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1636 lp->state.timer.function = at86rf230_async_state_timer;
Alexander Aring2e0571c2014-07-03 00:20:51 +02001637
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001638 lp->irq.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001639 lp->irq.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001640 spi_message_init(&lp->irq.msg);
1641 lp->irq.msg.context = &lp->irq;
Alexander Aring263be332015-03-01 21:55:33 +01001642 lp->irq.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001643 lp->irq.trx.tx_buf = lp->irq.buf;
1644 lp->irq.trx.rx_buf = lp->irq.buf;
1645 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001646 hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1647 lp->irq.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001648
1649 lp->tx.lp = lp;
Alexander Aringcca990c2015-03-01 21:55:31 +01001650 lp->tx.irq = lp->spi->irq;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001651 spi_message_init(&lp->tx.msg);
1652 lp->tx.msg.context = &lp->tx;
Alexander Aring263be332015-03-01 21:55:33 +01001653 lp->tx.trx.len = 2;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001654 lp->tx.trx.tx_buf = lp->tx.buf;
1655 lp->tx.trx.rx_buf = lp->tx.buf;
1656 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
Alexander Aringeb3b435e2015-03-09 13:56:10 +01001657 hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1658 lp->tx.timer.function = at86rf230_async_state_timer;
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001659}
1660
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001661static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001662{
Alexander Aring5a504392014-10-25 17:16:34 +02001663 struct ieee802154_hw *hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001664 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001665 unsigned int status;
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001666 int rc, irq_type, rstn, slp_tr;
Alexander Aringe3721742015-03-07 22:07:07 +01001667 u8 xtal_trim = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001668
1669 if (!spi->irq) {
1670 dev_err(&spi->dev, "no IRQ specified\n");
1671 return -EINVAL;
1672 }
1673
Alexander Aringccdaeb22015-02-27 09:58:26 +01001674 rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001675 if (rc < 0) {
1676 dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
1677 return rc;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001678 }
1679
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001680 if (gpio_is_valid(rstn)) {
1681 rc = devm_gpio_request_one(&spi->dev, rstn,
Alexander Aring0679e292014-04-24 19:09:09 +02001682 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001683 if (rc)
1684 return rc;
1685 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001686
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001687 if (gpio_is_valid(slp_tr)) {
1688 rc = devm_gpio_request_one(&spi->dev, slp_tr,
Alexander Aring0679e292014-04-24 19:09:09 +02001689 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001690 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001691 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001692 }
1693
1694 /* Reset */
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001695 if (gpio_is_valid(rstn)) {
Alexander Aring3fa27572014-03-15 09:29:06 +01001696 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001697 gpio_set_value(rstn, 0);
Alexander Aring3fa27572014-03-15 09:29:06 +01001698 udelay(1);
Alexander Aringaaa1c4d2015-02-27 09:58:25 +01001699 gpio_set_value(rstn, 1);
Alexander Aring3fa27572014-03-15 09:29:06 +01001700 usleep_range(120, 240);
1701 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001702
Alexander Aring5a504392014-10-25 17:16:34 +02001703 hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
1704 if (!hw)
Alexander Aring0679e292014-04-24 19:09:09 +02001705 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001706
Alexander Aring5a504392014-10-25 17:16:34 +02001707 lp = hw->priv;
1708 lp->hw = hw;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001709 lp->spi = spi;
Alexander Aringd2c8bf52015-04-30 17:45:03 +02001710 lp->slp_tr = slp_tr;
Alexander Aring5a504392014-10-25 17:16:34 +02001711 hw->parent = &spi->dev;
Alexander Aring7c118c12014-11-05 20:51:20 +01001712 hw->vif_data_size = sizeof(*lp);
Alexander Aringf6f4e862014-11-05 20:51:26 +01001713 ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001714
Alexander Aringf76014f772014-07-03 00:20:44 +02001715 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1716 if (IS_ERR(lp->regmap)) {
1717 rc = PTR_ERR(lp->regmap);
1718 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1719 rc);
1720 goto free_dev;
1721 }
1722
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001723 at86rf230_setup_spi_messages(lp);
1724
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001725 rc = at86rf230_detect_device(lp);
1726 if (rc < 0)
1727 goto free_dev;
1728
Alexander Aring2e0571c2014-07-03 00:20:51 +02001729 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001730
1731 spi_set_drvdata(spi, lp);
1732
Alexander Aringccdaeb22015-02-27 09:58:26 +01001733 rc = at86rf230_hw_init(lp, xtal_trim);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001734 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001735 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001736
Alexander Aring19626942014-04-24 19:09:15 +02001737 /* Read irq status register to reset irq line */
1738 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001739 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001740 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001741
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001742 irq_type = irq_get_trigger_type(spi->irq);
1743 if (!irq_type)
1744 irq_type = IRQF_TRIGGER_RISING;
1745
1746 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1747 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001748 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001749 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001750
Alexander Aring5a504392014-10-25 17:16:34 +02001751 rc = ieee802154_register_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001752 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001753 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001754
1755 return rc;
1756
Alexander Aring640985e2014-07-03 00:20:43 +02001757free_dev:
Alexander Aring5a504392014-10-25 17:16:34 +02001758 ieee802154_free_hw(lp->hw);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001759
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001760 return rc;
1761}
1762
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001763static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001764{
1765 struct at86rf230_local *lp = spi_get_drvdata(spi);
1766
Alexander Aring17e84a92014-03-31 03:26:51 +02001767 /* mask all at86rf230 irq's */
1768 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
Alexander Aring5a504392014-10-25 17:16:34 +02001769 ieee802154_unregister_hw(lp->hw);
1770 ieee802154_free_hw(lp->hw);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001771 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001772
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001773 return 0;
1774}
1775
Alexander Aring1086b4f2014-04-24 19:09:11 +02001776static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001777 { .compatible = "atmel,at86rf230", },
1778 { .compatible = "atmel,at86rf231", },
1779 { .compatible = "atmel,at86rf233", },
1780 { .compatible = "atmel,at86rf212", },
1781 { },
1782};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001783MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001784
Alexander Aring90b15522014-04-24 19:09:12 +02001785static const struct spi_device_id at86rf230_device_id[] = {
1786 { .name = "at86rf230", },
1787 { .name = "at86rf231", },
1788 { .name = "at86rf233", },
1789 { .name = "at86rf212", },
1790 { },
1791};
1792MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1793
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001794static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001795 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001796 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001797 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001798 .name = "at86rf230",
1799 .owner = THIS_MODULE,
1800 },
1801 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001802 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001803};
1804
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001805module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001806
1807MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1808MODULE_LICENSE("GPL v2");