blob: a64914a39866c8025fd7c2fd54c3e67aa8968a6a [file] [log] [blame]
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001/*
2 * AT86RF230/RF231 driver
3 *
4 * Copyright (C) 2009-2012 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22 */
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/interrupt.h>
Alexander Aring4af619a2014-04-24 19:09:05 +020026#include <linux/irq.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000027#include <linux/gpio.h>
28#include <linux/delay.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000029#include <linux/spinlock.h>
30#include <linux/spi/spi.h>
31#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020032#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000033#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010034#include <linux/of_gpio.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000035
Alexander Aring1d15d6b2014-07-03 00:20:48 +020036#include <net/ieee802154.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000037#include <net/mac802154.h>
38#include <net/wpan-phy.h>
39
Alexander Aringa53d1f72014-07-03 00:20:46 +020040struct at86rf230_local;
41/* at86rf2xx chip depend data.
42 * All timings are in us.
43 */
44struct at86rf2xx_chip_data {
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;
50 /* short interframe spacing time */
51 u16 t_sifs;
52 /* long interframe spacing time */
53 u16 t_lifs;
54 /* completion timeout for tx in msecs */
55 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020056 int rssi_base_val;
57
58 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020059 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020060};
61
Alexander Aring1d15d6b2014-07-03 00:20:48 +020062#define AT86RF2XX_MAX_BUF (127 + 3)
63
64struct at86rf230_state_change {
65 struct at86rf230_local *lp;
66
67 struct spi_message msg;
68 struct spi_transfer trx;
69 u8 buf[AT86RF2XX_MAX_BUF];
70
71 void (*complete)(void *context);
72 u8 from_state;
73 u8 to_state;
74};
75
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000076struct at86rf230_local {
77 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000078
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020080 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020081 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000082
Alexander Aring2e0571c2014-07-03 00:20:51 +020083 struct completion state_complete;
84 struct at86rf230_state_change state;
85
Alexander Aring1d15d6b2014-07-03 00:20:48 +020086 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020087
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010088 bool tx_aret;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020089 bool is_tx;
90 /* spinlock for is_tx protection */
91 spinlock_t lock;
92 struct completion tx_complete;
93 struct sk_buff *tx_skb;
94 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000095};
96
97#define RG_TRX_STATUS (0x01)
98#define SR_TRX_STATUS 0x01, 0x1f, 0
99#define SR_RESERVED_01_3 0x01, 0x20, 5
100#define SR_CCA_STATUS 0x01, 0x40, 6
101#define SR_CCA_DONE 0x01, 0x80, 7
102#define RG_TRX_STATE (0x02)
103#define SR_TRX_CMD 0x02, 0x1f, 0
104#define SR_TRAC_STATUS 0x02, 0xe0, 5
105#define RG_TRX_CTRL_0 (0x03)
106#define SR_CLKM_CTRL 0x03, 0x07, 0
107#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
108#define SR_PAD_IO_CLKM 0x03, 0x30, 4
109#define SR_PAD_IO 0x03, 0xc0, 6
110#define RG_TRX_CTRL_1 (0x04)
111#define SR_IRQ_POLARITY 0x04, 0x01, 0
112#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
113#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
114#define SR_RX_BL_CTRL 0x04, 0x10, 4
115#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
116#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
117#define SR_PA_EXT_EN 0x04, 0x80, 7
118#define RG_PHY_TX_PWR (0x05)
119#define SR_TX_PWR 0x05, 0x0f, 0
120#define SR_PA_LT 0x05, 0x30, 4
121#define SR_PA_BUF_LT 0x05, 0xc0, 6
122#define RG_PHY_RSSI (0x06)
123#define SR_RSSI 0x06, 0x1f, 0
124#define SR_RND_VALUE 0x06, 0x60, 5
125#define SR_RX_CRC_VALID 0x06, 0x80, 7
126#define RG_PHY_ED_LEVEL (0x07)
127#define SR_ED_LEVEL 0x07, 0xff, 0
128#define RG_PHY_CC_CCA (0x08)
129#define SR_CHANNEL 0x08, 0x1f, 0
130#define SR_CCA_MODE 0x08, 0x60, 5
131#define SR_CCA_REQUEST 0x08, 0x80, 7
132#define RG_CCA_THRES (0x09)
133#define SR_CCA_ED_THRES 0x09, 0x0f, 0
134#define SR_RESERVED_09_1 0x09, 0xf0, 4
135#define RG_RX_CTRL (0x0a)
136#define SR_PDT_THRES 0x0a, 0x0f, 0
137#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
138#define RG_SFD_VALUE (0x0b)
139#define SR_SFD_VALUE 0x0b, 0xff, 0
140#define RG_TRX_CTRL_2 (0x0c)
141#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100142#define SR_SUB_MODE 0x0c, 0x04, 2
143#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100144#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
145#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000146#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
147#define RG_ANT_DIV (0x0d)
148#define SR_ANT_CTRL 0x0d, 0x03, 0
149#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
150#define SR_ANT_DIV_EN 0x0d, 0x08, 3
151#define SR_RESERVED_0d_2 0x0d, 0x70, 4
152#define SR_ANT_SEL 0x0d, 0x80, 7
153#define RG_IRQ_MASK (0x0e)
154#define SR_IRQ_MASK 0x0e, 0xff, 0
155#define RG_IRQ_STATUS (0x0f)
156#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
157#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
158#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
159#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
160#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
161#define SR_IRQ_5_AMI 0x0f, 0x20, 5
162#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
163#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
164#define RG_VREG_CTRL (0x10)
165#define SR_RESERVED_10_6 0x10, 0x03, 0
166#define SR_DVDD_OK 0x10, 0x04, 2
167#define SR_DVREG_EXT 0x10, 0x08, 3
168#define SR_RESERVED_10_3 0x10, 0x30, 4
169#define SR_AVDD_OK 0x10, 0x40, 6
170#define SR_AVREG_EXT 0x10, 0x80, 7
171#define RG_BATMON (0x11)
172#define SR_BATMON_VTH 0x11, 0x0f, 0
173#define SR_BATMON_HR 0x11, 0x10, 4
174#define SR_BATMON_OK 0x11, 0x20, 5
175#define SR_RESERVED_11_1 0x11, 0xc0, 6
176#define RG_XOSC_CTRL (0x12)
177#define SR_XTAL_TRIM 0x12, 0x0f, 0
178#define SR_XTAL_MODE 0x12, 0xf0, 4
179#define RG_RX_SYN (0x15)
180#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
181#define SR_RESERVED_15_2 0x15, 0x70, 4
182#define SR_RX_PDT_DIS 0x15, 0x80, 7
183#define RG_XAH_CTRL_1 (0x17)
184#define SR_RESERVED_17_8 0x17, 0x01, 0
185#define SR_AACK_PROM_MODE 0x17, 0x02, 1
186#define SR_AACK_ACK_TIME 0x17, 0x04, 2
187#define SR_RESERVED_17_5 0x17, 0x08, 3
188#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
189#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100190#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000191#define SR_RESERVED_17_1 0x17, 0x80, 7
192#define RG_FTN_CTRL (0x18)
193#define SR_RESERVED_18_2 0x18, 0x7f, 0
194#define SR_FTN_START 0x18, 0x80, 7
195#define RG_PLL_CF (0x1a)
196#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
197#define SR_PLL_CF_START 0x1a, 0x80, 7
198#define RG_PLL_DCU (0x1b)
199#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
200#define SR_RESERVED_1b_2 0x1b, 0x40, 6
201#define SR_PLL_DCU_START 0x1b, 0x80, 7
202#define RG_PART_NUM (0x1c)
203#define SR_PART_NUM 0x1c, 0xff, 0
204#define RG_VERSION_NUM (0x1d)
205#define SR_VERSION_NUM 0x1d, 0xff, 0
206#define RG_MAN_ID_0 (0x1e)
207#define SR_MAN_ID_0 0x1e, 0xff, 0
208#define RG_MAN_ID_1 (0x1f)
209#define SR_MAN_ID_1 0x1f, 0xff, 0
210#define RG_SHORT_ADDR_0 (0x20)
211#define SR_SHORT_ADDR_0 0x20, 0xff, 0
212#define RG_SHORT_ADDR_1 (0x21)
213#define SR_SHORT_ADDR_1 0x21, 0xff, 0
214#define RG_PAN_ID_0 (0x22)
215#define SR_PAN_ID_0 0x22, 0xff, 0
216#define RG_PAN_ID_1 (0x23)
217#define SR_PAN_ID_1 0x23, 0xff, 0
218#define RG_IEEE_ADDR_0 (0x24)
219#define SR_IEEE_ADDR_0 0x24, 0xff, 0
220#define RG_IEEE_ADDR_1 (0x25)
221#define SR_IEEE_ADDR_1 0x25, 0xff, 0
222#define RG_IEEE_ADDR_2 (0x26)
223#define SR_IEEE_ADDR_2 0x26, 0xff, 0
224#define RG_IEEE_ADDR_3 (0x27)
225#define SR_IEEE_ADDR_3 0x27, 0xff, 0
226#define RG_IEEE_ADDR_4 (0x28)
227#define SR_IEEE_ADDR_4 0x28, 0xff, 0
228#define RG_IEEE_ADDR_5 (0x29)
229#define SR_IEEE_ADDR_5 0x29, 0xff, 0
230#define RG_IEEE_ADDR_6 (0x2a)
231#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
232#define RG_IEEE_ADDR_7 (0x2b)
233#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
234#define RG_XAH_CTRL_0 (0x2c)
235#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
236#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
237#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
238#define RG_CSMA_SEED_0 (0x2d)
239#define SR_CSMA_SEED_0 0x2d, 0xff, 0
240#define RG_CSMA_SEED_1 (0x2e)
241#define SR_CSMA_SEED_1 0x2e, 0x07, 0
242#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
243#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
244#define SR_AACK_SET_PD 0x2e, 0x20, 5
245#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
246#define RG_CSMA_BE (0x2f)
247#define SR_MIN_BE 0x2f, 0x0f, 0
248#define SR_MAX_BE 0x2f, 0xf0, 4
249
250#define CMD_REG 0x80
251#define CMD_REG_MASK 0x3f
252#define CMD_WRITE 0x40
253#define CMD_FB 0x20
254
255#define IRQ_BAT_LOW (1 << 7)
256#define IRQ_TRX_UR (1 << 6)
257#define IRQ_AMI (1 << 5)
258#define IRQ_CCA_ED (1 << 4)
259#define IRQ_TRX_END (1 << 3)
260#define IRQ_RX_START (1 << 2)
261#define IRQ_PLL_UNL (1 << 1)
262#define IRQ_PLL_LOCK (1 << 0)
263
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000264#define IRQ_ACTIVE_HIGH 0
265#define IRQ_ACTIVE_LOW 1
266
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000267#define STATE_P_ON 0x00 /* BUSY */
268#define STATE_BUSY_RX 0x01
269#define STATE_BUSY_TX 0x02
270#define STATE_FORCE_TRX_OFF 0x03
271#define STATE_FORCE_TX_ON 0x04 /* IDLE */
272/* 0x05 */ /* INVALID_PARAMETER */
273#define STATE_RX_ON 0x06
274/* 0x07 */ /* SUCCESS */
275#define STATE_TRX_OFF 0x08
276#define STATE_TX_ON 0x09
277/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
278#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500279#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000280#define STATE_BUSY_RX_AACK 0x11
281#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000282#define STATE_RX_AACK_ON 0x16
283#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000284#define STATE_RX_ON_NOCLK 0x1C
285#define STATE_RX_AACK_ON_NOCLK 0x1D
286#define STATE_BUSY_RX_AACK_NOCLK 0x1E
287#define STATE_TRANSITION_IN_PROGRESS 0x1F
288
Alexander Aringf76014f772014-07-03 00:20:44 +0200289#define AT86RF2XX_NUMREGS 0x3F
290
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200291static int
292at86rf230_async_state_change(struct at86rf230_local *lp,
293 struct at86rf230_state_change *ctx,
294 const u8 state, void (*complete)(void *context));
295
Alexander Aringf76014f772014-07-03 00:20:44 +0200296static inline int
297__at86rf230_write(struct at86rf230_local *lp,
298 unsigned int addr, unsigned int data)
299{
300 return regmap_write(lp->regmap, addr, data);
301}
302
303static inline int
304__at86rf230_read(struct at86rf230_local *lp,
305 unsigned int addr, unsigned int *data)
306{
307 return regmap_read(lp->regmap, addr, data);
308}
309
310static inline int
311at86rf230_read_subreg(struct at86rf230_local *lp,
312 unsigned int addr, unsigned int mask,
313 unsigned int shift, unsigned int *data)
314{
315 int rc;
316
317 rc = __at86rf230_read(lp, addr, data);
318 if (rc > 0)
319 *data = (*data & mask) >> shift;
320
321 return rc;
322}
323
324static inline int
325at86rf230_write_subreg(struct at86rf230_local *lp,
326 unsigned int addr, unsigned int mask,
327 unsigned int shift, unsigned int data)
328{
329 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
330}
331
332static bool
333at86rf230_reg_writeable(struct device *dev, unsigned int reg)
334{
335 switch (reg) {
336 case RG_TRX_STATE:
337 case RG_TRX_CTRL_0:
338 case RG_TRX_CTRL_1:
339 case RG_PHY_TX_PWR:
340 case RG_PHY_ED_LEVEL:
341 case RG_PHY_CC_CCA:
342 case RG_CCA_THRES:
343 case RG_RX_CTRL:
344 case RG_SFD_VALUE:
345 case RG_TRX_CTRL_2:
346 case RG_ANT_DIV:
347 case RG_IRQ_MASK:
348 case RG_VREG_CTRL:
349 case RG_BATMON:
350 case RG_XOSC_CTRL:
351 case RG_RX_SYN:
352 case RG_XAH_CTRL_1:
353 case RG_FTN_CTRL:
354 case RG_PLL_CF:
355 case RG_PLL_DCU:
356 case RG_SHORT_ADDR_0:
357 case RG_SHORT_ADDR_1:
358 case RG_PAN_ID_0:
359 case RG_PAN_ID_1:
360 case RG_IEEE_ADDR_0:
361 case RG_IEEE_ADDR_1:
362 case RG_IEEE_ADDR_2:
363 case RG_IEEE_ADDR_3:
364 case RG_IEEE_ADDR_4:
365 case RG_IEEE_ADDR_5:
366 case RG_IEEE_ADDR_6:
367 case RG_IEEE_ADDR_7:
368 case RG_XAH_CTRL_0:
369 case RG_CSMA_SEED_0:
370 case RG_CSMA_SEED_1:
371 case RG_CSMA_BE:
372 return true;
373 default:
374 return false;
375 }
376}
377
378static bool
379at86rf230_reg_readable(struct device *dev, unsigned int reg)
380{
381 bool rc;
382
383 /* all writeable are also readable */
384 rc = at86rf230_reg_writeable(dev, reg);
385 if (rc)
386 return rc;
387
388 /* readonly regs */
389 switch (reg) {
390 case RG_TRX_STATUS:
391 case RG_PHY_RSSI:
392 case RG_IRQ_STATUS:
393 case RG_PART_NUM:
394 case RG_VERSION_NUM:
395 case RG_MAN_ID_1:
396 case RG_MAN_ID_0:
397 return true;
398 default:
399 return false;
400 }
401}
402
403static bool
404at86rf230_reg_volatile(struct device *dev, unsigned int reg)
405{
406 /* can be changed during runtime */
407 switch (reg) {
408 case RG_TRX_STATUS:
409 case RG_TRX_STATE:
410 case RG_PHY_RSSI:
411 case RG_PHY_ED_LEVEL:
412 case RG_IRQ_STATUS:
413 case RG_VREG_CTRL:
414 return true;
415 default:
416 return false;
417 }
418}
419
420static bool
421at86rf230_reg_precious(struct device *dev, unsigned int reg)
422{
423 /* don't clear irq line on read */
424 switch (reg) {
425 case RG_IRQ_STATUS:
426 return true;
427 default:
428 return false;
429 }
430}
431
432static struct regmap_config at86rf230_regmap_spi_config = {
433 .reg_bits = 8,
434 .val_bits = 8,
435 .write_flag_mask = CMD_REG | CMD_WRITE,
436 .read_flag_mask = CMD_REG,
437 .cache_type = REGCACHE_RBTREE,
438 .max_register = AT86RF2XX_NUMREGS,
439 .writeable_reg = at86rf230_reg_writeable,
440 .readable_reg = at86rf230_reg_readable,
441 .volatile_reg = at86rf230_reg_volatile,
442 .precious_reg = at86rf230_reg_precious,
443};
444
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200445static void
446at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000447{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200448 struct at86rf230_state_change *ctx = context;
449 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000450
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200451 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL);
452}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000453
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200454static void
455at86rf230_async_error(struct at86rf230_local *lp,
456 struct at86rf230_state_change *ctx, int rc)
457{
458 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000459
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200460 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
461 at86rf230_async_error_recover);
462}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000463
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200464/* Generic function to get some register value in async mode */
465static int
466at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
467 struct at86rf230_state_change *ctx,
468 void (*complete)(void *context))
469{
470 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000471
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200472 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
473 ctx->trx.len = 2;
474 ctx->msg.complete = complete;
475 return spi_async(lp->spi, &ctx->msg);
476}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000477
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200478static void
479at86rf230_async_state_assert(void *context)
480{
481 struct at86rf230_state_change *ctx = context;
482 struct at86rf230_local *lp = ctx->lp;
483 const u8 *buf = ctx->buf;
484 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000485
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200486 /* Assert state change */
487 if (trx_state != ctx->to_state) {
488 /* Special handling if transceiver state is in
489 * STATE_BUSY_RX_AACK and a SHR was detected.
490 */
491 if (trx_state == STATE_BUSY_RX_AACK) {
492 /* Undocumented race condition. If we send a state
493 * change to STATE_RX_AACK_ON the transceiver could
494 * change his state automatically to STATE_BUSY_RX_AACK
495 * if a SHR was detected. This is not an error, but we
496 * can't assert this.
497 */
498 if (ctx->to_state == STATE_RX_AACK_ON)
499 goto done;
500
501 /* If we change to STATE_TX_ON without forcing and
502 * transceiver state is STATE_BUSY_RX_AACK, we wait
503 * 'tFrame + tPAck' receiving time. In this time the
504 * PDU should be received. If the transceiver is still
505 * in STATE_BUSY_RX_AACK, we run a force state change
506 * to STATE_TX_ON. This is a timeout handling, if the
507 * transceiver stucks in STATE_BUSY_RX_AACK.
508 */
509 if (ctx->to_state == STATE_TX_ON) {
510 at86rf230_async_state_change(lp, ctx,
511 STATE_FORCE_TX_ON,
512 ctx->complete);
513 return;
514 }
515 }
516
517
518 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
519 ctx->from_state, ctx->to_state, trx_state);
520 }
521
522done:
523 if (ctx->complete)
524 ctx->complete(context);
525}
526
527/* Do state change timing delay. */
528static void
529at86rf230_async_state_delay(void *context)
530{
531 struct at86rf230_state_change *ctx = context;
532 struct at86rf230_local *lp = ctx->lp;
533 struct at86rf2xx_chip_data *c = lp->data;
534 bool force = false;
535 int rc;
536
537 /* The force state changes are will show as normal states in the
538 * state status subregister. We change the to_state to the
539 * corresponding one and remember if it was a force change, this
540 * differs if we do a state change from STATE_BUSY_RX_AACK.
541 */
542 switch (ctx->to_state) {
543 case STATE_FORCE_TX_ON:
544 ctx->to_state = STATE_TX_ON;
545 force = true;
546 break;
547 case STATE_FORCE_TRX_OFF:
548 ctx->to_state = STATE_TRX_OFF;
549 force = true;
550 break;
551 default:
552 break;
553 }
554
555 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200556 case STATE_TRX_OFF:
557 switch (ctx->to_state) {
558 case STATE_RX_AACK_ON:
559 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
560 goto change;
561 case STATE_TX_ON:
562 usleep_range(c->t_off_to_tx_on,
563 c->t_off_to_tx_on + 10);
564 goto change;
565 default:
566 break;
567 }
568 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200569 case STATE_BUSY_RX_AACK:
570 switch (ctx->to_state) {
571 case STATE_TX_ON:
572 /* Wait for worst case receiving time if we
573 * didn't make a force change from BUSY_RX_AACK
574 * to TX_ON.
575 */
576 if (!force) {
577 usleep_range(c->t_frame + c->t_p_ack,
578 c->t_frame + c->t_p_ack + 1000);
579 goto change;
580 }
581 break;
582 default:
583 break;
584 }
585 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200586 /* Default value, means RESET state */
587 case STATE_P_ON:
588 switch (ctx->to_state) {
589 case STATE_TRX_OFF:
590 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
591 goto change;
592 default:
593 break;
594 }
595 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200596 default:
597 break;
598 }
599
600 /* Default delay is 1us in the most cases */
601 udelay(1);
602
603change:
604 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
605 at86rf230_async_state_assert);
606 if (rc)
607 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
608}
609
610static void
611at86rf230_async_state_change_start(void *context)
612{
613 struct at86rf230_state_change *ctx = context;
614 struct at86rf230_local *lp = ctx->lp;
615 u8 *buf = ctx->buf;
616 const u8 trx_state = buf[1] & 0x1f;
617 int rc;
618
619 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
620 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
621 udelay(1);
622 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
623 at86rf230_async_state_change_start);
624 if (rc)
625 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
626 return;
627 }
628
629 /* Check if we already are in the state which we change in */
630 if (trx_state == ctx->to_state) {
631 if (ctx->complete)
632 ctx->complete(context);
633 return;
634 }
635
636 /* Set current state to the context of state change */
637 ctx->from_state = trx_state;
638
639 /* Going into the next step for a state change which do a timing
640 * relevant delay.
641 */
642 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
643 buf[1] = ctx->to_state;
644 ctx->trx.len = 2;
645 ctx->msg.complete = at86rf230_async_state_delay;
646 rc = spi_async(lp->spi, &ctx->msg);
647 if (rc)
648 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000649}
650
651static int
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200652at86rf230_async_state_change(struct at86rf230_local *lp,
653 struct at86rf230_state_change *ctx,
654 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000655{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200656 /* Initialization for the state change context */
657 ctx->to_state = state;
658 ctx->complete = complete;
659 return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
660 at86rf230_async_state_change_start);
661}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000662
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200663static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200664at86rf230_sync_state_change_complete(void *context)
665{
666 struct at86rf230_state_change *ctx = context;
667 struct at86rf230_local *lp = ctx->lp;
668
669 complete(&lp->state_complete);
670}
671
672/* This function do a sync framework above the async state change.
673 * Some callbacks of the IEEE 802.15.4 driver interface need to be
674 * handled synchronously.
675 */
676static int
677at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
678{
679 int rc;
680
681 rc = at86rf230_async_state_change(lp, &lp->state, state,
682 at86rf230_sync_state_change_complete);
683 if (rc) {
684 at86rf230_async_error(lp, &lp->state, rc);
685 return rc;
686 }
687
688 rc = wait_for_completion_timeout(&lp->state_complete,
689 msecs_to_jiffies(100));
690 if (!rc)
691 return -ETIMEDOUT;
692
693 return 0;
694}
695
696static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200697at86rf230_tx_complete(void *context)
698{
699 struct at86rf230_state_change *ctx = context;
700 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000701
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200702 complete(&lp->tx_complete);
703}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000704
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200705static void
706at86rf230_tx_on(void *context)
707{
708 struct at86rf230_state_change *ctx = context;
709 struct at86rf230_local *lp = ctx->lp;
710 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000711
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200712 rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
713 at86rf230_tx_complete);
714 if (rc)
715 at86rf230_async_error(lp, ctx, rc);
716}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000717
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200718static void
719at86rf230_tx_trac_error(void *context)
720{
721 struct at86rf230_state_change *ctx = context;
722 struct at86rf230_local *lp = ctx->lp;
723 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000724
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200725 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
726 at86rf230_tx_on);
727 if (rc)
728 at86rf230_async_error(lp, ctx, rc);
729}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000730
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200731static void
732at86rf230_tx_trac_check(void *context)
733{
734 struct at86rf230_state_change *ctx = context;
735 struct at86rf230_local *lp = ctx->lp;
736 const u8 *buf = ctx->buf;
737 const u8 trac = (buf[1] & 0xe0) >> 5;
738 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000739
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200740 /* If trac status is different than zero we need to do a state change
741 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
742 * state to TX_ON.
743 */
744 if (trac) {
745 rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
746 at86rf230_tx_trac_error);
747 if (rc)
748 at86rf230_async_error(lp, ctx, rc);
749 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000750 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000751
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200752 at86rf230_tx_on(context);
753}
754
755
756static void
757at86rf230_tx_trac_status(void *context)
758{
759 struct at86rf230_state_change *ctx = context;
760 struct at86rf230_local *lp = ctx->lp;
761 int rc;
762
763 rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
764 at86rf230_tx_trac_check);
765 if (rc)
766 at86rf230_async_error(lp, ctx, rc);
767}
768
769static void
770at86rf230_rx(struct at86rf230_local *lp,
771 const u8 *data, u8 len)
772{
773 u8 lqi;
774 struct sk_buff *skb;
775 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
776
777 if (len < 2)
778 return;
779
780 /* read full frame buffer and invalid lqi value to lowest
781 * indicator if frame was is in a corrupted state.
782 */
783 if (len > IEEE802154_MTU) {
784 lqi = 0;
785 len = IEEE802154_MTU;
786 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
787 } else {
788 lqi = data[len];
789 }
790
791 memcpy(rx_local_buf, data, len);
792 enable_irq(lp->spi->irq);
793
794 skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC);
795 if (!skb) {
796 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
797 return;
798 }
799
800 memcpy(skb_put(skb, len), rx_local_buf, len);
801
802 /* We do not put CRC into the frame */
803 skb_trim(skb, len - 2);
804
805 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
806}
807
808static void
809at86rf230_rx_read_frame_complete(void *context)
810{
811 struct at86rf230_state_change *ctx = context;
812 struct at86rf230_local *lp = ctx->lp;
813 const u8 *buf = lp->irq.buf;
814 const u8 len = buf[1];
815
816 at86rf230_rx(lp, buf + 2, len);
817}
818
819static int
820at86rf230_rx_read_frame(struct at86rf230_local *lp)
821{
822 u8 *buf = lp->irq.buf;
823
824 buf[0] = CMD_FB;
825 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
826 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
827 return spi_async(lp->spi, &lp->irq.msg);
828}
829
830static void
831at86rf230_rx_trac_check(void *context)
832{
833 struct at86rf230_state_change *ctx = context;
834 struct at86rf230_local *lp = ctx->lp;
835 int rc;
836
837 /* Possible check on trac status here. This could be useful to make
838 * some stats why receive is failed. Not used at the moment, but it's
839 * maybe timing relevant. Datasheet doesn't say anything about this.
840 * The programming guide say do it so.
841 */
842
843 rc = at86rf230_rx_read_frame(lp);
844 if (rc) {
845 enable_irq(lp->spi->irq);
846 at86rf230_async_error(lp, ctx, rc);
847 }
848}
849
850static int
851at86rf230_irq_trx_end(struct at86rf230_local *lp)
852{
853 spin_lock(&lp->lock);
854 if (lp->is_tx) {
855 lp->is_tx = 0;
856 spin_unlock(&lp->lock);
857 enable_irq(lp->spi->irq);
858
859 if (lp->tx_aret)
860 return at86rf230_async_state_change(lp, &lp->irq,
861 STATE_FORCE_TX_ON,
862 at86rf230_tx_trac_status);
863 else
864 return at86rf230_async_state_change(lp, &lp->irq,
865 STATE_RX_AACK_ON,
866 at86rf230_tx_complete);
867 } else {
868 spin_unlock(&lp->lock);
869 return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
870 at86rf230_rx_trac_check);
871 }
872}
873
874static void
875at86rf230_irq_status(void *context)
876{
877 struct at86rf230_state_change *ctx = context;
878 struct at86rf230_local *lp = ctx->lp;
879 const u8 *buf = lp->irq.buf;
880 const u8 irq = buf[1];
881 int rc;
882
883 if (irq & IRQ_TRX_END) {
884 rc = at86rf230_irq_trx_end(lp);
885 if (rc)
886 at86rf230_async_error(lp, ctx, rc);
887 } else {
888 enable_irq(lp->spi->irq);
889 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
890 irq);
891 }
892}
893
894static irqreturn_t at86rf230_isr(int irq, void *data)
895{
896 struct at86rf230_local *lp = data;
897 struct at86rf230_state_change *ctx = &lp->irq;
898 u8 *buf = ctx->buf;
899 int rc;
900
901 disable_irq_nosync(lp->spi->irq);
902
903 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
904 ctx->trx.len = 2;
905 ctx->msg.complete = at86rf230_irq_status;
906 rc = spi_async(lp->spi, &ctx->msg);
907 if (rc) {
908 at86rf230_async_error(lp, ctx, rc);
909 return IRQ_NONE;
910 }
911
912 return IRQ_HANDLED;
913}
914
915static void
916at86rf230_write_frame_complete(void *context)
917{
918 struct at86rf230_state_change *ctx = context;
919 struct at86rf230_local *lp = ctx->lp;
920 u8 *buf = ctx->buf;
921 int rc;
922
923 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
924 buf[1] = STATE_BUSY_TX;
925 ctx->trx.len = 2;
926 ctx->msg.complete = NULL;
927 rc = spi_async(lp->spi, &ctx->msg);
928 if (rc)
929 at86rf230_async_error(lp, ctx, rc);
930}
931
932static void
933at86rf230_write_frame(void *context)
934{
935 struct at86rf230_state_change *ctx = context;
936 struct at86rf230_local *lp = ctx->lp;
937 struct sk_buff *skb = lp->tx_skb;
938 u8 *buf = lp->tx.buf;
939 int rc;
940
941 spin_lock(&lp->lock);
942 lp->is_tx = 1;
943 spin_unlock(&lp->lock);
944
945 buf[0] = CMD_FB | CMD_WRITE;
946 buf[1] = skb->len + 2;
947 memcpy(buf + 2, skb->data, skb->len);
948 lp->tx.trx.len = skb->len + 2;
949 lp->tx.msg.complete = at86rf230_write_frame_complete;
950 rc = spi_async(lp->spi, &lp->tx.msg);
951 if (rc)
952 at86rf230_async_error(lp, ctx, rc);
953}
954
955static void
956at86rf230_xmit_tx_on(void *context)
957{
958 struct at86rf230_state_change *ctx = context;
959 struct at86rf230_local *lp = ctx->lp;
960 int rc;
961
962 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
963 at86rf230_write_frame);
964 if (rc)
965 at86rf230_async_error(lp, ctx, rc);
966}
967
968static int
969at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
970{
971 struct at86rf230_local *lp = dev->priv;
972 struct at86rf230_state_change *ctx = &lp->tx;
973
974 void (*tx_complete)(void *context) = at86rf230_write_frame;
975 int rc;
976
977 lp->tx_skb = skb;
978
979 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
980 * are in STATE_TX_ON. The pfad differs here, so we change
981 * the complete handler.
982 */
983 if (lp->tx_aret)
984 tx_complete = at86rf230_xmit_tx_on;
985
986 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
987 tx_complete);
988 if (rc) {
989 at86rf230_async_error(lp, ctx, rc);
990 return rc;
991 }
992 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
993 msecs_to_jiffies(lp->data->t_tx_timeout));
994 if (!rc) {
995 at86rf230_async_error(lp, ctx, rc);
996 return -ETIMEDOUT;
997 }
998
999 /* Interfame spacing time, which is phy depend.
1000 * TODO
1001 * Move this handling in MAC 802.15.4 layer.
1002 * This is currently a workaround to avoid fragmenation issues.
1003 */
1004 if (skb->len > 18)
1005 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
1006 else
1007 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
1008
1009 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001010}
1011
1012static int
1013at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
1014{
1015 might_sleep();
1016 BUG_ON(!level);
1017 *level = 0xbe;
1018 return 0;
1019}
1020
1021static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001022at86rf230_start(struct ieee802154_dev *dev)
1023{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001024 return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001025}
1026
1027static void
1028at86rf230_stop(struct ieee802154_dev *dev)
1029{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001030 at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001031}
1032
1033static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001034at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001035{
1036 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1037}
1038
1039static int
1040at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1041{
1042 int rc;
1043
1044 if (channel == 0)
1045 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1046 else
1047 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1048 if (rc < 0)
1049 return rc;
1050
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001051 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001052 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001053 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001054 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001055 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001056 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001057 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001058 if (rc < 0)
1059 return rc;
1060
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001061 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1062}
1063
1064static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001065at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1066{
1067 struct at86rf230_local *lp = dev->priv;
1068 int rc;
1069
1070 might_sleep();
1071
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001072 if (page < 0 || page > 31 ||
1073 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001074 WARN_ON(1);
1075 return -EINVAL;
1076 }
1077
Alexander Aringa53d1f72014-07-03 00:20:46 +02001078 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001079 if (rc < 0)
1080 return rc;
1081
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001082 msleep(1); /* Wait for PLL */
1083 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001084 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001085
1086 return 0;
1087}
1088
1089static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001090at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1091 struct ieee802154_hw_addr_filt *filt,
1092 unsigned long changed)
1093{
1094 struct at86rf230_local *lp = dev->priv;
1095
1096 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001097 u16 addr = le16_to_cpu(filt->short_addr);
1098
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001099 dev_vdbg(&lp->spi->dev,
1100 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001101 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1102 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001103 }
1104
1105 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001106 u16 pan = le16_to_cpu(filt->pan_id);
1107
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001108 dev_vdbg(&lp->spi->dev,
1109 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001110 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1111 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001112 }
1113
1114 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001115 u8 i, addr[8];
1116
1117 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001118 dev_vdbg(&lp->spi->dev,
1119 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001120 for (i = 0; i < 8; i++)
1121 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001122 }
1123
1124 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
1125 dev_vdbg(&lp->spi->dev,
1126 "at86rf230_set_hw_addr_filt called for panc change\n");
1127 if (filt->pan_coord)
1128 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1129 else
1130 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1131 }
1132
1133 return 0;
1134}
1135
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001136static int
Alexander Aring640985e2014-07-03 00:20:43 +02001137at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001138{
1139 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001140
1141 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1142 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1143 * 0dB.
1144 * thus, supported values for db range from -26 to 5, for 31dB of
1145 * reduction to 0dB of reduction.
1146 */
1147 if (db > 5 || db < -26)
1148 return -EINVAL;
1149
1150 db = -(db - 5);
1151
Jean Sacren677676c2014-03-01 15:54:36 -07001152 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001153}
1154
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001155static int
Alexander Aring640985e2014-07-03 00:20:43 +02001156at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001157{
1158 struct at86rf230_local *lp = dev->priv;
1159
1160 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1161}
1162
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001163static int
Alexander Aring640985e2014-07-03 00:20:43 +02001164at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001165{
1166 struct at86rf230_local *lp = dev->priv;
1167
1168 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1169}
1170
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001171static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001172at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1173{
1174 return (level - lp->data->rssi_base_val) * 100 / 207;
1175}
1176
1177static int
1178at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1179{
1180 return (level - lp->data->rssi_base_val) / 2;
1181}
1182
1183static int
Alexander Aring640985e2014-07-03 00:20:43 +02001184at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001185{
1186 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001187
Alexander Aringa53d1f72014-07-03 00:20:46 +02001188 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001189 return -EINVAL;
1190
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001191 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1192 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001193}
1194
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001195static int
Alexander Aring640985e2014-07-03 00:20:43 +02001196at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001197 u8 retries)
1198{
1199 struct at86rf230_local *lp = dev->priv;
1200 int rc;
1201
1202 if (min_be > max_be || max_be > 8 || retries > 5)
1203 return -EINVAL;
1204
1205 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1206 if (rc)
1207 return rc;
1208
1209 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1210 if (rc)
1211 return rc;
1212
Alexander Aring39d7f322014-04-05 13:49:26 +02001213 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001214}
1215
1216static int
Alexander Aring640985e2014-07-03 00:20:43 +02001217at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001218{
1219 struct at86rf230_local *lp = dev->priv;
1220 int rc = 0;
1221
1222 if (retries < -1 || retries > 15)
1223 return -EINVAL;
1224
1225 lp->tx_aret = retries >= 0;
1226
1227 if (retries >= 0)
1228 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1229
1230 return rc;
1231}
1232
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001233static struct ieee802154_ops at86rf230_ops = {
1234 .owner = THIS_MODULE,
1235 .xmit = at86rf230_xmit,
1236 .ed = at86rf230_ed,
1237 .set_channel = at86rf230_channel,
1238 .start = at86rf230_start,
1239 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001240 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001241 .set_txpower = at86rf230_set_txpower,
1242 .set_lbt = at86rf230_set_lbt,
1243 .set_cca_mode = at86rf230_set_cca_mode,
1244 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1245 .set_csma_params = at86rf230_set_csma_params,
1246 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001247};
1248
Alexander Aringa53d1f72014-07-03 00:20:46 +02001249static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring09e536c2014-07-03 00:20:52 +02001250 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001251 .t_off_to_aack = 80,
1252 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001253 .t_frame = 4096,
1254 .t_p_ack = 545,
1255 .t_sifs = 192,
1256 .t_lifs = 480,
1257 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001258 .rssi_base_val = -91,
1259 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001260 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001261};
1262
1263static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring09e536c2014-07-03 00:20:52 +02001264 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001265 .t_off_to_aack = 110,
1266 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001267 .t_frame = 4096,
1268 .t_p_ack = 545,
1269 .t_sifs = 192,
1270 .t_lifs = 480,
1271 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001272 .rssi_base_val = -91,
1273 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001274 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001275};
1276
1277static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring09e536c2014-07-03 00:20:52 +02001278 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001279 .t_off_to_aack = 200,
1280 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001281 .t_frame = 4096,
1282 .t_p_ack = 545,
1283 .t_sifs = 192,
1284 .t_lifs = 480,
1285 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001286 .rssi_base_val = -100,
1287 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001288 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001289};
1290
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001291static int at86rf230_hw_init(struct at86rf230_local *lp)
1292{
Alexander Aring1db05582014-07-03 00:20:50 +02001293 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001294 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001295 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001296
Alexander Aring09e536c2014-07-03 00:20:52 +02001297 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001298 if (rc)
1299 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001300
Alexander Aring4af619a2014-04-24 19:09:05 +02001301 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001302 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001303 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001304
Alexander Aring18c65042014-04-24 19:09:18 +02001305 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001306 if (rc)
1307 return rc;
1308
Alexander Aring6bd2b132014-07-03 00:20:49 +02001309 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1310 if (rc)
1311 return rc;
1312
Sascha Herrmann057dad62013-04-14 22:33:29 +00001313 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001314 if (rc)
1315 return rc;
1316
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001317 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1318 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1319 if (rc)
1320 return rc;
1321 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1322 if (rc)
1323 return rc;
1324
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001325 /* CLKM changes are applied immediately */
1326 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1327 if (rc)
1328 return rc;
1329
1330 /* Turn CLKM Off */
1331 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1332 if (rc)
1333 return rc;
1334 /* Wait the next SLEEP cycle */
1335 msleep(100);
1336
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001337 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001338 if (rc)
1339 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001340 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001341 dev_err(&lp->spi->dev, "DVDD error\n");
1342 return -EINVAL;
1343 }
1344
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001345 return 0;
1346}
1347
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001348static struct at86rf230_platform_data *
1349at86rf230_get_pdata(struct spi_device *spi)
1350{
1351 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001352
1353 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1354 return spi->dev.platform_data;
1355
1356 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1357 if (!pdata)
1358 goto done;
1359
1360 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1361 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1362
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001363 spi->dev.platform_data = pdata;
1364done:
1365 return pdata;
1366}
1367
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001368static int
1369at86rf230_detect_device(struct at86rf230_local *lp)
1370{
1371 unsigned int part, version, val;
1372 u16 man_id = 0;
1373 const char *chip;
1374 int rc;
1375
1376 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1377 if (rc)
1378 return rc;
1379 man_id |= val;
1380
1381 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1382 if (rc)
1383 return rc;
1384 man_id |= (val << 8);
1385
1386 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1387 if (rc)
1388 return rc;
1389
1390 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1391 if (rc)
1392 return rc;
1393
1394 if (man_id != 0x001f) {
1395 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1396 man_id >> 8, man_id & 0xFF);
1397 return -EINVAL;
1398 }
1399
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001400 lp->dev->extra_tx_headroom = 0;
1401 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1402 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1403
1404 switch (part) {
1405 case 2:
1406 chip = "at86rf230";
1407 rc = -ENOTSUPP;
1408 break;
1409 case 3:
1410 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001411 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001412 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1413 break;
1414 case 7:
1415 chip = "at86rf212";
1416 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001417 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001418 lp->dev->flags |= IEEE802154_HW_LBT;
1419 lp->dev->phy->channels_supported[0] = 0x00007FF;
1420 lp->dev->phy->channels_supported[2] = 0x00007FF;
1421 } else {
1422 rc = -ENOTSUPP;
1423 }
1424 break;
1425 case 11:
1426 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001427 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001428 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1429 break;
1430 default:
1431 chip = "unkown";
1432 rc = -ENOTSUPP;
1433 break;
1434 }
1435
1436 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1437
1438 return rc;
1439}
1440
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001441static void
1442at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1443{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001444 lp->state.lp = lp;
1445 spi_message_init(&lp->state.msg);
1446 lp->state.msg.context = &lp->state;
1447 lp->state.trx.tx_buf = lp->state.buf;
1448 lp->state.trx.rx_buf = lp->state.buf;
1449 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1450
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001451 lp->irq.lp = lp;
1452 spi_message_init(&lp->irq.msg);
1453 lp->irq.msg.context = &lp->irq;
1454 lp->irq.trx.tx_buf = lp->irq.buf;
1455 lp->irq.trx.rx_buf = lp->irq.buf;
1456 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1457
1458 lp->tx.lp = lp;
1459 spi_message_init(&lp->tx.msg);
1460 lp->tx.msg.context = &lp->tx;
1461 lp->tx.trx.tx_buf = lp->tx.buf;
1462 lp->tx.trx.rx_buf = lp->tx.buf;
1463 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1464}
1465
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001466static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001467{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001468 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001469 struct ieee802154_dev *dev;
1470 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001471 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001472 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001473
1474 if (!spi->irq) {
1475 dev_err(&spi->dev, "no IRQ specified\n");
1476 return -EINVAL;
1477 }
1478
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001479 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001480 if (!pdata) {
1481 dev_err(&spi->dev, "no platform_data\n");
1482 return -EINVAL;
1483 }
1484
Alexander Aring3fa27572014-03-15 09:29:06 +01001485 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001486 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1487 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001488 if (rc)
1489 return rc;
1490 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001491
1492 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001493 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1494 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001495 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001496 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001497 }
1498
1499 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001500 if (gpio_is_valid(pdata->rstn)) {
1501 udelay(1);
1502 gpio_set_value(pdata->rstn, 0);
1503 udelay(1);
1504 gpio_set_value(pdata->rstn, 1);
1505 usleep_range(120, 240);
1506 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001507
Alexander Aring640985e2014-07-03 00:20:43 +02001508 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001509 if (!dev)
1510 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001511
1512 lp = dev->priv;
1513 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001514 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001515 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001516
Alexander Aringf76014f772014-07-03 00:20:44 +02001517 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1518 if (IS_ERR(lp->regmap)) {
1519 rc = PTR_ERR(lp->regmap);
1520 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1521 rc);
1522 goto free_dev;
1523 }
1524
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001525 at86rf230_setup_spi_messages(lp);
1526
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001527 rc = at86rf230_detect_device(lp);
1528 if (rc < 0)
1529 goto free_dev;
1530
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001531 spin_lock_init(&lp->lock);
1532 init_completion(&lp->tx_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001533 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001534
1535 spi_set_drvdata(spi, lp);
1536
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001537 rc = at86rf230_hw_init(lp);
1538 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001539 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001540
Alexander Aring19626942014-04-24 19:09:15 +02001541 /* Read irq status register to reset irq line */
1542 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001543 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001544 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001545
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001546 irq_type = irq_get_trigger_type(spi->irq);
1547 if (!irq_type)
1548 irq_type = IRQF_TRIGGER_RISING;
1549
1550 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1551 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001552 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001553 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001554
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001555 rc = ieee802154_register_device(lp->dev);
1556 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001557 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001558
1559 return rc;
1560
Alexander Aring640985e2014-07-03 00:20:43 +02001561free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001562 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001563
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001564 return rc;
1565}
1566
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001567static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001568{
1569 struct at86rf230_local *lp = spi_get_drvdata(spi);
1570
Alexander Aring17e84a92014-03-31 03:26:51 +02001571 /* mask all at86rf230 irq's */
1572 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001573 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001574 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001575 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001576
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001577 return 0;
1578}
1579
Alexander Aring1086b4f2014-04-24 19:09:11 +02001580static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001581 { .compatible = "atmel,at86rf230", },
1582 { .compatible = "atmel,at86rf231", },
1583 { .compatible = "atmel,at86rf233", },
1584 { .compatible = "atmel,at86rf212", },
1585 { },
1586};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001587MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001588
Alexander Aring90b15522014-04-24 19:09:12 +02001589static const struct spi_device_id at86rf230_device_id[] = {
1590 { .name = "at86rf230", },
1591 { .name = "at86rf231", },
1592 { .name = "at86rf233", },
1593 { .name = "at86rf212", },
1594 { },
1595};
1596MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1597
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001598static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001599 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001600 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001601 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001602 .name = "at86rf230",
1603 .owner = THIS_MODULE,
1604 },
1605 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001606 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001607};
1608
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001609module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001610
1611MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1612MODULE_LICENSE("GPL v2");