blob: dbf85b85a70881e3367a514157dbad5b2cdc4391 [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 Aring984e0c62014-07-03 00:20:53 +020045 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020046 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020047 u16 t_off_to_aack;
48 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020049 u16 t_frame;
50 u16 t_p_ack;
51 /* short interframe spacing time */
52 u16 t_sifs;
53 /* long interframe spacing time */
54 u16 t_lifs;
55 /* completion timeout for tx in msecs */
56 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020057 int rssi_base_val;
58
59 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020060 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020061};
62
Alexander Aring1d15d6b2014-07-03 00:20:48 +020063#define AT86RF2XX_MAX_BUF (127 + 3)
64
65struct at86rf230_state_change {
66 struct at86rf230_local *lp;
67
68 struct spi_message msg;
69 struct spi_transfer trx;
70 u8 buf[AT86RF2XX_MAX_BUF];
71
72 void (*complete)(void *context);
73 u8 from_state;
74 u8 to_state;
75};
76
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000077struct at86rf230_local {
78 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000079
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000080 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020081 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020082 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000083
Alexander Aring2e0571c2014-07-03 00:20:51 +020084 struct completion state_complete;
85 struct at86rf230_state_change state;
86
Alexander Aring1d15d6b2014-07-03 00:20:48 +020087 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020088
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010089 bool tx_aret;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020090 bool is_tx;
91 /* spinlock for is_tx protection */
92 spinlock_t lock;
93 struct completion tx_complete;
94 struct sk_buff *tx_skb;
95 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000096};
97
98#define RG_TRX_STATUS (0x01)
99#define SR_TRX_STATUS 0x01, 0x1f, 0
100#define SR_RESERVED_01_3 0x01, 0x20, 5
101#define SR_CCA_STATUS 0x01, 0x40, 6
102#define SR_CCA_DONE 0x01, 0x80, 7
103#define RG_TRX_STATE (0x02)
104#define SR_TRX_CMD 0x02, 0x1f, 0
105#define SR_TRAC_STATUS 0x02, 0xe0, 5
106#define RG_TRX_CTRL_0 (0x03)
107#define SR_CLKM_CTRL 0x03, 0x07, 0
108#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
109#define SR_PAD_IO_CLKM 0x03, 0x30, 4
110#define SR_PAD_IO 0x03, 0xc0, 6
111#define RG_TRX_CTRL_1 (0x04)
112#define SR_IRQ_POLARITY 0x04, 0x01, 0
113#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
114#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
115#define SR_RX_BL_CTRL 0x04, 0x10, 4
116#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
117#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
118#define SR_PA_EXT_EN 0x04, 0x80, 7
119#define RG_PHY_TX_PWR (0x05)
120#define SR_TX_PWR 0x05, 0x0f, 0
121#define SR_PA_LT 0x05, 0x30, 4
122#define SR_PA_BUF_LT 0x05, 0xc0, 6
123#define RG_PHY_RSSI (0x06)
124#define SR_RSSI 0x06, 0x1f, 0
125#define SR_RND_VALUE 0x06, 0x60, 5
126#define SR_RX_CRC_VALID 0x06, 0x80, 7
127#define RG_PHY_ED_LEVEL (0x07)
128#define SR_ED_LEVEL 0x07, 0xff, 0
129#define RG_PHY_CC_CCA (0x08)
130#define SR_CHANNEL 0x08, 0x1f, 0
131#define SR_CCA_MODE 0x08, 0x60, 5
132#define SR_CCA_REQUEST 0x08, 0x80, 7
133#define RG_CCA_THRES (0x09)
134#define SR_CCA_ED_THRES 0x09, 0x0f, 0
135#define SR_RESERVED_09_1 0x09, 0xf0, 4
136#define RG_RX_CTRL (0x0a)
137#define SR_PDT_THRES 0x0a, 0x0f, 0
138#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
139#define RG_SFD_VALUE (0x0b)
140#define SR_SFD_VALUE 0x0b, 0xff, 0
141#define RG_TRX_CTRL_2 (0x0c)
142#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100143#define SR_SUB_MODE 0x0c, 0x04, 2
144#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100145#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
146#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000147#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
148#define RG_ANT_DIV (0x0d)
149#define SR_ANT_CTRL 0x0d, 0x03, 0
150#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
151#define SR_ANT_DIV_EN 0x0d, 0x08, 3
152#define SR_RESERVED_0d_2 0x0d, 0x70, 4
153#define SR_ANT_SEL 0x0d, 0x80, 7
154#define RG_IRQ_MASK (0x0e)
155#define SR_IRQ_MASK 0x0e, 0xff, 0
156#define RG_IRQ_STATUS (0x0f)
157#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
158#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
159#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
160#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
161#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
162#define SR_IRQ_5_AMI 0x0f, 0x20, 5
163#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
164#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
165#define RG_VREG_CTRL (0x10)
166#define SR_RESERVED_10_6 0x10, 0x03, 0
167#define SR_DVDD_OK 0x10, 0x04, 2
168#define SR_DVREG_EXT 0x10, 0x08, 3
169#define SR_RESERVED_10_3 0x10, 0x30, 4
170#define SR_AVDD_OK 0x10, 0x40, 6
171#define SR_AVREG_EXT 0x10, 0x80, 7
172#define RG_BATMON (0x11)
173#define SR_BATMON_VTH 0x11, 0x0f, 0
174#define SR_BATMON_HR 0x11, 0x10, 4
175#define SR_BATMON_OK 0x11, 0x20, 5
176#define SR_RESERVED_11_1 0x11, 0xc0, 6
177#define RG_XOSC_CTRL (0x12)
178#define SR_XTAL_TRIM 0x12, 0x0f, 0
179#define SR_XTAL_MODE 0x12, 0xf0, 4
180#define RG_RX_SYN (0x15)
181#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
182#define SR_RESERVED_15_2 0x15, 0x70, 4
183#define SR_RX_PDT_DIS 0x15, 0x80, 7
184#define RG_XAH_CTRL_1 (0x17)
185#define SR_RESERVED_17_8 0x17, 0x01, 0
186#define SR_AACK_PROM_MODE 0x17, 0x02, 1
187#define SR_AACK_ACK_TIME 0x17, 0x04, 2
188#define SR_RESERVED_17_5 0x17, 0x08, 3
189#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
190#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100191#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000192#define SR_RESERVED_17_1 0x17, 0x80, 7
193#define RG_FTN_CTRL (0x18)
194#define SR_RESERVED_18_2 0x18, 0x7f, 0
195#define SR_FTN_START 0x18, 0x80, 7
196#define RG_PLL_CF (0x1a)
197#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
198#define SR_PLL_CF_START 0x1a, 0x80, 7
199#define RG_PLL_DCU (0x1b)
200#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
201#define SR_RESERVED_1b_2 0x1b, 0x40, 6
202#define SR_PLL_DCU_START 0x1b, 0x80, 7
203#define RG_PART_NUM (0x1c)
204#define SR_PART_NUM 0x1c, 0xff, 0
205#define RG_VERSION_NUM (0x1d)
206#define SR_VERSION_NUM 0x1d, 0xff, 0
207#define RG_MAN_ID_0 (0x1e)
208#define SR_MAN_ID_0 0x1e, 0xff, 0
209#define RG_MAN_ID_1 (0x1f)
210#define SR_MAN_ID_1 0x1f, 0xff, 0
211#define RG_SHORT_ADDR_0 (0x20)
212#define SR_SHORT_ADDR_0 0x20, 0xff, 0
213#define RG_SHORT_ADDR_1 (0x21)
214#define SR_SHORT_ADDR_1 0x21, 0xff, 0
215#define RG_PAN_ID_0 (0x22)
216#define SR_PAN_ID_0 0x22, 0xff, 0
217#define RG_PAN_ID_1 (0x23)
218#define SR_PAN_ID_1 0x23, 0xff, 0
219#define RG_IEEE_ADDR_0 (0x24)
220#define SR_IEEE_ADDR_0 0x24, 0xff, 0
221#define RG_IEEE_ADDR_1 (0x25)
222#define SR_IEEE_ADDR_1 0x25, 0xff, 0
223#define RG_IEEE_ADDR_2 (0x26)
224#define SR_IEEE_ADDR_2 0x26, 0xff, 0
225#define RG_IEEE_ADDR_3 (0x27)
226#define SR_IEEE_ADDR_3 0x27, 0xff, 0
227#define RG_IEEE_ADDR_4 (0x28)
228#define SR_IEEE_ADDR_4 0x28, 0xff, 0
229#define RG_IEEE_ADDR_5 (0x29)
230#define SR_IEEE_ADDR_5 0x29, 0xff, 0
231#define RG_IEEE_ADDR_6 (0x2a)
232#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
233#define RG_IEEE_ADDR_7 (0x2b)
234#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
235#define RG_XAH_CTRL_0 (0x2c)
236#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
237#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
238#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
239#define RG_CSMA_SEED_0 (0x2d)
240#define SR_CSMA_SEED_0 0x2d, 0xff, 0
241#define RG_CSMA_SEED_1 (0x2e)
242#define SR_CSMA_SEED_1 0x2e, 0x07, 0
243#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
244#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
245#define SR_AACK_SET_PD 0x2e, 0x20, 5
246#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
247#define RG_CSMA_BE (0x2f)
248#define SR_MIN_BE 0x2f, 0x0f, 0
249#define SR_MAX_BE 0x2f, 0xf0, 4
250
251#define CMD_REG 0x80
252#define CMD_REG_MASK 0x3f
253#define CMD_WRITE 0x40
254#define CMD_FB 0x20
255
256#define IRQ_BAT_LOW (1 << 7)
257#define IRQ_TRX_UR (1 << 6)
258#define IRQ_AMI (1 << 5)
259#define IRQ_CCA_ED (1 << 4)
260#define IRQ_TRX_END (1 << 3)
261#define IRQ_RX_START (1 << 2)
262#define IRQ_PLL_UNL (1 << 1)
263#define IRQ_PLL_LOCK (1 << 0)
264
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000265#define IRQ_ACTIVE_HIGH 0
266#define IRQ_ACTIVE_LOW 1
267
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000268#define STATE_P_ON 0x00 /* BUSY */
269#define STATE_BUSY_RX 0x01
270#define STATE_BUSY_TX 0x02
271#define STATE_FORCE_TRX_OFF 0x03
272#define STATE_FORCE_TX_ON 0x04 /* IDLE */
273/* 0x05 */ /* INVALID_PARAMETER */
274#define STATE_RX_ON 0x06
275/* 0x07 */ /* SUCCESS */
276#define STATE_TRX_OFF 0x08
277#define STATE_TX_ON 0x09
278/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
279#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500280#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000281#define STATE_BUSY_RX_AACK 0x11
282#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000283#define STATE_RX_AACK_ON 0x16
284#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000285#define STATE_RX_ON_NOCLK 0x1C
286#define STATE_RX_AACK_ON_NOCLK 0x1D
287#define STATE_BUSY_RX_AACK_NOCLK 0x1E
288#define STATE_TRANSITION_IN_PROGRESS 0x1F
289
Alexander Aringf76014f772014-07-03 00:20:44 +0200290#define AT86RF2XX_NUMREGS 0x3F
291
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200292static int
293at86rf230_async_state_change(struct at86rf230_local *lp,
294 struct at86rf230_state_change *ctx,
295 const u8 state, void (*complete)(void *context));
296
Alexander Aringf76014f772014-07-03 00:20:44 +0200297static inline int
298__at86rf230_write(struct at86rf230_local *lp,
299 unsigned int addr, unsigned int data)
300{
301 return regmap_write(lp->regmap, addr, data);
302}
303
304static inline int
305__at86rf230_read(struct at86rf230_local *lp,
306 unsigned int addr, unsigned int *data)
307{
308 return regmap_read(lp->regmap, addr, data);
309}
310
311static inline int
312at86rf230_read_subreg(struct at86rf230_local *lp,
313 unsigned int addr, unsigned int mask,
314 unsigned int shift, unsigned int *data)
315{
316 int rc;
317
318 rc = __at86rf230_read(lp, addr, data);
319 if (rc > 0)
320 *data = (*data & mask) >> shift;
321
322 return rc;
323}
324
325static inline int
326at86rf230_write_subreg(struct at86rf230_local *lp,
327 unsigned int addr, unsigned int mask,
328 unsigned int shift, unsigned int data)
329{
330 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
331}
332
333static bool
334at86rf230_reg_writeable(struct device *dev, unsigned int reg)
335{
336 switch (reg) {
337 case RG_TRX_STATE:
338 case RG_TRX_CTRL_0:
339 case RG_TRX_CTRL_1:
340 case RG_PHY_TX_PWR:
341 case RG_PHY_ED_LEVEL:
342 case RG_PHY_CC_CCA:
343 case RG_CCA_THRES:
344 case RG_RX_CTRL:
345 case RG_SFD_VALUE:
346 case RG_TRX_CTRL_2:
347 case RG_ANT_DIV:
348 case RG_IRQ_MASK:
349 case RG_VREG_CTRL:
350 case RG_BATMON:
351 case RG_XOSC_CTRL:
352 case RG_RX_SYN:
353 case RG_XAH_CTRL_1:
354 case RG_FTN_CTRL:
355 case RG_PLL_CF:
356 case RG_PLL_DCU:
357 case RG_SHORT_ADDR_0:
358 case RG_SHORT_ADDR_1:
359 case RG_PAN_ID_0:
360 case RG_PAN_ID_1:
361 case RG_IEEE_ADDR_0:
362 case RG_IEEE_ADDR_1:
363 case RG_IEEE_ADDR_2:
364 case RG_IEEE_ADDR_3:
365 case RG_IEEE_ADDR_4:
366 case RG_IEEE_ADDR_5:
367 case RG_IEEE_ADDR_6:
368 case RG_IEEE_ADDR_7:
369 case RG_XAH_CTRL_0:
370 case RG_CSMA_SEED_0:
371 case RG_CSMA_SEED_1:
372 case RG_CSMA_BE:
373 return true;
374 default:
375 return false;
376 }
377}
378
379static bool
380at86rf230_reg_readable(struct device *dev, unsigned int reg)
381{
382 bool rc;
383
384 /* all writeable are also readable */
385 rc = at86rf230_reg_writeable(dev, reg);
386 if (rc)
387 return rc;
388
389 /* readonly regs */
390 switch (reg) {
391 case RG_TRX_STATUS:
392 case RG_PHY_RSSI:
393 case RG_IRQ_STATUS:
394 case RG_PART_NUM:
395 case RG_VERSION_NUM:
396 case RG_MAN_ID_1:
397 case RG_MAN_ID_0:
398 return true;
399 default:
400 return false;
401 }
402}
403
404static bool
405at86rf230_reg_volatile(struct device *dev, unsigned int reg)
406{
407 /* can be changed during runtime */
408 switch (reg) {
409 case RG_TRX_STATUS:
410 case RG_TRX_STATE:
411 case RG_PHY_RSSI:
412 case RG_PHY_ED_LEVEL:
413 case RG_IRQ_STATUS:
414 case RG_VREG_CTRL:
415 return true;
416 default:
417 return false;
418 }
419}
420
421static bool
422at86rf230_reg_precious(struct device *dev, unsigned int reg)
423{
424 /* don't clear irq line on read */
425 switch (reg) {
426 case RG_IRQ_STATUS:
427 return true;
428 default:
429 return false;
430 }
431}
432
433static struct regmap_config at86rf230_regmap_spi_config = {
434 .reg_bits = 8,
435 .val_bits = 8,
436 .write_flag_mask = CMD_REG | CMD_WRITE,
437 .read_flag_mask = CMD_REG,
438 .cache_type = REGCACHE_RBTREE,
439 .max_register = AT86RF2XX_NUMREGS,
440 .writeable_reg = at86rf230_reg_writeable,
441 .readable_reg = at86rf230_reg_readable,
442 .volatile_reg = at86rf230_reg_volatile,
443 .precious_reg = at86rf230_reg_precious,
444};
445
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200446static void
447at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000448{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200449 struct at86rf230_state_change *ctx = context;
450 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000451
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200452 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL);
453}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000454
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200455static void
456at86rf230_async_error(struct at86rf230_local *lp,
457 struct at86rf230_state_change *ctx, int rc)
458{
459 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000460
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200461 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
462 at86rf230_async_error_recover);
463}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000464
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200465/* Generic function to get some register value in async mode */
466static int
467at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
468 struct at86rf230_state_change *ctx,
469 void (*complete)(void *context))
470{
471 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000472
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200473 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
474 ctx->trx.len = 2;
475 ctx->msg.complete = complete;
476 return spi_async(lp->spi, &ctx->msg);
477}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000478
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200479static void
480at86rf230_async_state_assert(void *context)
481{
482 struct at86rf230_state_change *ctx = context;
483 struct at86rf230_local *lp = ctx->lp;
484 const u8 *buf = ctx->buf;
485 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000486
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200487 /* Assert state change */
488 if (trx_state != ctx->to_state) {
489 /* Special handling if transceiver state is in
490 * STATE_BUSY_RX_AACK and a SHR was detected.
491 */
492 if (trx_state == STATE_BUSY_RX_AACK) {
493 /* Undocumented race condition. If we send a state
494 * change to STATE_RX_AACK_ON the transceiver could
495 * change his state automatically to STATE_BUSY_RX_AACK
496 * if a SHR was detected. This is not an error, but we
497 * can't assert this.
498 */
499 if (ctx->to_state == STATE_RX_AACK_ON)
500 goto done;
501
502 /* If we change to STATE_TX_ON without forcing and
503 * transceiver state is STATE_BUSY_RX_AACK, we wait
504 * 'tFrame + tPAck' receiving time. In this time the
505 * PDU should be received. If the transceiver is still
506 * in STATE_BUSY_RX_AACK, we run a force state change
507 * to STATE_TX_ON. This is a timeout handling, if the
508 * transceiver stucks in STATE_BUSY_RX_AACK.
509 */
510 if (ctx->to_state == STATE_TX_ON) {
511 at86rf230_async_state_change(lp, ctx,
512 STATE_FORCE_TX_ON,
513 ctx->complete);
514 return;
515 }
516 }
517
518
519 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
520 ctx->from_state, ctx->to_state, trx_state);
521 }
522
523done:
524 if (ctx->complete)
525 ctx->complete(context);
526}
527
528/* Do state change timing delay. */
529static void
530at86rf230_async_state_delay(void *context)
531{
532 struct at86rf230_state_change *ctx = context;
533 struct at86rf230_local *lp = ctx->lp;
534 struct at86rf2xx_chip_data *c = lp->data;
535 bool force = false;
536 int rc;
537
538 /* The force state changes are will show as normal states in the
539 * state status subregister. We change the to_state to the
540 * corresponding one and remember if it was a force change, this
541 * differs if we do a state change from STATE_BUSY_RX_AACK.
542 */
543 switch (ctx->to_state) {
544 case STATE_FORCE_TX_ON:
545 ctx->to_state = STATE_TX_ON;
546 force = true;
547 break;
548 case STATE_FORCE_TRX_OFF:
549 ctx->to_state = STATE_TRX_OFF;
550 force = true;
551 break;
552 default:
553 break;
554 }
555
556 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200557 case STATE_TRX_OFF:
558 switch (ctx->to_state) {
559 case STATE_RX_AACK_ON:
560 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
561 goto change;
562 case STATE_TX_ON:
563 usleep_range(c->t_off_to_tx_on,
564 c->t_off_to_tx_on + 10);
565 goto change;
566 default:
567 break;
568 }
569 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200570 case STATE_BUSY_RX_AACK:
571 switch (ctx->to_state) {
572 case STATE_TX_ON:
573 /* Wait for worst case receiving time if we
574 * didn't make a force change from BUSY_RX_AACK
575 * to TX_ON.
576 */
577 if (!force) {
578 usleep_range(c->t_frame + c->t_p_ack,
579 c->t_frame + c->t_p_ack + 1000);
580 goto change;
581 }
582 break;
583 default:
584 break;
585 }
586 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200587 /* Default value, means RESET state */
588 case STATE_P_ON:
589 switch (ctx->to_state) {
590 case STATE_TRX_OFF:
591 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
592 goto change;
593 default:
594 break;
595 }
596 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200597 default:
598 break;
599 }
600
601 /* Default delay is 1us in the most cases */
602 udelay(1);
603
604change:
605 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
606 at86rf230_async_state_assert);
607 if (rc)
608 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
609}
610
611static void
612at86rf230_async_state_change_start(void *context)
613{
614 struct at86rf230_state_change *ctx = context;
615 struct at86rf230_local *lp = ctx->lp;
616 u8 *buf = ctx->buf;
617 const u8 trx_state = buf[1] & 0x1f;
618 int rc;
619
620 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
621 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
622 udelay(1);
623 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
624 at86rf230_async_state_change_start);
625 if (rc)
626 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
627 return;
628 }
629
630 /* Check if we already are in the state which we change in */
631 if (trx_state == ctx->to_state) {
632 if (ctx->complete)
633 ctx->complete(context);
634 return;
635 }
636
637 /* Set current state to the context of state change */
638 ctx->from_state = trx_state;
639
640 /* Going into the next step for a state change which do a timing
641 * relevant delay.
642 */
643 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
644 buf[1] = ctx->to_state;
645 ctx->trx.len = 2;
646 ctx->msg.complete = at86rf230_async_state_delay;
647 rc = spi_async(lp->spi, &ctx->msg);
648 if (rc)
649 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000650}
651
652static int
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200653at86rf230_async_state_change(struct at86rf230_local *lp,
654 struct at86rf230_state_change *ctx,
655 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000656{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200657 /* Initialization for the state change context */
658 ctx->to_state = state;
659 ctx->complete = complete;
660 return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
661 at86rf230_async_state_change_start);
662}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000663
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200664static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200665at86rf230_sync_state_change_complete(void *context)
666{
667 struct at86rf230_state_change *ctx = context;
668 struct at86rf230_local *lp = ctx->lp;
669
670 complete(&lp->state_complete);
671}
672
673/* This function do a sync framework above the async state change.
674 * Some callbacks of the IEEE 802.15.4 driver interface need to be
675 * handled synchronously.
676 */
677static int
678at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
679{
680 int rc;
681
682 rc = at86rf230_async_state_change(lp, &lp->state, state,
683 at86rf230_sync_state_change_complete);
684 if (rc) {
685 at86rf230_async_error(lp, &lp->state, rc);
686 return rc;
687 }
688
689 rc = wait_for_completion_timeout(&lp->state_complete,
690 msecs_to_jiffies(100));
691 if (!rc)
692 return -ETIMEDOUT;
693
694 return 0;
695}
696
697static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200698at86rf230_tx_complete(void *context)
699{
700 struct at86rf230_state_change *ctx = context;
701 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000702
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200703 complete(&lp->tx_complete);
704}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000705
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200706static void
707at86rf230_tx_on(void *context)
708{
709 struct at86rf230_state_change *ctx = context;
710 struct at86rf230_local *lp = ctx->lp;
711 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000712
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200713 rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
714 at86rf230_tx_complete);
715 if (rc)
716 at86rf230_async_error(lp, ctx, rc);
717}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000718
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200719static void
720at86rf230_tx_trac_error(void *context)
721{
722 struct at86rf230_state_change *ctx = context;
723 struct at86rf230_local *lp = ctx->lp;
724 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000725
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200726 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
727 at86rf230_tx_on);
728 if (rc)
729 at86rf230_async_error(lp, ctx, rc);
730}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000731
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200732static void
733at86rf230_tx_trac_check(void *context)
734{
735 struct at86rf230_state_change *ctx = context;
736 struct at86rf230_local *lp = ctx->lp;
737 const u8 *buf = ctx->buf;
738 const u8 trac = (buf[1] & 0xe0) >> 5;
739 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000740
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200741 /* If trac status is different than zero we need to do a state change
742 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
743 * state to TX_ON.
744 */
745 if (trac) {
746 rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
747 at86rf230_tx_trac_error);
748 if (rc)
749 at86rf230_async_error(lp, ctx, rc);
750 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000751 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000752
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200753 at86rf230_tx_on(context);
754}
755
756
757static void
758at86rf230_tx_trac_status(void *context)
759{
760 struct at86rf230_state_change *ctx = context;
761 struct at86rf230_local *lp = ctx->lp;
762 int rc;
763
764 rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
765 at86rf230_tx_trac_check);
766 if (rc)
767 at86rf230_async_error(lp, ctx, rc);
768}
769
770static void
771at86rf230_rx(struct at86rf230_local *lp,
772 const u8 *data, u8 len)
773{
774 u8 lqi;
775 struct sk_buff *skb;
776 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
777
778 if (len < 2)
779 return;
780
781 /* read full frame buffer and invalid lqi value to lowest
782 * indicator if frame was is in a corrupted state.
783 */
784 if (len > IEEE802154_MTU) {
785 lqi = 0;
786 len = IEEE802154_MTU;
787 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
788 } else {
789 lqi = data[len];
790 }
791
792 memcpy(rx_local_buf, data, len);
793 enable_irq(lp->spi->irq);
794
795 skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC);
796 if (!skb) {
797 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
798 return;
799 }
800
801 memcpy(skb_put(skb, len), rx_local_buf, len);
802
803 /* We do not put CRC into the frame */
804 skb_trim(skb, len - 2);
805
806 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
807}
808
809static void
810at86rf230_rx_read_frame_complete(void *context)
811{
812 struct at86rf230_state_change *ctx = context;
813 struct at86rf230_local *lp = ctx->lp;
814 const u8 *buf = lp->irq.buf;
815 const u8 len = buf[1];
816
817 at86rf230_rx(lp, buf + 2, len);
818}
819
820static int
821at86rf230_rx_read_frame(struct at86rf230_local *lp)
822{
823 u8 *buf = lp->irq.buf;
824
825 buf[0] = CMD_FB;
826 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
827 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
828 return spi_async(lp->spi, &lp->irq.msg);
829}
830
831static void
832at86rf230_rx_trac_check(void *context)
833{
834 struct at86rf230_state_change *ctx = context;
835 struct at86rf230_local *lp = ctx->lp;
836 int rc;
837
838 /* Possible check on trac status here. This could be useful to make
839 * some stats why receive is failed. Not used at the moment, but it's
840 * maybe timing relevant. Datasheet doesn't say anything about this.
841 * The programming guide say do it so.
842 */
843
844 rc = at86rf230_rx_read_frame(lp);
845 if (rc) {
846 enable_irq(lp->spi->irq);
847 at86rf230_async_error(lp, ctx, rc);
848 }
849}
850
851static int
852at86rf230_irq_trx_end(struct at86rf230_local *lp)
853{
854 spin_lock(&lp->lock);
855 if (lp->is_tx) {
856 lp->is_tx = 0;
857 spin_unlock(&lp->lock);
858 enable_irq(lp->spi->irq);
859
860 if (lp->tx_aret)
861 return at86rf230_async_state_change(lp, &lp->irq,
862 STATE_FORCE_TX_ON,
863 at86rf230_tx_trac_status);
864 else
865 return at86rf230_async_state_change(lp, &lp->irq,
866 STATE_RX_AACK_ON,
867 at86rf230_tx_complete);
868 } else {
869 spin_unlock(&lp->lock);
870 return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
871 at86rf230_rx_trac_check);
872 }
873}
874
875static void
876at86rf230_irq_status(void *context)
877{
878 struct at86rf230_state_change *ctx = context;
879 struct at86rf230_local *lp = ctx->lp;
880 const u8 *buf = lp->irq.buf;
881 const u8 irq = buf[1];
882 int rc;
883
884 if (irq & IRQ_TRX_END) {
885 rc = at86rf230_irq_trx_end(lp);
886 if (rc)
887 at86rf230_async_error(lp, ctx, rc);
888 } else {
889 enable_irq(lp->spi->irq);
890 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
891 irq);
892 }
893}
894
895static irqreturn_t at86rf230_isr(int irq, void *data)
896{
897 struct at86rf230_local *lp = data;
898 struct at86rf230_state_change *ctx = &lp->irq;
899 u8 *buf = ctx->buf;
900 int rc;
901
902 disable_irq_nosync(lp->spi->irq);
903
904 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
905 ctx->trx.len = 2;
906 ctx->msg.complete = at86rf230_irq_status;
907 rc = spi_async(lp->spi, &ctx->msg);
908 if (rc) {
909 at86rf230_async_error(lp, ctx, rc);
910 return IRQ_NONE;
911 }
912
913 return IRQ_HANDLED;
914}
915
916static void
917at86rf230_write_frame_complete(void *context)
918{
919 struct at86rf230_state_change *ctx = context;
920 struct at86rf230_local *lp = ctx->lp;
921 u8 *buf = ctx->buf;
922 int rc;
923
924 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
925 buf[1] = STATE_BUSY_TX;
926 ctx->trx.len = 2;
927 ctx->msg.complete = NULL;
928 rc = spi_async(lp->spi, &ctx->msg);
929 if (rc)
930 at86rf230_async_error(lp, ctx, rc);
931}
932
933static void
934at86rf230_write_frame(void *context)
935{
936 struct at86rf230_state_change *ctx = context;
937 struct at86rf230_local *lp = ctx->lp;
938 struct sk_buff *skb = lp->tx_skb;
939 u8 *buf = lp->tx.buf;
940 int rc;
941
942 spin_lock(&lp->lock);
943 lp->is_tx = 1;
944 spin_unlock(&lp->lock);
945
946 buf[0] = CMD_FB | CMD_WRITE;
947 buf[1] = skb->len + 2;
948 memcpy(buf + 2, skb->data, skb->len);
949 lp->tx.trx.len = skb->len + 2;
950 lp->tx.msg.complete = at86rf230_write_frame_complete;
951 rc = spi_async(lp->spi, &lp->tx.msg);
952 if (rc)
953 at86rf230_async_error(lp, ctx, rc);
954}
955
956static void
957at86rf230_xmit_tx_on(void *context)
958{
959 struct at86rf230_state_change *ctx = context;
960 struct at86rf230_local *lp = ctx->lp;
961 int rc;
962
963 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
964 at86rf230_write_frame);
965 if (rc)
966 at86rf230_async_error(lp, ctx, rc);
967}
968
969static int
970at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
971{
972 struct at86rf230_local *lp = dev->priv;
973 struct at86rf230_state_change *ctx = &lp->tx;
974
975 void (*tx_complete)(void *context) = at86rf230_write_frame;
976 int rc;
977
978 lp->tx_skb = skb;
979
980 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
981 * are in STATE_TX_ON. The pfad differs here, so we change
982 * the complete handler.
983 */
984 if (lp->tx_aret)
985 tx_complete = at86rf230_xmit_tx_on;
986
987 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
988 tx_complete);
989 if (rc) {
990 at86rf230_async_error(lp, ctx, rc);
991 return rc;
992 }
993 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
994 msecs_to_jiffies(lp->data->t_tx_timeout));
995 if (!rc) {
996 at86rf230_async_error(lp, ctx, rc);
997 return -ETIMEDOUT;
998 }
999
1000 /* Interfame spacing time, which is phy depend.
1001 * TODO
1002 * Move this handling in MAC 802.15.4 layer.
1003 * This is currently a workaround to avoid fragmenation issues.
1004 */
1005 if (skb->len > 18)
1006 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
1007 else
1008 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
1009
1010 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001011}
1012
1013static int
1014at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
1015{
1016 might_sleep();
1017 BUG_ON(!level);
1018 *level = 0xbe;
1019 return 0;
1020}
1021
1022static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001023at86rf230_start(struct ieee802154_dev *dev)
1024{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001025 return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001026}
1027
1028static void
1029at86rf230_stop(struct ieee802154_dev *dev)
1030{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001031 at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001032}
1033
1034static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001035at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001036{
1037 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1038}
1039
1040static int
1041at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1042{
1043 int rc;
1044
1045 if (channel == 0)
1046 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1047 else
1048 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1049 if (rc < 0)
1050 return rc;
1051
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001052 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001053 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001054 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001055 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001056 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001057 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001058 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001059 if (rc < 0)
1060 return rc;
1061
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001062 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1063}
1064
1065static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001066at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1067{
1068 struct at86rf230_local *lp = dev->priv;
1069 int rc;
1070
1071 might_sleep();
1072
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001073 if (page < 0 || page > 31 ||
1074 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001075 WARN_ON(1);
1076 return -EINVAL;
1077 }
1078
Alexander Aringa53d1f72014-07-03 00:20:46 +02001079 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001080 if (rc < 0)
1081 return rc;
1082
Alexander Aring984e0c62014-07-03 00:20:53 +02001083 /* Wait for PLL */
1084 usleep_range(lp->data->t_channel_switch,
1085 lp->data->t_channel_switch + 10);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001086 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001087 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001088
1089 return 0;
1090}
1091
1092static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001093at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1094 struct ieee802154_hw_addr_filt *filt,
1095 unsigned long changed)
1096{
1097 struct at86rf230_local *lp = dev->priv;
1098
1099 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001100 u16 addr = le16_to_cpu(filt->short_addr);
1101
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001102 dev_vdbg(&lp->spi->dev,
1103 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001104 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1105 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001106 }
1107
1108 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001109 u16 pan = le16_to_cpu(filt->pan_id);
1110
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001111 dev_vdbg(&lp->spi->dev,
1112 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001113 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1114 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001115 }
1116
1117 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001118 u8 i, addr[8];
1119
1120 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001121 dev_vdbg(&lp->spi->dev,
1122 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001123 for (i = 0; i < 8; i++)
1124 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001125 }
1126
1127 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
1128 dev_vdbg(&lp->spi->dev,
1129 "at86rf230_set_hw_addr_filt called for panc change\n");
1130 if (filt->pan_coord)
1131 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1132 else
1133 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1134 }
1135
1136 return 0;
1137}
1138
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001139static int
Alexander Aring640985e2014-07-03 00:20:43 +02001140at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001141{
1142 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001143
1144 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1145 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1146 * 0dB.
1147 * thus, supported values for db range from -26 to 5, for 31dB of
1148 * reduction to 0dB of reduction.
1149 */
1150 if (db > 5 || db < -26)
1151 return -EINVAL;
1152
1153 db = -(db - 5);
1154
Jean Sacren677676c2014-03-01 15:54:36 -07001155 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001156}
1157
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001158static int
Alexander Aring640985e2014-07-03 00:20:43 +02001159at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001160{
1161 struct at86rf230_local *lp = dev->priv;
1162
1163 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1164}
1165
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001166static int
Alexander Aring640985e2014-07-03 00:20:43 +02001167at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001168{
1169 struct at86rf230_local *lp = dev->priv;
1170
1171 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1172}
1173
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001174static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001175at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1176{
1177 return (level - lp->data->rssi_base_val) * 100 / 207;
1178}
1179
1180static int
1181at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1182{
1183 return (level - lp->data->rssi_base_val) / 2;
1184}
1185
1186static int
Alexander Aring640985e2014-07-03 00:20:43 +02001187at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001188{
1189 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001190
Alexander Aringa53d1f72014-07-03 00:20:46 +02001191 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001192 return -EINVAL;
1193
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001194 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1195 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001196}
1197
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001198static int
Alexander Aring640985e2014-07-03 00:20:43 +02001199at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001200 u8 retries)
1201{
1202 struct at86rf230_local *lp = dev->priv;
1203 int rc;
1204
1205 if (min_be > max_be || max_be > 8 || retries > 5)
1206 return -EINVAL;
1207
1208 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1209 if (rc)
1210 return rc;
1211
1212 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1213 if (rc)
1214 return rc;
1215
Alexander Aring39d7f322014-04-05 13:49:26 +02001216 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001217}
1218
1219static int
Alexander Aring640985e2014-07-03 00:20:43 +02001220at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001221{
1222 struct at86rf230_local *lp = dev->priv;
1223 int rc = 0;
1224
1225 if (retries < -1 || retries > 15)
1226 return -EINVAL;
1227
1228 lp->tx_aret = retries >= 0;
1229
1230 if (retries >= 0)
1231 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1232
1233 return rc;
1234}
1235
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001236static struct ieee802154_ops at86rf230_ops = {
1237 .owner = THIS_MODULE,
1238 .xmit = at86rf230_xmit,
1239 .ed = at86rf230_ed,
1240 .set_channel = at86rf230_channel,
1241 .start = at86rf230_start,
1242 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001243 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001244 .set_txpower = at86rf230_set_txpower,
1245 .set_lbt = at86rf230_set_lbt,
1246 .set_cca_mode = at86rf230_set_cca_mode,
1247 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1248 .set_csma_params = at86rf230_set_csma_params,
1249 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001250};
1251
Alexander Aringa53d1f72014-07-03 00:20:46 +02001252static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring984e0c62014-07-03 00:20:53 +02001253 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001254 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001255 .t_off_to_aack = 80,
1256 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001257 .t_frame = 4096,
1258 .t_p_ack = 545,
1259 .t_sifs = 192,
1260 .t_lifs = 480,
1261 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001262 .rssi_base_val = -91,
1263 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001264 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001265};
1266
1267static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring984e0c62014-07-03 00:20:53 +02001268 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001269 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001270 .t_off_to_aack = 110,
1271 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001272 .t_frame = 4096,
1273 .t_p_ack = 545,
1274 .t_sifs = 192,
1275 .t_lifs = 480,
1276 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001277 .rssi_base_val = -91,
1278 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001279 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001280};
1281
1282static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring984e0c62014-07-03 00:20:53 +02001283 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001284 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001285 .t_off_to_aack = 200,
1286 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001287 .t_frame = 4096,
1288 .t_p_ack = 545,
1289 .t_sifs = 192,
1290 .t_lifs = 480,
1291 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001292 .rssi_base_val = -100,
1293 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001294 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001295};
1296
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001297static int at86rf230_hw_init(struct at86rf230_local *lp)
1298{
Alexander Aring1db05582014-07-03 00:20:50 +02001299 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001300 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001301 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001302
Alexander Aring09e536c2014-07-03 00:20:52 +02001303 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001304 if (rc)
1305 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001306
Alexander Aring4af619a2014-04-24 19:09:05 +02001307 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001308 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001309 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001310
Alexander Aring18c65042014-04-24 19:09:18 +02001311 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001312 if (rc)
1313 return rc;
1314
Alexander Aring6bd2b132014-07-03 00:20:49 +02001315 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1316 if (rc)
1317 return rc;
1318
Sascha Herrmann057dad62013-04-14 22:33:29 +00001319 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001320 if (rc)
1321 return rc;
1322
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001323 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1324 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1325 if (rc)
1326 return rc;
1327 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1328 if (rc)
1329 return rc;
1330
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001331 /* CLKM changes are applied immediately */
1332 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1333 if (rc)
1334 return rc;
1335
1336 /* Turn CLKM Off */
1337 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1338 if (rc)
1339 return rc;
1340 /* Wait the next SLEEP cycle */
1341 msleep(100);
1342
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001343 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001344 if (rc)
1345 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001346 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001347 dev_err(&lp->spi->dev, "DVDD error\n");
1348 return -EINVAL;
1349 }
1350
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001351 return 0;
1352}
1353
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001354static struct at86rf230_platform_data *
1355at86rf230_get_pdata(struct spi_device *spi)
1356{
1357 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001358
1359 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1360 return spi->dev.platform_data;
1361
1362 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1363 if (!pdata)
1364 goto done;
1365
1366 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1367 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1368
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001369 spi->dev.platform_data = pdata;
1370done:
1371 return pdata;
1372}
1373
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001374static int
1375at86rf230_detect_device(struct at86rf230_local *lp)
1376{
1377 unsigned int part, version, val;
1378 u16 man_id = 0;
1379 const char *chip;
1380 int rc;
1381
1382 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1383 if (rc)
1384 return rc;
1385 man_id |= val;
1386
1387 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1388 if (rc)
1389 return rc;
1390 man_id |= (val << 8);
1391
1392 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1393 if (rc)
1394 return rc;
1395
1396 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1397 if (rc)
1398 return rc;
1399
1400 if (man_id != 0x001f) {
1401 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1402 man_id >> 8, man_id & 0xFF);
1403 return -EINVAL;
1404 }
1405
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001406 lp->dev->extra_tx_headroom = 0;
1407 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1408 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1409
1410 switch (part) {
1411 case 2:
1412 chip = "at86rf230";
1413 rc = -ENOTSUPP;
1414 break;
1415 case 3:
1416 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001417 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001418 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1419 break;
1420 case 7:
1421 chip = "at86rf212";
1422 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001423 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001424 lp->dev->flags |= IEEE802154_HW_LBT;
1425 lp->dev->phy->channels_supported[0] = 0x00007FF;
1426 lp->dev->phy->channels_supported[2] = 0x00007FF;
1427 } else {
1428 rc = -ENOTSUPP;
1429 }
1430 break;
1431 case 11:
1432 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001433 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001434 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1435 break;
1436 default:
1437 chip = "unkown";
1438 rc = -ENOTSUPP;
1439 break;
1440 }
1441
1442 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1443
1444 return rc;
1445}
1446
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001447static void
1448at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1449{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001450 lp->state.lp = lp;
1451 spi_message_init(&lp->state.msg);
1452 lp->state.msg.context = &lp->state;
1453 lp->state.trx.tx_buf = lp->state.buf;
1454 lp->state.trx.rx_buf = lp->state.buf;
1455 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1456
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001457 lp->irq.lp = lp;
1458 spi_message_init(&lp->irq.msg);
1459 lp->irq.msg.context = &lp->irq;
1460 lp->irq.trx.tx_buf = lp->irq.buf;
1461 lp->irq.trx.rx_buf = lp->irq.buf;
1462 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1463
1464 lp->tx.lp = lp;
1465 spi_message_init(&lp->tx.msg);
1466 lp->tx.msg.context = &lp->tx;
1467 lp->tx.trx.tx_buf = lp->tx.buf;
1468 lp->tx.trx.rx_buf = lp->tx.buf;
1469 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1470}
1471
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001472static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001473{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001474 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001475 struct ieee802154_dev *dev;
1476 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001477 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001478 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001479
1480 if (!spi->irq) {
1481 dev_err(&spi->dev, "no IRQ specified\n");
1482 return -EINVAL;
1483 }
1484
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001485 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001486 if (!pdata) {
1487 dev_err(&spi->dev, "no platform_data\n");
1488 return -EINVAL;
1489 }
1490
Alexander Aring3fa27572014-03-15 09:29:06 +01001491 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001492 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1493 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001494 if (rc)
1495 return rc;
1496 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001497
1498 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001499 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1500 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001501 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001502 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001503 }
1504
1505 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001506 if (gpio_is_valid(pdata->rstn)) {
1507 udelay(1);
1508 gpio_set_value(pdata->rstn, 0);
1509 udelay(1);
1510 gpio_set_value(pdata->rstn, 1);
1511 usleep_range(120, 240);
1512 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001513
Alexander Aring640985e2014-07-03 00:20:43 +02001514 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001515 if (!dev)
1516 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001517
1518 lp = dev->priv;
1519 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001520 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001521 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001522
Alexander Aringf76014f772014-07-03 00:20:44 +02001523 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1524 if (IS_ERR(lp->regmap)) {
1525 rc = PTR_ERR(lp->regmap);
1526 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1527 rc);
1528 goto free_dev;
1529 }
1530
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001531 at86rf230_setup_spi_messages(lp);
1532
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001533 rc = at86rf230_detect_device(lp);
1534 if (rc < 0)
1535 goto free_dev;
1536
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001537 spin_lock_init(&lp->lock);
1538 init_completion(&lp->tx_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001539 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001540
1541 spi_set_drvdata(spi, lp);
1542
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001543 rc = at86rf230_hw_init(lp);
1544 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001545 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001546
Alexander Aring19626942014-04-24 19:09:15 +02001547 /* Read irq status register to reset irq line */
1548 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001549 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001550 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001551
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001552 irq_type = irq_get_trigger_type(spi->irq);
1553 if (!irq_type)
1554 irq_type = IRQF_TRIGGER_RISING;
1555
1556 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1557 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001558 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001559 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001560
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001561 rc = ieee802154_register_device(lp->dev);
1562 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001563 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001564
1565 return rc;
1566
Alexander Aring640985e2014-07-03 00:20:43 +02001567free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001568 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001569
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001570 return rc;
1571}
1572
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001573static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001574{
1575 struct at86rf230_local *lp = spi_get_drvdata(spi);
1576
Alexander Aring17e84a92014-03-31 03:26:51 +02001577 /* mask all at86rf230 irq's */
1578 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001579 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001580 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001581 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001582
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001583 return 0;
1584}
1585
Alexander Aring1086b4f2014-04-24 19:09:11 +02001586static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001587 { .compatible = "atmel,at86rf230", },
1588 { .compatible = "atmel,at86rf231", },
1589 { .compatible = "atmel,at86rf233", },
1590 { .compatible = "atmel,at86rf212", },
1591 { },
1592};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001593MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001594
Alexander Aring90b15522014-04-24 19:09:12 +02001595static const struct spi_device_id at86rf230_device_id[] = {
1596 { .name = "at86rf230", },
1597 { .name = "at86rf231", },
1598 { .name = "at86rf233", },
1599 { .name = "at86rf212", },
1600 { },
1601};
1602MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1603
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001604static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001605 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001606 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001607 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001608 .name = "at86rf230",
1609 .owner = THIS_MODULE,
1610 },
1611 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001612 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001613};
1614
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001615module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001616
1617MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1618MODULE_LICENSE("GPL v2");