blob: 79ec843416f7fbc0b74e6a7224c235813f396c37 [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 Aring7a4ef912014-07-03 00:20:54 +020045 u16 t_sleep_cycle;
Alexander Aring984e0c62014-07-03 00:20:53 +020046 u16 t_channel_switch;
Alexander Aring09e536c2014-07-03 00:20:52 +020047 u16 t_reset_to_off;
Alexander Aring2e0571c2014-07-03 00:20:51 +020048 u16 t_off_to_aack;
49 u16 t_off_to_tx_on;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020050 u16 t_frame;
51 u16 t_p_ack;
52 /* short interframe spacing time */
53 u16 t_sifs;
54 /* long interframe spacing time */
55 u16 t_lifs;
56 /* completion timeout for tx in msecs */
57 u16 t_tx_timeout;
Alexander Aringa53d1f72014-07-03 00:20:46 +020058 int rssi_base_val;
59
60 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020061 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020062};
63
Alexander Aring1d15d6b2014-07-03 00:20:48 +020064#define AT86RF2XX_MAX_BUF (127 + 3)
65
66struct at86rf230_state_change {
67 struct at86rf230_local *lp;
68
69 struct spi_message msg;
70 struct spi_transfer trx;
71 u8 buf[AT86RF2XX_MAX_BUF];
72
73 void (*complete)(void *context);
74 u8 from_state;
75 u8 to_state;
76};
77
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000078struct at86rf230_local {
79 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000080
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000081 struct ieee802154_dev *dev;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020082 struct at86rf2xx_chip_data *data;
Alexander Aringf76014f772014-07-03 00:20:44 +020083 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000084
Alexander Aring2e0571c2014-07-03 00:20:51 +020085 struct completion state_complete;
86 struct at86rf230_state_change state;
87
Alexander Aring1d15d6b2014-07-03 00:20:48 +020088 struct at86rf230_state_change irq;
Alexander Aringa53d1f72014-07-03 00:20:46 +020089
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010090 bool tx_aret;
Alexander Aring1d15d6b2014-07-03 00:20:48 +020091 bool is_tx;
92 /* spinlock for is_tx protection */
93 spinlock_t lock;
94 struct completion tx_complete;
95 struct sk_buff *tx_skb;
96 struct at86rf230_state_change tx;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000097};
98
99#define RG_TRX_STATUS (0x01)
100#define SR_TRX_STATUS 0x01, 0x1f, 0
101#define SR_RESERVED_01_3 0x01, 0x20, 5
102#define SR_CCA_STATUS 0x01, 0x40, 6
103#define SR_CCA_DONE 0x01, 0x80, 7
104#define RG_TRX_STATE (0x02)
105#define SR_TRX_CMD 0x02, 0x1f, 0
106#define SR_TRAC_STATUS 0x02, 0xe0, 5
107#define RG_TRX_CTRL_0 (0x03)
108#define SR_CLKM_CTRL 0x03, 0x07, 0
109#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
110#define SR_PAD_IO_CLKM 0x03, 0x30, 4
111#define SR_PAD_IO 0x03, 0xc0, 6
112#define RG_TRX_CTRL_1 (0x04)
113#define SR_IRQ_POLARITY 0x04, 0x01, 0
114#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
115#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
116#define SR_RX_BL_CTRL 0x04, 0x10, 4
117#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
118#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
119#define SR_PA_EXT_EN 0x04, 0x80, 7
120#define RG_PHY_TX_PWR (0x05)
121#define SR_TX_PWR 0x05, 0x0f, 0
122#define SR_PA_LT 0x05, 0x30, 4
123#define SR_PA_BUF_LT 0x05, 0xc0, 6
124#define RG_PHY_RSSI (0x06)
125#define SR_RSSI 0x06, 0x1f, 0
126#define SR_RND_VALUE 0x06, 0x60, 5
127#define SR_RX_CRC_VALID 0x06, 0x80, 7
128#define RG_PHY_ED_LEVEL (0x07)
129#define SR_ED_LEVEL 0x07, 0xff, 0
130#define RG_PHY_CC_CCA (0x08)
131#define SR_CHANNEL 0x08, 0x1f, 0
132#define SR_CCA_MODE 0x08, 0x60, 5
133#define SR_CCA_REQUEST 0x08, 0x80, 7
134#define RG_CCA_THRES (0x09)
135#define SR_CCA_ED_THRES 0x09, 0x0f, 0
136#define SR_RESERVED_09_1 0x09, 0xf0, 4
137#define RG_RX_CTRL (0x0a)
138#define SR_PDT_THRES 0x0a, 0x0f, 0
139#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
140#define RG_SFD_VALUE (0x0b)
141#define SR_SFD_VALUE 0x0b, 0xff, 0
142#define RG_TRX_CTRL_2 (0x0c)
143#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100144#define SR_SUB_MODE 0x0c, 0x04, 2
145#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100146#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
147#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000148#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
149#define RG_ANT_DIV (0x0d)
150#define SR_ANT_CTRL 0x0d, 0x03, 0
151#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
152#define SR_ANT_DIV_EN 0x0d, 0x08, 3
153#define SR_RESERVED_0d_2 0x0d, 0x70, 4
154#define SR_ANT_SEL 0x0d, 0x80, 7
155#define RG_IRQ_MASK (0x0e)
156#define SR_IRQ_MASK 0x0e, 0xff, 0
157#define RG_IRQ_STATUS (0x0f)
158#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
159#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
160#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
161#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
162#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
163#define SR_IRQ_5_AMI 0x0f, 0x20, 5
164#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
165#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
166#define RG_VREG_CTRL (0x10)
167#define SR_RESERVED_10_6 0x10, 0x03, 0
168#define SR_DVDD_OK 0x10, 0x04, 2
169#define SR_DVREG_EXT 0x10, 0x08, 3
170#define SR_RESERVED_10_3 0x10, 0x30, 4
171#define SR_AVDD_OK 0x10, 0x40, 6
172#define SR_AVREG_EXT 0x10, 0x80, 7
173#define RG_BATMON (0x11)
174#define SR_BATMON_VTH 0x11, 0x0f, 0
175#define SR_BATMON_HR 0x11, 0x10, 4
176#define SR_BATMON_OK 0x11, 0x20, 5
177#define SR_RESERVED_11_1 0x11, 0xc0, 6
178#define RG_XOSC_CTRL (0x12)
179#define SR_XTAL_TRIM 0x12, 0x0f, 0
180#define SR_XTAL_MODE 0x12, 0xf0, 4
181#define RG_RX_SYN (0x15)
182#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
183#define SR_RESERVED_15_2 0x15, 0x70, 4
184#define SR_RX_PDT_DIS 0x15, 0x80, 7
185#define RG_XAH_CTRL_1 (0x17)
186#define SR_RESERVED_17_8 0x17, 0x01, 0
187#define SR_AACK_PROM_MODE 0x17, 0x02, 1
188#define SR_AACK_ACK_TIME 0x17, 0x04, 2
189#define SR_RESERVED_17_5 0x17, 0x08, 3
190#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
191#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100192#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000193#define SR_RESERVED_17_1 0x17, 0x80, 7
194#define RG_FTN_CTRL (0x18)
195#define SR_RESERVED_18_2 0x18, 0x7f, 0
196#define SR_FTN_START 0x18, 0x80, 7
197#define RG_PLL_CF (0x1a)
198#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
199#define SR_PLL_CF_START 0x1a, 0x80, 7
200#define RG_PLL_DCU (0x1b)
201#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
202#define SR_RESERVED_1b_2 0x1b, 0x40, 6
203#define SR_PLL_DCU_START 0x1b, 0x80, 7
204#define RG_PART_NUM (0x1c)
205#define SR_PART_NUM 0x1c, 0xff, 0
206#define RG_VERSION_NUM (0x1d)
207#define SR_VERSION_NUM 0x1d, 0xff, 0
208#define RG_MAN_ID_0 (0x1e)
209#define SR_MAN_ID_0 0x1e, 0xff, 0
210#define RG_MAN_ID_1 (0x1f)
211#define SR_MAN_ID_1 0x1f, 0xff, 0
212#define RG_SHORT_ADDR_0 (0x20)
213#define SR_SHORT_ADDR_0 0x20, 0xff, 0
214#define RG_SHORT_ADDR_1 (0x21)
215#define SR_SHORT_ADDR_1 0x21, 0xff, 0
216#define RG_PAN_ID_0 (0x22)
217#define SR_PAN_ID_0 0x22, 0xff, 0
218#define RG_PAN_ID_1 (0x23)
219#define SR_PAN_ID_1 0x23, 0xff, 0
220#define RG_IEEE_ADDR_0 (0x24)
221#define SR_IEEE_ADDR_0 0x24, 0xff, 0
222#define RG_IEEE_ADDR_1 (0x25)
223#define SR_IEEE_ADDR_1 0x25, 0xff, 0
224#define RG_IEEE_ADDR_2 (0x26)
225#define SR_IEEE_ADDR_2 0x26, 0xff, 0
226#define RG_IEEE_ADDR_3 (0x27)
227#define SR_IEEE_ADDR_3 0x27, 0xff, 0
228#define RG_IEEE_ADDR_4 (0x28)
229#define SR_IEEE_ADDR_4 0x28, 0xff, 0
230#define RG_IEEE_ADDR_5 (0x29)
231#define SR_IEEE_ADDR_5 0x29, 0xff, 0
232#define RG_IEEE_ADDR_6 (0x2a)
233#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
234#define RG_IEEE_ADDR_7 (0x2b)
235#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
236#define RG_XAH_CTRL_0 (0x2c)
237#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
238#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
239#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
240#define RG_CSMA_SEED_0 (0x2d)
241#define SR_CSMA_SEED_0 0x2d, 0xff, 0
242#define RG_CSMA_SEED_1 (0x2e)
243#define SR_CSMA_SEED_1 0x2e, 0x07, 0
244#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
245#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
246#define SR_AACK_SET_PD 0x2e, 0x20, 5
247#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
248#define RG_CSMA_BE (0x2f)
249#define SR_MIN_BE 0x2f, 0x0f, 0
250#define SR_MAX_BE 0x2f, 0xf0, 4
251
252#define CMD_REG 0x80
253#define CMD_REG_MASK 0x3f
254#define CMD_WRITE 0x40
255#define CMD_FB 0x20
256
257#define IRQ_BAT_LOW (1 << 7)
258#define IRQ_TRX_UR (1 << 6)
259#define IRQ_AMI (1 << 5)
260#define IRQ_CCA_ED (1 << 4)
261#define IRQ_TRX_END (1 << 3)
262#define IRQ_RX_START (1 << 2)
263#define IRQ_PLL_UNL (1 << 1)
264#define IRQ_PLL_LOCK (1 << 0)
265
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000266#define IRQ_ACTIVE_HIGH 0
267#define IRQ_ACTIVE_LOW 1
268
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000269#define STATE_P_ON 0x00 /* BUSY */
270#define STATE_BUSY_RX 0x01
271#define STATE_BUSY_TX 0x02
272#define STATE_FORCE_TRX_OFF 0x03
273#define STATE_FORCE_TX_ON 0x04 /* IDLE */
274/* 0x05 */ /* INVALID_PARAMETER */
275#define STATE_RX_ON 0x06
276/* 0x07 */ /* SUCCESS */
277#define STATE_TRX_OFF 0x08
278#define STATE_TX_ON 0x09
279/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
280#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500281#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000282#define STATE_BUSY_RX_AACK 0x11
283#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000284#define STATE_RX_AACK_ON 0x16
285#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000286#define STATE_RX_ON_NOCLK 0x1C
287#define STATE_RX_AACK_ON_NOCLK 0x1D
288#define STATE_BUSY_RX_AACK_NOCLK 0x1E
289#define STATE_TRANSITION_IN_PROGRESS 0x1F
290
Alexander Aringf76014f772014-07-03 00:20:44 +0200291#define AT86RF2XX_NUMREGS 0x3F
292
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200293static int
294at86rf230_async_state_change(struct at86rf230_local *lp,
295 struct at86rf230_state_change *ctx,
296 const u8 state, void (*complete)(void *context));
297
Alexander Aringf76014f772014-07-03 00:20:44 +0200298static inline int
299__at86rf230_write(struct at86rf230_local *lp,
300 unsigned int addr, unsigned int data)
301{
302 return regmap_write(lp->regmap, addr, data);
303}
304
305static inline int
306__at86rf230_read(struct at86rf230_local *lp,
307 unsigned int addr, unsigned int *data)
308{
309 return regmap_read(lp->regmap, addr, data);
310}
311
312static inline int
313at86rf230_read_subreg(struct at86rf230_local *lp,
314 unsigned int addr, unsigned int mask,
315 unsigned int shift, unsigned int *data)
316{
317 int rc;
318
319 rc = __at86rf230_read(lp, addr, data);
320 if (rc > 0)
321 *data = (*data & mask) >> shift;
322
323 return rc;
324}
325
326static inline int
327at86rf230_write_subreg(struct at86rf230_local *lp,
328 unsigned int addr, unsigned int mask,
329 unsigned int shift, unsigned int data)
330{
331 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
332}
333
334static bool
335at86rf230_reg_writeable(struct device *dev, unsigned int reg)
336{
337 switch (reg) {
338 case RG_TRX_STATE:
339 case RG_TRX_CTRL_0:
340 case RG_TRX_CTRL_1:
341 case RG_PHY_TX_PWR:
342 case RG_PHY_ED_LEVEL:
343 case RG_PHY_CC_CCA:
344 case RG_CCA_THRES:
345 case RG_RX_CTRL:
346 case RG_SFD_VALUE:
347 case RG_TRX_CTRL_2:
348 case RG_ANT_DIV:
349 case RG_IRQ_MASK:
350 case RG_VREG_CTRL:
351 case RG_BATMON:
352 case RG_XOSC_CTRL:
353 case RG_RX_SYN:
354 case RG_XAH_CTRL_1:
355 case RG_FTN_CTRL:
356 case RG_PLL_CF:
357 case RG_PLL_DCU:
358 case RG_SHORT_ADDR_0:
359 case RG_SHORT_ADDR_1:
360 case RG_PAN_ID_0:
361 case RG_PAN_ID_1:
362 case RG_IEEE_ADDR_0:
363 case RG_IEEE_ADDR_1:
364 case RG_IEEE_ADDR_2:
365 case RG_IEEE_ADDR_3:
366 case RG_IEEE_ADDR_4:
367 case RG_IEEE_ADDR_5:
368 case RG_IEEE_ADDR_6:
369 case RG_IEEE_ADDR_7:
370 case RG_XAH_CTRL_0:
371 case RG_CSMA_SEED_0:
372 case RG_CSMA_SEED_1:
373 case RG_CSMA_BE:
374 return true;
375 default:
376 return false;
377 }
378}
379
380static bool
381at86rf230_reg_readable(struct device *dev, unsigned int reg)
382{
383 bool rc;
384
385 /* all writeable are also readable */
386 rc = at86rf230_reg_writeable(dev, reg);
387 if (rc)
388 return rc;
389
390 /* readonly regs */
391 switch (reg) {
392 case RG_TRX_STATUS:
393 case RG_PHY_RSSI:
394 case RG_IRQ_STATUS:
395 case RG_PART_NUM:
396 case RG_VERSION_NUM:
397 case RG_MAN_ID_1:
398 case RG_MAN_ID_0:
399 return true;
400 default:
401 return false;
402 }
403}
404
405static bool
406at86rf230_reg_volatile(struct device *dev, unsigned int reg)
407{
408 /* can be changed during runtime */
409 switch (reg) {
410 case RG_TRX_STATUS:
411 case RG_TRX_STATE:
412 case RG_PHY_RSSI:
413 case RG_PHY_ED_LEVEL:
414 case RG_IRQ_STATUS:
415 case RG_VREG_CTRL:
416 return true;
417 default:
418 return false;
419 }
420}
421
422static bool
423at86rf230_reg_precious(struct device *dev, unsigned int reg)
424{
425 /* don't clear irq line on read */
426 switch (reg) {
427 case RG_IRQ_STATUS:
428 return true;
429 default:
430 return false;
431 }
432}
433
434static struct regmap_config at86rf230_regmap_spi_config = {
435 .reg_bits = 8,
436 .val_bits = 8,
437 .write_flag_mask = CMD_REG | CMD_WRITE,
438 .read_flag_mask = CMD_REG,
439 .cache_type = REGCACHE_RBTREE,
440 .max_register = AT86RF2XX_NUMREGS,
441 .writeable_reg = at86rf230_reg_writeable,
442 .readable_reg = at86rf230_reg_readable,
443 .volatile_reg = at86rf230_reg_volatile,
444 .precious_reg = at86rf230_reg_precious,
445};
446
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200447static void
448at86rf230_async_error_recover(void *context)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000449{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200450 struct at86rf230_state_change *ctx = context;
451 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000452
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200453 at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL);
454}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000455
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200456static void
457at86rf230_async_error(struct at86rf230_local *lp,
458 struct at86rf230_state_change *ctx, int rc)
459{
460 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000461
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200462 at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
463 at86rf230_async_error_recover);
464}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000465
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200466/* Generic function to get some register value in async mode */
467static int
468at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
469 struct at86rf230_state_change *ctx,
470 void (*complete)(void *context))
471{
472 u8 *tx_buf = ctx->buf;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000473
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200474 tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
475 ctx->trx.len = 2;
476 ctx->msg.complete = complete;
477 return spi_async(lp->spi, &ctx->msg);
478}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000479
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200480static void
481at86rf230_async_state_assert(void *context)
482{
483 struct at86rf230_state_change *ctx = context;
484 struct at86rf230_local *lp = ctx->lp;
485 const u8 *buf = ctx->buf;
486 const u8 trx_state = buf[1] & 0x1f;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000487
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200488 /* Assert state change */
489 if (trx_state != ctx->to_state) {
490 /* Special handling if transceiver state is in
491 * STATE_BUSY_RX_AACK and a SHR was detected.
492 */
493 if (trx_state == STATE_BUSY_RX_AACK) {
494 /* Undocumented race condition. If we send a state
495 * change to STATE_RX_AACK_ON the transceiver could
496 * change his state automatically to STATE_BUSY_RX_AACK
497 * if a SHR was detected. This is not an error, but we
498 * can't assert this.
499 */
500 if (ctx->to_state == STATE_RX_AACK_ON)
501 goto done;
502
503 /* If we change to STATE_TX_ON without forcing and
504 * transceiver state is STATE_BUSY_RX_AACK, we wait
505 * 'tFrame + tPAck' receiving time. In this time the
506 * PDU should be received. If the transceiver is still
507 * in STATE_BUSY_RX_AACK, we run a force state change
508 * to STATE_TX_ON. This is a timeout handling, if the
509 * transceiver stucks in STATE_BUSY_RX_AACK.
510 */
511 if (ctx->to_state == STATE_TX_ON) {
512 at86rf230_async_state_change(lp, ctx,
513 STATE_FORCE_TX_ON,
514 ctx->complete);
515 return;
516 }
517 }
518
519
520 dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
521 ctx->from_state, ctx->to_state, trx_state);
522 }
523
524done:
525 if (ctx->complete)
526 ctx->complete(context);
527}
528
529/* Do state change timing delay. */
530static void
531at86rf230_async_state_delay(void *context)
532{
533 struct at86rf230_state_change *ctx = context;
534 struct at86rf230_local *lp = ctx->lp;
535 struct at86rf2xx_chip_data *c = lp->data;
536 bool force = false;
537 int rc;
538
539 /* The force state changes are will show as normal states in the
540 * state status subregister. We change the to_state to the
541 * corresponding one and remember if it was a force change, this
542 * differs if we do a state change from STATE_BUSY_RX_AACK.
543 */
544 switch (ctx->to_state) {
545 case STATE_FORCE_TX_ON:
546 ctx->to_state = STATE_TX_ON;
547 force = true;
548 break;
549 case STATE_FORCE_TRX_OFF:
550 ctx->to_state = STATE_TRX_OFF;
551 force = true;
552 break;
553 default:
554 break;
555 }
556
557 switch (ctx->from_state) {
Alexander Aring2e0571c2014-07-03 00:20:51 +0200558 case STATE_TRX_OFF:
559 switch (ctx->to_state) {
560 case STATE_RX_AACK_ON:
561 usleep_range(c->t_off_to_aack, c->t_off_to_aack + 10);
562 goto change;
563 case STATE_TX_ON:
564 usleep_range(c->t_off_to_tx_on,
565 c->t_off_to_tx_on + 10);
566 goto change;
567 default:
568 break;
569 }
570 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200571 case STATE_BUSY_RX_AACK:
572 switch (ctx->to_state) {
573 case STATE_TX_ON:
574 /* Wait for worst case receiving time if we
575 * didn't make a force change from BUSY_RX_AACK
576 * to TX_ON.
577 */
578 if (!force) {
579 usleep_range(c->t_frame + c->t_p_ack,
580 c->t_frame + c->t_p_ack + 1000);
581 goto change;
582 }
583 break;
584 default:
585 break;
586 }
587 break;
Alexander Aring09e536c2014-07-03 00:20:52 +0200588 /* Default value, means RESET state */
589 case STATE_P_ON:
590 switch (ctx->to_state) {
591 case STATE_TRX_OFF:
592 usleep_range(c->t_reset_to_off, c->t_reset_to_off + 10);
593 goto change;
594 default:
595 break;
596 }
597 break;
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200598 default:
599 break;
600 }
601
602 /* Default delay is 1us in the most cases */
603 udelay(1);
604
605change:
606 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
607 at86rf230_async_state_assert);
608 if (rc)
609 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
610}
611
612static void
613at86rf230_async_state_change_start(void *context)
614{
615 struct at86rf230_state_change *ctx = context;
616 struct at86rf230_local *lp = ctx->lp;
617 u8 *buf = ctx->buf;
618 const u8 trx_state = buf[1] & 0x1f;
619 int rc;
620
621 /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
622 if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
623 udelay(1);
624 rc = at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
625 at86rf230_async_state_change_start);
626 if (rc)
627 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
628 return;
629 }
630
631 /* Check if we already are in the state which we change in */
632 if (trx_state == ctx->to_state) {
633 if (ctx->complete)
634 ctx->complete(context);
635 return;
636 }
637
638 /* Set current state to the context of state change */
639 ctx->from_state = trx_state;
640
641 /* Going into the next step for a state change which do a timing
642 * relevant delay.
643 */
644 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
645 buf[1] = ctx->to_state;
646 ctx->trx.len = 2;
647 ctx->msg.complete = at86rf230_async_state_delay;
648 rc = spi_async(lp->spi, &ctx->msg);
649 if (rc)
650 dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000651}
652
653static int
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200654at86rf230_async_state_change(struct at86rf230_local *lp,
655 struct at86rf230_state_change *ctx,
656 const u8 state, void (*complete)(void *context))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000657{
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200658 /* Initialization for the state change context */
659 ctx->to_state = state;
660 ctx->complete = complete;
661 return at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
662 at86rf230_async_state_change_start);
663}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000664
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200665static void
Alexander Aring2e0571c2014-07-03 00:20:51 +0200666at86rf230_sync_state_change_complete(void *context)
667{
668 struct at86rf230_state_change *ctx = context;
669 struct at86rf230_local *lp = ctx->lp;
670
671 complete(&lp->state_complete);
672}
673
674/* This function do a sync framework above the async state change.
675 * Some callbacks of the IEEE 802.15.4 driver interface need to be
676 * handled synchronously.
677 */
678static int
679at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
680{
681 int rc;
682
683 rc = at86rf230_async_state_change(lp, &lp->state, state,
684 at86rf230_sync_state_change_complete);
685 if (rc) {
686 at86rf230_async_error(lp, &lp->state, rc);
687 return rc;
688 }
689
690 rc = wait_for_completion_timeout(&lp->state_complete,
691 msecs_to_jiffies(100));
692 if (!rc)
693 return -ETIMEDOUT;
694
695 return 0;
696}
697
698static void
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200699at86rf230_tx_complete(void *context)
700{
701 struct at86rf230_state_change *ctx = context;
702 struct at86rf230_local *lp = ctx->lp;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000703
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200704 complete(&lp->tx_complete);
705}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000706
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200707static void
708at86rf230_tx_on(void *context)
709{
710 struct at86rf230_state_change *ctx = context;
711 struct at86rf230_local *lp = ctx->lp;
712 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000713
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200714 rc = at86rf230_async_state_change(lp, &lp->irq, STATE_RX_AACK_ON,
715 at86rf230_tx_complete);
716 if (rc)
717 at86rf230_async_error(lp, ctx, rc);
718}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000719
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200720static void
721at86rf230_tx_trac_error(void *context)
722{
723 struct at86rf230_state_change *ctx = context;
724 struct at86rf230_local *lp = ctx->lp;
725 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000726
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200727 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
728 at86rf230_tx_on);
729 if (rc)
730 at86rf230_async_error(lp, ctx, rc);
731}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000732
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200733static void
734at86rf230_tx_trac_check(void *context)
735{
736 struct at86rf230_state_change *ctx = context;
737 struct at86rf230_local *lp = ctx->lp;
738 const u8 *buf = ctx->buf;
739 const u8 trac = (buf[1] & 0xe0) >> 5;
740 int rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000741
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200742 /* If trac status is different than zero we need to do a state change
743 * to STATE_FORCE_TRX_OFF then STATE_TX_ON to recover the transceiver
744 * state to TX_ON.
745 */
746 if (trac) {
747 rc = at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
748 at86rf230_tx_trac_error);
749 if (rc)
750 at86rf230_async_error(lp, ctx, rc);
751 return;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000752 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000753
Alexander Aring1d15d6b2014-07-03 00:20:48 +0200754 at86rf230_tx_on(context);
755}
756
757
758static void
759at86rf230_tx_trac_status(void *context)
760{
761 struct at86rf230_state_change *ctx = context;
762 struct at86rf230_local *lp = ctx->lp;
763 int rc;
764
765 rc = at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
766 at86rf230_tx_trac_check);
767 if (rc)
768 at86rf230_async_error(lp, ctx, rc);
769}
770
771static void
772at86rf230_rx(struct at86rf230_local *lp,
773 const u8 *data, u8 len)
774{
775 u8 lqi;
776 struct sk_buff *skb;
777 u8 rx_local_buf[AT86RF2XX_MAX_BUF];
778
779 if (len < 2)
780 return;
781
782 /* read full frame buffer and invalid lqi value to lowest
783 * indicator if frame was is in a corrupted state.
784 */
785 if (len > IEEE802154_MTU) {
786 lqi = 0;
787 len = IEEE802154_MTU;
788 dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
789 } else {
790 lqi = data[len];
791 }
792
793 memcpy(rx_local_buf, data, len);
794 enable_irq(lp->spi->irq);
795
796 skb = alloc_skb(IEEE802154_MTU, GFP_ATOMIC);
797 if (!skb) {
798 dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
799 return;
800 }
801
802 memcpy(skb_put(skb, len), rx_local_buf, len);
803
804 /* We do not put CRC into the frame */
805 skb_trim(skb, len - 2);
806
807 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
808}
809
810static void
811at86rf230_rx_read_frame_complete(void *context)
812{
813 struct at86rf230_state_change *ctx = context;
814 struct at86rf230_local *lp = ctx->lp;
815 const u8 *buf = lp->irq.buf;
816 const u8 len = buf[1];
817
818 at86rf230_rx(lp, buf + 2, len);
819}
820
821static int
822at86rf230_rx_read_frame(struct at86rf230_local *lp)
823{
824 u8 *buf = lp->irq.buf;
825
826 buf[0] = CMD_FB;
827 lp->irq.trx.len = AT86RF2XX_MAX_BUF;
828 lp->irq.msg.complete = at86rf230_rx_read_frame_complete;
829 return spi_async(lp->spi, &lp->irq.msg);
830}
831
832static void
833at86rf230_rx_trac_check(void *context)
834{
835 struct at86rf230_state_change *ctx = context;
836 struct at86rf230_local *lp = ctx->lp;
837 int rc;
838
839 /* Possible check on trac status here. This could be useful to make
840 * some stats why receive is failed. Not used at the moment, but it's
841 * maybe timing relevant. Datasheet doesn't say anything about this.
842 * The programming guide say do it so.
843 */
844
845 rc = at86rf230_rx_read_frame(lp);
846 if (rc) {
847 enable_irq(lp->spi->irq);
848 at86rf230_async_error(lp, ctx, rc);
849 }
850}
851
852static int
853at86rf230_irq_trx_end(struct at86rf230_local *lp)
854{
855 spin_lock(&lp->lock);
856 if (lp->is_tx) {
857 lp->is_tx = 0;
858 spin_unlock(&lp->lock);
859 enable_irq(lp->spi->irq);
860
861 if (lp->tx_aret)
862 return at86rf230_async_state_change(lp, &lp->irq,
863 STATE_FORCE_TX_ON,
864 at86rf230_tx_trac_status);
865 else
866 return at86rf230_async_state_change(lp, &lp->irq,
867 STATE_RX_AACK_ON,
868 at86rf230_tx_complete);
869 } else {
870 spin_unlock(&lp->lock);
871 return at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
872 at86rf230_rx_trac_check);
873 }
874}
875
876static void
877at86rf230_irq_status(void *context)
878{
879 struct at86rf230_state_change *ctx = context;
880 struct at86rf230_local *lp = ctx->lp;
881 const u8 *buf = lp->irq.buf;
882 const u8 irq = buf[1];
883 int rc;
884
885 if (irq & IRQ_TRX_END) {
886 rc = at86rf230_irq_trx_end(lp);
887 if (rc)
888 at86rf230_async_error(lp, ctx, rc);
889 } else {
890 enable_irq(lp->spi->irq);
891 dev_err(&lp->spi->dev, "not supported irq %02x received\n",
892 irq);
893 }
894}
895
896static irqreturn_t at86rf230_isr(int irq, void *data)
897{
898 struct at86rf230_local *lp = data;
899 struct at86rf230_state_change *ctx = &lp->irq;
900 u8 *buf = ctx->buf;
901 int rc;
902
903 disable_irq_nosync(lp->spi->irq);
904
905 buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
906 ctx->trx.len = 2;
907 ctx->msg.complete = at86rf230_irq_status;
908 rc = spi_async(lp->spi, &ctx->msg);
909 if (rc) {
910 at86rf230_async_error(lp, ctx, rc);
911 return IRQ_NONE;
912 }
913
914 return IRQ_HANDLED;
915}
916
917static void
918at86rf230_write_frame_complete(void *context)
919{
920 struct at86rf230_state_change *ctx = context;
921 struct at86rf230_local *lp = ctx->lp;
922 u8 *buf = ctx->buf;
923 int rc;
924
925 buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
926 buf[1] = STATE_BUSY_TX;
927 ctx->trx.len = 2;
928 ctx->msg.complete = NULL;
929 rc = spi_async(lp->spi, &ctx->msg);
930 if (rc)
931 at86rf230_async_error(lp, ctx, rc);
932}
933
934static void
935at86rf230_write_frame(void *context)
936{
937 struct at86rf230_state_change *ctx = context;
938 struct at86rf230_local *lp = ctx->lp;
939 struct sk_buff *skb = lp->tx_skb;
940 u8 *buf = lp->tx.buf;
941 int rc;
942
943 spin_lock(&lp->lock);
944 lp->is_tx = 1;
945 spin_unlock(&lp->lock);
946
947 buf[0] = CMD_FB | CMD_WRITE;
948 buf[1] = skb->len + 2;
949 memcpy(buf + 2, skb->data, skb->len);
950 lp->tx.trx.len = skb->len + 2;
951 lp->tx.msg.complete = at86rf230_write_frame_complete;
952 rc = spi_async(lp->spi, &lp->tx.msg);
953 if (rc)
954 at86rf230_async_error(lp, ctx, rc);
955}
956
957static void
958at86rf230_xmit_tx_on(void *context)
959{
960 struct at86rf230_state_change *ctx = context;
961 struct at86rf230_local *lp = ctx->lp;
962 int rc;
963
964 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
965 at86rf230_write_frame);
966 if (rc)
967 at86rf230_async_error(lp, ctx, rc);
968}
969
970static int
971at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
972{
973 struct at86rf230_local *lp = dev->priv;
974 struct at86rf230_state_change *ctx = &lp->tx;
975
976 void (*tx_complete)(void *context) = at86rf230_write_frame;
977 int rc;
978
979 lp->tx_skb = skb;
980
981 /* In ARET mode we need to go into STATE_TX_ARET_ON after we
982 * are in STATE_TX_ON. The pfad differs here, so we change
983 * the complete handler.
984 */
985 if (lp->tx_aret)
986 tx_complete = at86rf230_xmit_tx_on;
987
988 rc = at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
989 tx_complete);
990 if (rc) {
991 at86rf230_async_error(lp, ctx, rc);
992 return rc;
993 }
994 rc = wait_for_completion_interruptible_timeout(&lp->tx_complete,
995 msecs_to_jiffies(lp->data->t_tx_timeout));
996 if (!rc) {
997 at86rf230_async_error(lp, ctx, rc);
998 return -ETIMEDOUT;
999 }
1000
1001 /* Interfame spacing time, which is phy depend.
1002 * TODO
1003 * Move this handling in MAC 802.15.4 layer.
1004 * This is currently a workaround to avoid fragmenation issues.
1005 */
1006 if (skb->len > 18)
1007 usleep_range(lp->data->t_lifs, lp->data->t_lifs + 10);
1008 else
1009 usleep_range(lp->data->t_sifs, lp->data->t_sifs + 10);
1010
1011 return 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001012}
1013
1014static int
1015at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
1016{
1017 might_sleep();
1018 BUG_ON(!level);
1019 *level = 0xbe;
1020 return 0;
1021}
1022
1023static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001024at86rf230_start(struct ieee802154_dev *dev)
1025{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001026 return at86rf230_sync_state_change(dev->priv, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001027}
1028
1029static void
1030at86rf230_stop(struct ieee802154_dev *dev)
1031{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001032 at86rf230_sync_state_change(dev->priv, STATE_FORCE_TRX_OFF);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001033}
1034
1035static int
Alexander Aringa53d1f72014-07-03 00:20:46 +02001036at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001037{
1038 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1039}
1040
1041static int
1042at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
1043{
1044 int rc;
1045
1046 if (channel == 0)
1047 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
1048 else
1049 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
1050 if (rc < 0)
1051 return rc;
1052
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001053 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001054 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001055 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001056 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001057 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +02001058 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001059 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001060 if (rc < 0)
1061 return rc;
1062
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001063 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
1064}
1065
1066static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001067at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
1068{
1069 struct at86rf230_local *lp = dev->priv;
1070 int rc;
1071
1072 might_sleep();
1073
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001074 if (page < 0 || page > 31 ||
1075 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001076 WARN_ON(1);
1077 return -EINVAL;
1078 }
1079
Alexander Aringa53d1f72014-07-03 00:20:46 +02001080 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001081 if (rc < 0)
1082 return rc;
1083
Alexander Aring984e0c62014-07-03 00:20:53 +02001084 /* Wait for PLL */
1085 usleep_range(lp->data->t_channel_switch,
1086 lp->data->t_channel_switch + 10);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001087 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001088 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001089
1090 return 0;
1091}
1092
1093static int
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001094at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
1095 struct ieee802154_hw_addr_filt *filt,
1096 unsigned long changed)
1097{
1098 struct at86rf230_local *lp = dev->priv;
1099
1100 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001101 u16 addr = le16_to_cpu(filt->short_addr);
1102
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001103 dev_vdbg(&lp->spi->dev,
1104 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001105 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
1106 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001107 }
1108
1109 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001110 u16 pan = le16_to_cpu(filt->pan_id);
1111
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001112 dev_vdbg(&lp->spi->dev,
1113 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001114 __at86rf230_write(lp, RG_PAN_ID_0, pan);
1115 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001116 }
1117
1118 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001119 u8 i, addr[8];
1120
1121 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001122 dev_vdbg(&lp->spi->dev,
1123 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +01001124 for (i = 0; i < 8; i++)
1125 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001126 }
1127
1128 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
1129 dev_vdbg(&lp->spi->dev,
1130 "at86rf230_set_hw_addr_filt called for panc change\n");
1131 if (filt->pan_coord)
1132 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
1133 else
1134 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
1135 }
1136
1137 return 0;
1138}
1139
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001140static int
Alexander Aring640985e2014-07-03 00:20:43 +02001141at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001142{
1143 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001144
1145 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
1146 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
1147 * 0dB.
1148 * thus, supported values for db range from -26 to 5, for 31dB of
1149 * reduction to 0dB of reduction.
1150 */
1151 if (db > 5 || db < -26)
1152 return -EINVAL;
1153
1154 db = -(db - 5);
1155
Jean Sacren677676c2014-03-01 15:54:36 -07001156 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +01001157}
1158
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001159static int
Alexander Aring640985e2014-07-03 00:20:43 +02001160at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +01001161{
1162 struct at86rf230_local *lp = dev->priv;
1163
1164 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
1165}
1166
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001167static int
Alexander Aring640985e2014-07-03 00:20:43 +02001168at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +01001169{
1170 struct at86rf230_local *lp = dev->priv;
1171
1172 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
1173}
1174
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001175static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001176at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
1177{
1178 return (level - lp->data->rssi_base_val) * 100 / 207;
1179}
1180
1181static int
1182at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
1183{
1184 return (level - lp->data->rssi_base_val) / 2;
1185}
1186
1187static int
Alexander Aring640985e2014-07-03 00:20:43 +02001188at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001189{
1190 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001191
Alexander Aringa53d1f72014-07-03 00:20:46 +02001192 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001193 return -EINVAL;
1194
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001195 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
1196 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +01001197}
1198
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001199static int
Alexander Aring640985e2014-07-03 00:20:43 +02001200at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001201 u8 retries)
1202{
1203 struct at86rf230_local *lp = dev->priv;
1204 int rc;
1205
1206 if (min_be > max_be || max_be > 8 || retries > 5)
1207 return -EINVAL;
1208
1209 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
1210 if (rc)
1211 return rc;
1212
1213 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
1214 if (rc)
1215 return rc;
1216
Alexander Aring39d7f322014-04-05 13:49:26 +02001217 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001218}
1219
1220static int
Alexander Aring640985e2014-07-03 00:20:43 +02001221at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001222{
1223 struct at86rf230_local *lp = dev->priv;
1224 int rc = 0;
1225
1226 if (retries < -1 || retries > 15)
1227 return -EINVAL;
1228
1229 lp->tx_aret = retries >= 0;
1230
1231 if (retries >= 0)
1232 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
1233
1234 return rc;
1235}
1236
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001237static struct ieee802154_ops at86rf230_ops = {
1238 .owner = THIS_MODULE,
1239 .xmit = at86rf230_xmit,
1240 .ed = at86rf230_ed,
1241 .set_channel = at86rf230_channel,
1242 .start = at86rf230_start,
1243 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +00001244 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +02001245 .set_txpower = at86rf230_set_txpower,
1246 .set_lbt = at86rf230_set_lbt,
1247 .set_cca_mode = at86rf230_set_cca_mode,
1248 .set_cca_ed_level = at86rf230_set_cca_ed_level,
1249 .set_csma_params = at86rf230_set_csma_params,
1250 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001251};
1252
Alexander Aringa53d1f72014-07-03 00:20:46 +02001253static struct at86rf2xx_chip_data at86rf233_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001254 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001255 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001256 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001257 .t_off_to_aack = 80,
1258 .t_off_to_tx_on = 80,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001259 .t_frame = 4096,
1260 .t_p_ack = 545,
1261 .t_sifs = 192,
1262 .t_lifs = 480,
1263 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001264 .rssi_base_val = -91,
1265 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001266 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001267};
1268
1269static struct at86rf2xx_chip_data at86rf231_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001270 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001271 .t_channel_switch = 24,
Alexander Aring09e536c2014-07-03 00:20:52 +02001272 .t_reset_to_off = 37,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001273 .t_off_to_aack = 110,
1274 .t_off_to_tx_on = 110,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001275 .t_frame = 4096,
1276 .t_p_ack = 545,
1277 .t_sifs = 192,
1278 .t_lifs = 480,
1279 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001280 .rssi_base_val = -91,
1281 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001282 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001283};
1284
1285static struct at86rf2xx_chip_data at86rf212_data = {
Alexander Aring7a4ef912014-07-03 00:20:54 +02001286 .t_sleep_cycle = 330,
Alexander Aring984e0c62014-07-03 00:20:53 +02001287 .t_channel_switch = 11,
Alexander Aring09e536c2014-07-03 00:20:52 +02001288 .t_reset_to_off = 26,
Alexander Aring2e0571c2014-07-03 00:20:51 +02001289 .t_off_to_aack = 200,
1290 .t_off_to_tx_on = 200,
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001291 .t_frame = 4096,
1292 .t_p_ack = 545,
1293 .t_sifs = 192,
1294 .t_lifs = 480,
1295 .t_tx_timeout = 2000,
Alexander Aringa53d1f72014-07-03 00:20:46 +02001296 .rssi_base_val = -100,
1297 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +02001298 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +02001299};
1300
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001301static int at86rf230_hw_init(struct at86rf230_local *lp)
1302{
Alexander Aring1db05582014-07-03 00:20:50 +02001303 int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
Alexander Aringf76014f772014-07-03 00:20:44 +02001304 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001305 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001306
Alexander Aring09e536c2014-07-03 00:20:52 +02001307 rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +01001308 if (rc)
1309 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001310
Alexander Aring4af619a2014-04-24 19:09:05 +02001311 irq_type = irq_get_trigger_type(lp->spi->irq);
Alexander Aring1db05582014-07-03 00:20:50 +02001312 if (irq_type == IRQ_TYPE_EDGE_FALLING)
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001313 irq_pol = IRQ_ACTIVE_LOW;
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001314
Alexander Aring18c65042014-04-24 19:09:18 +02001315 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001316 if (rc)
1317 return rc;
1318
Alexander Aring6bd2b132014-07-03 00:20:49 +02001319 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
1320 if (rc)
1321 return rc;
1322
Sascha Herrmann057dad62013-04-14 22:33:29 +00001323 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001324 if (rc)
1325 return rc;
1326
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001327 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1328 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1329 if (rc)
1330 return rc;
1331 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1332 if (rc)
1333 return rc;
1334
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001335 /* CLKM changes are applied immediately */
1336 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1337 if (rc)
1338 return rc;
1339
1340 /* Turn CLKM Off */
1341 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1342 if (rc)
1343 return rc;
1344 /* Wait the next SLEEP cycle */
Alexander Aring7a4ef912014-07-03 00:20:54 +02001345 usleep_range(lp->data->t_sleep_cycle,
1346 lp->data->t_sleep_cycle + 100);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001347
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001348 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001349 if (rc)
1350 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001351 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001352 dev_err(&lp->spi->dev, "DVDD error\n");
1353 return -EINVAL;
1354 }
1355
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001356 return 0;
1357}
1358
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001359static struct at86rf230_platform_data *
1360at86rf230_get_pdata(struct spi_device *spi)
1361{
1362 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001363
1364 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1365 return spi->dev.platform_data;
1366
1367 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1368 if (!pdata)
1369 goto done;
1370
1371 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1372 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1373
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001374 spi->dev.platform_data = pdata;
1375done:
1376 return pdata;
1377}
1378
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001379static int
1380at86rf230_detect_device(struct at86rf230_local *lp)
1381{
1382 unsigned int part, version, val;
1383 u16 man_id = 0;
1384 const char *chip;
1385 int rc;
1386
1387 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1388 if (rc)
1389 return rc;
1390 man_id |= val;
1391
1392 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1393 if (rc)
1394 return rc;
1395 man_id |= (val << 8);
1396
1397 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1398 if (rc)
1399 return rc;
1400
1401 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1402 if (rc)
1403 return rc;
1404
1405 if (man_id != 0x001f) {
1406 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1407 man_id >> 8, man_id & 0xFF);
1408 return -EINVAL;
1409 }
1410
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001411 lp->dev->extra_tx_headroom = 0;
1412 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1413 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1414
1415 switch (part) {
1416 case 2:
1417 chip = "at86rf230";
1418 rc = -ENOTSUPP;
1419 break;
1420 case 3:
1421 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001422 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001423 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1424 break;
1425 case 7:
1426 chip = "at86rf212";
1427 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001428 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001429 lp->dev->flags |= IEEE802154_HW_LBT;
1430 lp->dev->phy->channels_supported[0] = 0x00007FF;
1431 lp->dev->phy->channels_supported[2] = 0x00007FF;
1432 } else {
1433 rc = -ENOTSUPP;
1434 }
1435 break;
1436 case 11:
1437 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001438 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001439 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1440 break;
1441 default:
1442 chip = "unkown";
1443 rc = -ENOTSUPP;
1444 break;
1445 }
1446
1447 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1448
1449 return rc;
1450}
1451
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001452static void
1453at86rf230_setup_spi_messages(struct at86rf230_local *lp)
1454{
Alexander Aring2e0571c2014-07-03 00:20:51 +02001455 lp->state.lp = lp;
1456 spi_message_init(&lp->state.msg);
1457 lp->state.msg.context = &lp->state;
1458 lp->state.trx.tx_buf = lp->state.buf;
1459 lp->state.trx.rx_buf = lp->state.buf;
1460 spi_message_add_tail(&lp->state.trx, &lp->state.msg);
1461
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001462 lp->irq.lp = lp;
1463 spi_message_init(&lp->irq.msg);
1464 lp->irq.msg.context = &lp->irq;
1465 lp->irq.trx.tx_buf = lp->irq.buf;
1466 lp->irq.trx.rx_buf = lp->irq.buf;
1467 spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
1468
1469 lp->tx.lp = lp;
1470 spi_message_init(&lp->tx.msg);
1471 lp->tx.msg.context = &lp->tx;
1472 lp->tx.trx.tx_buf = lp->tx.buf;
1473 lp->tx.trx.rx_buf = lp->tx.buf;
1474 spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
1475}
1476
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001477static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001478{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001479 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001480 struct ieee802154_dev *dev;
1481 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001482 unsigned int status;
Alexander Aring4af619a2014-04-24 19:09:05 +02001483 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001484
1485 if (!spi->irq) {
1486 dev_err(&spi->dev, "no IRQ specified\n");
1487 return -EINVAL;
1488 }
1489
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001490 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001491 if (!pdata) {
1492 dev_err(&spi->dev, "no platform_data\n");
1493 return -EINVAL;
1494 }
1495
Alexander Aring3fa27572014-03-15 09:29:06 +01001496 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001497 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1498 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001499 if (rc)
1500 return rc;
1501 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001502
1503 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001504 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1505 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001506 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001507 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001508 }
1509
1510 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001511 if (gpio_is_valid(pdata->rstn)) {
1512 udelay(1);
1513 gpio_set_value(pdata->rstn, 0);
1514 udelay(1);
1515 gpio_set_value(pdata->rstn, 1);
1516 usleep_range(120, 240);
1517 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001518
Alexander Aring640985e2014-07-03 00:20:43 +02001519 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001520 if (!dev)
1521 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001522
1523 lp = dev->priv;
1524 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001525 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001526 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001527
Alexander Aringf76014f772014-07-03 00:20:44 +02001528 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1529 if (IS_ERR(lp->regmap)) {
1530 rc = PTR_ERR(lp->regmap);
1531 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1532 rc);
1533 goto free_dev;
1534 }
1535
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001536 at86rf230_setup_spi_messages(lp);
1537
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001538 rc = at86rf230_detect_device(lp);
1539 if (rc < 0)
1540 goto free_dev;
1541
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001542 spin_lock_init(&lp->lock);
1543 init_completion(&lp->tx_complete);
Alexander Aring2e0571c2014-07-03 00:20:51 +02001544 init_completion(&lp->state_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001545
1546 spi_set_drvdata(spi, lp);
1547
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001548 rc = at86rf230_hw_init(lp);
1549 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 Aring19626942014-04-24 19:09:15 +02001552 /* Read irq status register to reset irq line */
1553 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001554 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001555 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001556
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001557 irq_type = irq_get_trigger_type(spi->irq);
1558 if (!irq_type)
1559 irq_type = IRQF_TRIGGER_RISING;
1560
1561 rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
1562 IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001563 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001564 goto free_dev;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001565
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001566 rc = ieee802154_register_device(lp->dev);
1567 if (rc)
Alexander Aring1d15d6b2014-07-03 00:20:48 +02001568 goto free_dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001569
1570 return rc;
1571
Alexander Aring640985e2014-07-03 00:20:43 +02001572free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001573 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001574
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001575 return rc;
1576}
1577
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001578static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001579{
1580 struct at86rf230_local *lp = spi_get_drvdata(spi);
1581
Alexander Aring17e84a92014-03-31 03:26:51 +02001582 /* mask all at86rf230 irq's */
1583 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001584 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001585 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001586 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001587
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001588 return 0;
1589}
1590
Alexander Aring1086b4f2014-04-24 19:09:11 +02001591static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001592 { .compatible = "atmel,at86rf230", },
1593 { .compatible = "atmel,at86rf231", },
1594 { .compatible = "atmel,at86rf233", },
1595 { .compatible = "atmel,at86rf212", },
1596 { },
1597};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001598MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001599
Alexander Aring90b15522014-04-24 19:09:12 +02001600static const struct spi_device_id at86rf230_device_id[] = {
1601 { .name = "at86rf230", },
1602 { .name = "at86rf231", },
1603 { .name = "at86rf233", },
1604 { .name = "at86rf212", },
1605 { },
1606};
1607MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1608
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001609static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001610 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001611 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001612 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001613 .name = "at86rf230",
1614 .owner = THIS_MODULE,
1615 },
1616 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001617 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001618};
1619
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001620module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001621
1622MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1623MODULE_LICENSE("GPL v2");