blob: 6556fecec5e287d00aa66c7933b088455dc5fa8b [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>
29#include <linux/mutex.h>
30#include <linux/workqueue.h>
31#include <linux/spinlock.h>
32#include <linux/spi/spi.h>
33#include <linux/spi/at86rf230.h>
Alexander Aringf76014f772014-07-03 00:20:44 +020034#include <linux/regmap.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000035#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010036#include <linux/of_gpio.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000037
38#include <net/mac802154.h>
39#include <net/wpan-phy.h>
40
Alexander Aringa53d1f72014-07-03 00:20:46 +020041struct at86rf230_local;
42/* at86rf2xx chip depend data.
43 * All timings are in us.
44 */
45struct at86rf2xx_chip_data {
46 int rssi_base_val;
47
48 int (*set_channel)(struct at86rf230_local *, int, int);
Alexander Aringa7d7eda2014-07-03 00:20:47 +020049 int (*get_desense_steps)(struct at86rf230_local *, s32);
Alexander Aringa53d1f72014-07-03 00:20:46 +020050};
51
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000052struct at86rf230_local {
53 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000054
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000055 u8 buf[2];
56 struct mutex bmux;
57
58 struct work_struct irqwork;
59 struct completion tx_complete;
60
61 struct ieee802154_dev *dev;
Alexander Aringf76014f772014-07-03 00:20:44 +020062 struct regmap *regmap;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000063
64 spinlock_t lock;
Sascha Herrmann057dad62013-04-14 22:33:29 +000065 bool irq_busy;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000066 bool is_tx;
Alexander Aringa53d1f72014-07-03 00:20:46 +020067
68 struct at86rf2xx_chip_data *data;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010069 bool tx_aret;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000070};
71
72#define RG_TRX_STATUS (0x01)
73#define SR_TRX_STATUS 0x01, 0x1f, 0
74#define SR_RESERVED_01_3 0x01, 0x20, 5
75#define SR_CCA_STATUS 0x01, 0x40, 6
76#define SR_CCA_DONE 0x01, 0x80, 7
77#define RG_TRX_STATE (0x02)
78#define SR_TRX_CMD 0x02, 0x1f, 0
79#define SR_TRAC_STATUS 0x02, 0xe0, 5
80#define RG_TRX_CTRL_0 (0x03)
81#define SR_CLKM_CTRL 0x03, 0x07, 0
82#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
83#define SR_PAD_IO_CLKM 0x03, 0x30, 4
84#define SR_PAD_IO 0x03, 0xc0, 6
85#define RG_TRX_CTRL_1 (0x04)
86#define SR_IRQ_POLARITY 0x04, 0x01, 0
87#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
88#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
89#define SR_RX_BL_CTRL 0x04, 0x10, 4
90#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
91#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
92#define SR_PA_EXT_EN 0x04, 0x80, 7
93#define RG_PHY_TX_PWR (0x05)
94#define SR_TX_PWR 0x05, 0x0f, 0
95#define SR_PA_LT 0x05, 0x30, 4
96#define SR_PA_BUF_LT 0x05, 0xc0, 6
97#define RG_PHY_RSSI (0x06)
98#define SR_RSSI 0x06, 0x1f, 0
99#define SR_RND_VALUE 0x06, 0x60, 5
100#define SR_RX_CRC_VALID 0x06, 0x80, 7
101#define RG_PHY_ED_LEVEL (0x07)
102#define SR_ED_LEVEL 0x07, 0xff, 0
103#define RG_PHY_CC_CCA (0x08)
104#define SR_CHANNEL 0x08, 0x1f, 0
105#define SR_CCA_MODE 0x08, 0x60, 5
106#define SR_CCA_REQUEST 0x08, 0x80, 7
107#define RG_CCA_THRES (0x09)
108#define SR_CCA_ED_THRES 0x09, 0x0f, 0
109#define SR_RESERVED_09_1 0x09, 0xf0, 4
110#define RG_RX_CTRL (0x0a)
111#define SR_PDT_THRES 0x0a, 0x0f, 0
112#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
113#define RG_SFD_VALUE (0x0b)
114#define SR_SFD_VALUE 0x0b, 0xff, 0
115#define RG_TRX_CTRL_2 (0x0c)
116#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100117#define SR_SUB_MODE 0x0c, 0x04, 2
118#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100119#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
120#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000121#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
122#define RG_ANT_DIV (0x0d)
123#define SR_ANT_CTRL 0x0d, 0x03, 0
124#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
125#define SR_ANT_DIV_EN 0x0d, 0x08, 3
126#define SR_RESERVED_0d_2 0x0d, 0x70, 4
127#define SR_ANT_SEL 0x0d, 0x80, 7
128#define RG_IRQ_MASK (0x0e)
129#define SR_IRQ_MASK 0x0e, 0xff, 0
130#define RG_IRQ_STATUS (0x0f)
131#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
132#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
133#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
134#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
135#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
136#define SR_IRQ_5_AMI 0x0f, 0x20, 5
137#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
138#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
139#define RG_VREG_CTRL (0x10)
140#define SR_RESERVED_10_6 0x10, 0x03, 0
141#define SR_DVDD_OK 0x10, 0x04, 2
142#define SR_DVREG_EXT 0x10, 0x08, 3
143#define SR_RESERVED_10_3 0x10, 0x30, 4
144#define SR_AVDD_OK 0x10, 0x40, 6
145#define SR_AVREG_EXT 0x10, 0x80, 7
146#define RG_BATMON (0x11)
147#define SR_BATMON_VTH 0x11, 0x0f, 0
148#define SR_BATMON_HR 0x11, 0x10, 4
149#define SR_BATMON_OK 0x11, 0x20, 5
150#define SR_RESERVED_11_1 0x11, 0xc0, 6
151#define RG_XOSC_CTRL (0x12)
152#define SR_XTAL_TRIM 0x12, 0x0f, 0
153#define SR_XTAL_MODE 0x12, 0xf0, 4
154#define RG_RX_SYN (0x15)
155#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
156#define SR_RESERVED_15_2 0x15, 0x70, 4
157#define SR_RX_PDT_DIS 0x15, 0x80, 7
158#define RG_XAH_CTRL_1 (0x17)
159#define SR_RESERVED_17_8 0x17, 0x01, 0
160#define SR_AACK_PROM_MODE 0x17, 0x02, 1
161#define SR_AACK_ACK_TIME 0x17, 0x04, 2
162#define SR_RESERVED_17_5 0x17, 0x08, 3
163#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
164#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100165#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000166#define SR_RESERVED_17_1 0x17, 0x80, 7
167#define RG_FTN_CTRL (0x18)
168#define SR_RESERVED_18_2 0x18, 0x7f, 0
169#define SR_FTN_START 0x18, 0x80, 7
170#define RG_PLL_CF (0x1a)
171#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
172#define SR_PLL_CF_START 0x1a, 0x80, 7
173#define RG_PLL_DCU (0x1b)
174#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
175#define SR_RESERVED_1b_2 0x1b, 0x40, 6
176#define SR_PLL_DCU_START 0x1b, 0x80, 7
177#define RG_PART_NUM (0x1c)
178#define SR_PART_NUM 0x1c, 0xff, 0
179#define RG_VERSION_NUM (0x1d)
180#define SR_VERSION_NUM 0x1d, 0xff, 0
181#define RG_MAN_ID_0 (0x1e)
182#define SR_MAN_ID_0 0x1e, 0xff, 0
183#define RG_MAN_ID_1 (0x1f)
184#define SR_MAN_ID_1 0x1f, 0xff, 0
185#define RG_SHORT_ADDR_0 (0x20)
186#define SR_SHORT_ADDR_0 0x20, 0xff, 0
187#define RG_SHORT_ADDR_1 (0x21)
188#define SR_SHORT_ADDR_1 0x21, 0xff, 0
189#define RG_PAN_ID_0 (0x22)
190#define SR_PAN_ID_0 0x22, 0xff, 0
191#define RG_PAN_ID_1 (0x23)
192#define SR_PAN_ID_1 0x23, 0xff, 0
193#define RG_IEEE_ADDR_0 (0x24)
194#define SR_IEEE_ADDR_0 0x24, 0xff, 0
195#define RG_IEEE_ADDR_1 (0x25)
196#define SR_IEEE_ADDR_1 0x25, 0xff, 0
197#define RG_IEEE_ADDR_2 (0x26)
198#define SR_IEEE_ADDR_2 0x26, 0xff, 0
199#define RG_IEEE_ADDR_3 (0x27)
200#define SR_IEEE_ADDR_3 0x27, 0xff, 0
201#define RG_IEEE_ADDR_4 (0x28)
202#define SR_IEEE_ADDR_4 0x28, 0xff, 0
203#define RG_IEEE_ADDR_5 (0x29)
204#define SR_IEEE_ADDR_5 0x29, 0xff, 0
205#define RG_IEEE_ADDR_6 (0x2a)
206#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
207#define RG_IEEE_ADDR_7 (0x2b)
208#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
209#define RG_XAH_CTRL_0 (0x2c)
210#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
211#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
212#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
213#define RG_CSMA_SEED_0 (0x2d)
214#define SR_CSMA_SEED_0 0x2d, 0xff, 0
215#define RG_CSMA_SEED_1 (0x2e)
216#define SR_CSMA_SEED_1 0x2e, 0x07, 0
217#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
218#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
219#define SR_AACK_SET_PD 0x2e, 0x20, 5
220#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
221#define RG_CSMA_BE (0x2f)
222#define SR_MIN_BE 0x2f, 0x0f, 0
223#define SR_MAX_BE 0x2f, 0xf0, 4
224
225#define CMD_REG 0x80
226#define CMD_REG_MASK 0x3f
227#define CMD_WRITE 0x40
228#define CMD_FB 0x20
229
230#define IRQ_BAT_LOW (1 << 7)
231#define IRQ_TRX_UR (1 << 6)
232#define IRQ_AMI (1 << 5)
233#define IRQ_CCA_ED (1 << 4)
234#define IRQ_TRX_END (1 << 3)
235#define IRQ_RX_START (1 << 2)
236#define IRQ_PLL_UNL (1 << 1)
237#define IRQ_PLL_LOCK (1 << 0)
238
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000239#define IRQ_ACTIVE_HIGH 0
240#define IRQ_ACTIVE_LOW 1
241
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000242#define STATE_P_ON 0x00 /* BUSY */
243#define STATE_BUSY_RX 0x01
244#define STATE_BUSY_TX 0x02
245#define STATE_FORCE_TRX_OFF 0x03
246#define STATE_FORCE_TX_ON 0x04 /* IDLE */
247/* 0x05 */ /* INVALID_PARAMETER */
248#define STATE_RX_ON 0x06
249/* 0x07 */ /* SUCCESS */
250#define STATE_TRX_OFF 0x08
251#define STATE_TX_ON 0x09
252/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
253#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500254#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000255#define STATE_BUSY_RX_AACK 0x11
256#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000257#define STATE_RX_AACK_ON 0x16
258#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000259#define STATE_RX_ON_NOCLK 0x1C
260#define STATE_RX_AACK_ON_NOCLK 0x1D
261#define STATE_BUSY_RX_AACK_NOCLK 0x1E
262#define STATE_TRANSITION_IN_PROGRESS 0x1F
263
Alexander Aringf76014f772014-07-03 00:20:44 +0200264#define AT86RF2XX_NUMREGS 0x3F
265
266static inline int
267__at86rf230_write(struct at86rf230_local *lp,
268 unsigned int addr, unsigned int data)
269{
270 return regmap_write(lp->regmap, addr, data);
271}
272
273static inline int
274__at86rf230_read(struct at86rf230_local *lp,
275 unsigned int addr, unsigned int *data)
276{
277 return regmap_read(lp->regmap, addr, data);
278}
279
280static inline int
281at86rf230_read_subreg(struct at86rf230_local *lp,
282 unsigned int addr, unsigned int mask,
283 unsigned int shift, unsigned int *data)
284{
285 int rc;
286
287 rc = __at86rf230_read(lp, addr, data);
288 if (rc > 0)
289 *data = (*data & mask) >> shift;
290
291 return rc;
292}
293
294static inline int
295at86rf230_write_subreg(struct at86rf230_local *lp,
296 unsigned int addr, unsigned int mask,
297 unsigned int shift, unsigned int data)
298{
299 return regmap_update_bits(lp->regmap, addr, mask, data << shift);
300}
301
302static bool
303at86rf230_reg_writeable(struct device *dev, unsigned int reg)
304{
305 switch (reg) {
306 case RG_TRX_STATE:
307 case RG_TRX_CTRL_0:
308 case RG_TRX_CTRL_1:
309 case RG_PHY_TX_PWR:
310 case RG_PHY_ED_LEVEL:
311 case RG_PHY_CC_CCA:
312 case RG_CCA_THRES:
313 case RG_RX_CTRL:
314 case RG_SFD_VALUE:
315 case RG_TRX_CTRL_2:
316 case RG_ANT_DIV:
317 case RG_IRQ_MASK:
318 case RG_VREG_CTRL:
319 case RG_BATMON:
320 case RG_XOSC_CTRL:
321 case RG_RX_SYN:
322 case RG_XAH_CTRL_1:
323 case RG_FTN_CTRL:
324 case RG_PLL_CF:
325 case RG_PLL_DCU:
326 case RG_SHORT_ADDR_0:
327 case RG_SHORT_ADDR_1:
328 case RG_PAN_ID_0:
329 case RG_PAN_ID_1:
330 case RG_IEEE_ADDR_0:
331 case RG_IEEE_ADDR_1:
332 case RG_IEEE_ADDR_2:
333 case RG_IEEE_ADDR_3:
334 case RG_IEEE_ADDR_4:
335 case RG_IEEE_ADDR_5:
336 case RG_IEEE_ADDR_6:
337 case RG_IEEE_ADDR_7:
338 case RG_XAH_CTRL_0:
339 case RG_CSMA_SEED_0:
340 case RG_CSMA_SEED_1:
341 case RG_CSMA_BE:
342 return true;
343 default:
344 return false;
345 }
346}
347
348static bool
349at86rf230_reg_readable(struct device *dev, unsigned int reg)
350{
351 bool rc;
352
353 /* all writeable are also readable */
354 rc = at86rf230_reg_writeable(dev, reg);
355 if (rc)
356 return rc;
357
358 /* readonly regs */
359 switch (reg) {
360 case RG_TRX_STATUS:
361 case RG_PHY_RSSI:
362 case RG_IRQ_STATUS:
363 case RG_PART_NUM:
364 case RG_VERSION_NUM:
365 case RG_MAN_ID_1:
366 case RG_MAN_ID_0:
367 return true;
368 default:
369 return false;
370 }
371}
372
373static bool
374at86rf230_reg_volatile(struct device *dev, unsigned int reg)
375{
376 /* can be changed during runtime */
377 switch (reg) {
378 case RG_TRX_STATUS:
379 case RG_TRX_STATE:
380 case RG_PHY_RSSI:
381 case RG_PHY_ED_LEVEL:
382 case RG_IRQ_STATUS:
383 case RG_VREG_CTRL:
384 return true;
385 default:
386 return false;
387 }
388}
389
390static bool
391at86rf230_reg_precious(struct device *dev, unsigned int reg)
392{
393 /* don't clear irq line on read */
394 switch (reg) {
395 case RG_IRQ_STATUS:
396 return true;
397 default:
398 return false;
399 }
400}
401
402static struct regmap_config at86rf230_regmap_spi_config = {
403 .reg_bits = 8,
404 .val_bits = 8,
405 .write_flag_mask = CMD_REG | CMD_WRITE,
406 .read_flag_mask = CMD_REG,
407 .cache_type = REGCACHE_RBTREE,
408 .max_register = AT86RF2XX_NUMREGS,
409 .writeable_reg = at86rf230_reg_writeable,
410 .readable_reg = at86rf230_reg_readable,
411 .volatile_reg = at86rf230_reg_volatile,
412 .precious_reg = at86rf230_reg_precious,
413};
414
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000415static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000416at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len)
417{
418 u8 *buf = lp->buf;
419 int status;
420 struct spi_message msg;
421 struct spi_transfer xfer_head = {
422 .len = 2,
423 .tx_buf = buf,
424
425 };
426 struct spi_transfer xfer_buf = {
427 .len = len,
428 .tx_buf = data,
429 };
430
431 mutex_lock(&lp->bmux);
432 buf[0] = CMD_WRITE | CMD_FB;
433 buf[1] = len + 2; /* 2 bytes for CRC that isn't written */
434
435 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
436 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
437
438 spi_message_init(&msg);
439 spi_message_add_tail(&xfer_head, &msg);
440 spi_message_add_tail(&xfer_buf, &msg);
441
442 status = spi_sync(lp->spi, &msg);
443 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
444 if (msg.status)
445 status = msg.status;
446
447 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
448 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
449 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
450
451 mutex_unlock(&lp->bmux);
452 return status;
453}
454
455static int
456at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi)
457{
458 u8 *buf = lp->buf;
459 int status;
460 struct spi_message msg;
461 struct spi_transfer xfer_head = {
462 .len = 2,
463 .tx_buf = buf,
464 .rx_buf = buf,
465 };
466 struct spi_transfer xfer_head1 = {
467 .len = 2,
468 .tx_buf = buf,
469 .rx_buf = buf,
470 };
471 struct spi_transfer xfer_buf = {
472 .len = 0,
473 .rx_buf = data,
474 };
475
476 mutex_lock(&lp->bmux);
477
478 buf[0] = CMD_FB;
479 buf[1] = 0x00;
480
481 spi_message_init(&msg);
482 spi_message_add_tail(&xfer_head, &msg);
483
484 status = spi_sync(lp->spi, &msg);
485 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
486
487 xfer_buf.len = *(buf + 1) + 1;
488 *len = buf[1];
489
490 buf[0] = CMD_FB;
491 buf[1] = 0x00;
492
493 spi_message_init(&msg);
494 spi_message_add_tail(&xfer_head1, &msg);
495 spi_message_add_tail(&xfer_buf, &msg);
496
497 status = spi_sync(lp->spi, &msg);
498
499 if (msg.status)
500 status = msg.status;
501
502 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
503 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
504 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
505
506 if (status) {
507 if (lqi && (*len > lp->buf[1]))
508 *lqi = data[lp->buf[1]];
509 }
510 mutex_unlock(&lp->bmux);
511
512 return status;
513}
514
515static int
516at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
517{
518 might_sleep();
519 BUG_ON(!level);
520 *level = 0xbe;
521 return 0;
522}
523
524static int
525at86rf230_state(struct ieee802154_dev *dev, int state)
526{
527 struct at86rf230_local *lp = dev->priv;
528 int rc;
Alexander Aringf76014f772014-07-03 00:20:44 +0200529 unsigned int val;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000530 u8 desired_status;
531
532 might_sleep();
533
534 if (state == STATE_FORCE_TX_ON)
535 desired_status = STATE_TX_ON;
536 else if (state == STATE_FORCE_TRX_OFF)
537 desired_status = STATE_TRX_OFF;
538 else
539 desired_status = state;
540
541 do {
542 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
543 if (rc)
544 goto err;
545 } while (val == STATE_TRANSITION_IN_PROGRESS);
546
547 if (val == desired_status)
548 return 0;
549
550 /* state is equal to phy states */
551 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state);
552 if (rc)
553 goto err;
554
555 do {
556 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
557 if (rc)
558 goto err;
559 } while (val == STATE_TRANSITION_IN_PROGRESS);
560
561
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100562 if (val == desired_status ||
563 (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) ||
564 (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000565 return 0;
566
567 pr_err("unexpected state change: %d, asked for %d\n", val, state);
568 return -EBUSY;
569
570err:
571 pr_err("error: %d\n", rc);
572 return rc;
573}
574
575static int
576at86rf230_start(struct ieee802154_dev *dev)
577{
578 struct at86rf230_local *lp = dev->priv;
579 u8 rc;
580
581 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
582 if (rc)
583 return rc;
584
Alexander Aring7332fcb2014-03-15 09:29:03 +0100585 rc = at86rf230_state(dev, STATE_TX_ON);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100586 if (rc)
587 return rc;
588
Phoebe Buckheister5b520bb2014-02-17 11:34:07 +0100589 return at86rf230_state(dev, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000590}
591
592static void
593at86rf230_stop(struct ieee802154_dev *dev)
594{
595 at86rf230_state(dev, STATE_FORCE_TRX_OFF);
596}
597
598static int
Alexander Aringa53d1f72014-07-03 00:20:46 +0200599at86rf23x_set_channel(struct at86rf230_local *lp, int page, int channel)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100600{
601 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
602}
603
604static int
605at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
606{
607 int rc;
608
609 if (channel == 0)
610 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
611 else
612 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
613 if (rc < 0)
614 return rc;
615
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100616 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100617 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Alexander Aringa53d1f72014-07-03 00:20:46 +0200618 lp->data->rssi_base_val = -100;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100619 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100620 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Alexander Aringa53d1f72014-07-03 00:20:46 +0200621 lp->data->rssi_base_val = -98;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100622 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100623 if (rc < 0)
624 return rc;
625
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100626 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
627}
628
629static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000630at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
631{
632 struct at86rf230_local *lp = dev->priv;
633 int rc;
634
635 might_sleep();
636
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100637 if (page < 0 || page > 31 ||
638 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000639 WARN_ON(1);
640 return -EINVAL;
641 }
642
Alexander Aringa53d1f72014-07-03 00:20:46 +0200643 rc = lp->data->set_channel(lp, page, channel);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100644 if (rc < 0)
645 return rc;
646
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000647 msleep(1); /* Wait for PLL */
648 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100649 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000650
651 return 0;
652}
653
654static int
655at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
656{
657 struct at86rf230_local *lp = dev->priv;
658 int rc;
659 unsigned long flags;
660
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100661 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000662 if (lp->irq_busy) {
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100663 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000664 return -EBUSY;
665 }
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100666 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000667
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000668 might_sleep();
669
670 rc = at86rf230_state(dev, STATE_FORCE_TX_ON);
671 if (rc)
672 goto err;
673
674 spin_lock_irqsave(&lp->lock, flags);
675 lp->is_tx = 1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800676 reinit_completion(&lp->tx_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000677 spin_unlock_irqrestore(&lp->lock, flags);
678
679 rc = at86rf230_write_fbuf(lp, skb->data, skb->len);
680 if (rc)
681 goto err_rx;
682
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100683 if (lp->tx_aret) {
684 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ARET_ON);
685 if (rc)
686 goto err_rx;
687 }
688
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000689 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX);
690 if (rc)
691 goto err_rx;
692
693 rc = wait_for_completion_interruptible(&lp->tx_complete);
694 if (rc < 0)
695 goto err_rx;
696
Alexander Aringee695592014-04-24 19:09:14 +0200697 return at86rf230_start(dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000698err_rx:
699 at86rf230_start(dev);
700err:
701 pr_err("error: %d\n", rc);
702
703 spin_lock_irqsave(&lp->lock, flags);
704 lp->is_tx = 0;
705 spin_unlock_irqrestore(&lp->lock, flags);
706
707 return rc;
708}
709
710static int at86rf230_rx(struct at86rf230_local *lp)
711{
712 u8 len = 128, lqi = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000713 struct sk_buff *skb;
714
715 skb = alloc_skb(len, GFP_KERNEL);
716
717 if (!skb)
718 return -ENOMEM;
719
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000720 if (at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000721 goto err;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000722
723 if (len < 2)
724 goto err;
725
726 skb_trim(skb, len - 2); /* We do not put CRC into the frame */
727
728 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
729
alex.bluesman.smirnov@gmail.com23c34212012-07-01 20:18:32 +0000730 dev_dbg(&lp->spi->dev, "READ_FBUF: %d %x\n", len, lqi);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000731
732 return 0;
733err:
734 pr_debug("received frame is too small\n");
735
736 kfree_skb(skb);
737 return -EINVAL;
738}
739
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000740static int
741at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
742 struct ieee802154_hw_addr_filt *filt,
743 unsigned long changed)
744{
745 struct at86rf230_local *lp = dev->priv;
746
747 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100748 u16 addr = le16_to_cpu(filt->short_addr);
749
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000750 dev_vdbg(&lp->spi->dev,
751 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100752 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
753 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000754 }
755
756 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100757 u16 pan = le16_to_cpu(filt->pan_id);
758
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000759 dev_vdbg(&lp->spi->dev,
760 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100761 __at86rf230_write(lp, RG_PAN_ID_0, pan);
762 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000763 }
764
765 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100766 u8 i, addr[8];
767
768 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000769 dev_vdbg(&lp->spi->dev,
770 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100771 for (i = 0; i < 8; i++)
772 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000773 }
774
775 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
776 dev_vdbg(&lp->spi->dev,
777 "at86rf230_set_hw_addr_filt called for panc change\n");
778 if (filt->pan_coord)
779 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
780 else
781 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
782 }
783
784 return 0;
785}
786
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100787static int
Alexander Aring640985e2014-07-03 00:20:43 +0200788at86rf230_set_txpower(struct ieee802154_dev *dev, int db)
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100789{
790 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100791
792 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
793 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
794 * 0dB.
795 * thus, supported values for db range from -26 to 5, for 31dB of
796 * reduction to 0dB of reduction.
797 */
798 if (db > 5 || db < -26)
799 return -EINVAL;
800
801 db = -(db - 5);
802
Jean Sacren677676c2014-03-01 15:54:36 -0700803 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100804}
805
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100806static int
Alexander Aring640985e2014-07-03 00:20:43 +0200807at86rf230_set_lbt(struct ieee802154_dev *dev, bool on)
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100808{
809 struct at86rf230_local *lp = dev->priv;
810
811 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
812}
813
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100814static int
Alexander Aring640985e2014-07-03 00:20:43 +0200815at86rf230_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100816{
817 struct at86rf230_local *lp = dev->priv;
818
819 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
820}
821
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100822static int
Alexander Aringa7d7eda2014-07-03 00:20:47 +0200823at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
824{
825 return (level - lp->data->rssi_base_val) * 100 / 207;
826}
827
828static int
829at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
830{
831 return (level - lp->data->rssi_base_val) / 2;
832}
833
834static int
Alexander Aring640985e2014-07-03 00:20:43 +0200835at86rf230_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100836{
837 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100838
Alexander Aringa53d1f72014-07-03 00:20:46 +0200839 if (level < lp->data->rssi_base_val || level > 30)
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100840 return -EINVAL;
841
Alexander Aringa7d7eda2014-07-03 00:20:47 +0200842 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
843 lp->data->get_desense_steps(lp, level));
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100844}
845
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100846static int
Alexander Aring640985e2014-07-03 00:20:43 +0200847at86rf230_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100848 u8 retries)
849{
850 struct at86rf230_local *lp = dev->priv;
851 int rc;
852
853 if (min_be > max_be || max_be > 8 || retries > 5)
854 return -EINVAL;
855
856 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
857 if (rc)
858 return rc;
859
860 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
861 if (rc)
862 return rc;
863
Alexander Aring39d7f322014-04-05 13:49:26 +0200864 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100865}
866
867static int
Alexander Aring640985e2014-07-03 00:20:43 +0200868at86rf230_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100869{
870 struct at86rf230_local *lp = dev->priv;
871 int rc = 0;
872
873 if (retries < -1 || retries > 15)
874 return -EINVAL;
875
876 lp->tx_aret = retries >= 0;
877
878 if (retries >= 0)
879 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
880
881 return rc;
882}
883
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000884static struct ieee802154_ops at86rf230_ops = {
885 .owner = THIS_MODULE,
886 .xmit = at86rf230_xmit,
887 .ed = at86rf230_ed,
888 .set_channel = at86rf230_channel,
889 .start = at86rf230_start,
890 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000891 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Alexander Aring640985e2014-07-03 00:20:43 +0200892 .set_txpower = at86rf230_set_txpower,
893 .set_lbt = at86rf230_set_lbt,
894 .set_cca_mode = at86rf230_set_cca_mode,
895 .set_cca_ed_level = at86rf230_set_cca_ed_level,
896 .set_csma_params = at86rf230_set_csma_params,
897 .set_frame_retries = at86rf230_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100898};
899
Alexander Aringa53d1f72014-07-03 00:20:46 +0200900static struct at86rf2xx_chip_data at86rf233_data = {
901 .rssi_base_val = -91,
902 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +0200903 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +0200904};
905
906static struct at86rf2xx_chip_data at86rf231_data = {
907 .rssi_base_val = -91,
908 .set_channel = at86rf23x_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +0200909 .get_desense_steps = at86rf23x_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +0200910};
911
912static struct at86rf2xx_chip_data at86rf212_data = {
913 .rssi_base_val = -100,
914 .set_channel = at86rf212_set_channel,
Alexander Aringa7d7eda2014-07-03 00:20:47 +0200915 .get_desense_steps = at86rf212_get_desens_steps
Alexander Aringa53d1f72014-07-03 00:20:46 +0200916};
917
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000918static void at86rf230_irqwork(struct work_struct *work)
919{
920 struct at86rf230_local *lp =
921 container_of(work, struct at86rf230_local, irqwork);
Alexander Aringf76014f772014-07-03 00:20:44 +0200922 unsigned int status;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000923 int rc;
924 unsigned long flags;
925
Alexander Aringf76014f772014-07-03 00:20:44 +0200926 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000927
928 status &= ~IRQ_PLL_LOCK; /* ignore */
929 status &= ~IRQ_RX_START; /* ignore */
930 status &= ~IRQ_AMI; /* ignore */
931 status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/
932
933 if (status & IRQ_TRX_END) {
934 status &= ~IRQ_TRX_END;
Alexander Aring7e814612014-03-15 09:29:04 +0100935 spin_lock_irqsave(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000936 if (lp->is_tx) {
937 lp->is_tx = 0;
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000938 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000939 complete(&lp->tx_complete);
940 } else {
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000941 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000942 at86rf230_rx(lp);
943 }
944 }
945
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000946 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000947 lp->irq_busy = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000948 spin_unlock_irqrestore(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000949}
950
951static void at86rf230_irqwork_level(struct work_struct *work)
952{
953 struct at86rf230_local *lp =
954 container_of(work, struct at86rf230_local, irqwork);
955
956 at86rf230_irqwork(work);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000957
958 enable_irq(lp->spi->irq);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000959}
960
961static irqreturn_t at86rf230_isr(int irq, void *data)
962{
963 struct at86rf230_local *lp = data;
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100964 unsigned long flags;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000965
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100966 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000967 lp->irq_busy = 1;
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100968 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000969
970 schedule_work(&lp->irqwork);
971
972 return IRQ_HANDLED;
973}
974
Sascha Herrmann057dad62013-04-14 22:33:29 +0000975static irqreturn_t at86rf230_isr_level(int irq, void *data)
976{
977 disable_irq_nosync(irq);
978
979 return at86rf230_isr(irq, data);
980}
981
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000982static int at86rf230_hw_init(struct at86rf230_local *lp)
983{
Alexander Aring4af619a2014-04-24 19:09:05 +0200984 int rc, irq_pol, irq_type;
Alexander Aringf76014f772014-07-03 00:20:44 +0200985 unsigned int dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100986 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000987
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +0100988 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF);
989 if (rc)
990 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000991
Alexander Aring4af619a2014-04-24 19:09:05 +0200992 irq_type = irq_get_trigger_type(lp->spi->irq);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000993 /* configure irq polarity, defaults to high active */
Alexander Aring4af619a2014-04-24 19:09:05 +0200994 if (irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000995 irq_pol = IRQ_ACTIVE_LOW;
996 else
997 irq_pol = IRQ_ACTIVE_HIGH;
998
Alexander Aring18c65042014-04-24 19:09:18 +0200999 rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001000 if (rc)
1001 return rc;
1002
Sascha Herrmann057dad62013-04-14 22:33:29 +00001003 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001004 if (rc)
1005 return rc;
1006
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +01001007 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
1008 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
1009 if (rc)
1010 return rc;
1011 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
1012 if (rc)
1013 return rc;
1014
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001015 /* CLKM changes are applied immediately */
1016 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1017 if (rc)
1018 return rc;
1019
1020 /* Turn CLKM Off */
1021 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1022 if (rc)
1023 return rc;
1024 /* Wait the next SLEEP cycle */
1025 msleep(100);
1026
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001027 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001028 if (rc)
1029 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001030 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001031 dev_err(&lp->spi->dev, "DVDD error\n");
1032 return -EINVAL;
1033 }
1034
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001035 return 0;
1036}
1037
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001038static struct at86rf230_platform_data *
1039at86rf230_get_pdata(struct spi_device *spi)
1040{
1041 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001042
1043 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1044 return spi->dev.platform_data;
1045
1046 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1047 if (!pdata)
1048 goto done;
1049
1050 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1051 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1052
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001053 spi->dev.platform_data = pdata;
1054done:
1055 return pdata;
1056}
1057
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001058static int
1059at86rf230_detect_device(struct at86rf230_local *lp)
1060{
1061 unsigned int part, version, val;
1062 u16 man_id = 0;
1063 const char *chip;
1064 int rc;
1065
1066 rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
1067 if (rc)
1068 return rc;
1069 man_id |= val;
1070
1071 rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
1072 if (rc)
1073 return rc;
1074 man_id |= (val << 8);
1075
1076 rc = __at86rf230_read(lp, RG_PART_NUM, &part);
1077 if (rc)
1078 return rc;
1079
1080 rc = __at86rf230_read(lp, RG_PART_NUM, &version);
1081 if (rc)
1082 return rc;
1083
1084 if (man_id != 0x001f) {
1085 dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1086 man_id >> 8, man_id & 0xFF);
1087 return -EINVAL;
1088 }
1089
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001090 lp->dev->extra_tx_headroom = 0;
1091 lp->dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK |
1092 IEEE802154_HW_TXPOWER | IEEE802154_HW_CSMA;
1093
1094 switch (part) {
1095 case 2:
1096 chip = "at86rf230";
1097 rc = -ENOTSUPP;
1098 break;
1099 case 3:
1100 chip = "at86rf231";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001101 lp->data = &at86rf231_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001102 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1103 break;
1104 case 7:
1105 chip = "at86rf212";
1106 if (version == 1) {
Alexander Aringa53d1f72014-07-03 00:20:46 +02001107 lp->data = &at86rf212_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001108 lp->dev->flags |= IEEE802154_HW_LBT;
1109 lp->dev->phy->channels_supported[0] = 0x00007FF;
1110 lp->dev->phy->channels_supported[2] = 0x00007FF;
1111 } else {
1112 rc = -ENOTSUPP;
1113 }
1114 break;
1115 case 11:
1116 chip = "at86rf233";
Alexander Aringa53d1f72014-07-03 00:20:46 +02001117 lp->data = &at86rf233_data;
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001118 lp->dev->phy->channels_supported[0] = 0x7FFF800;
1119 break;
1120 default:
1121 chip = "unkown";
1122 rc = -ENOTSUPP;
1123 break;
1124 }
1125
1126 dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
1127
1128 return rc;
1129}
1130
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001131static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001132{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001133 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001134 struct ieee802154_dev *dev;
1135 struct at86rf230_local *lp;
Alexander Aringf76014f772014-07-03 00:20:44 +02001136 unsigned int status;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001137 irq_handler_t irq_handler;
1138 work_func_t irq_worker;
Alexander Aring4af619a2014-04-24 19:09:05 +02001139 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001140
1141 if (!spi->irq) {
1142 dev_err(&spi->dev, "no IRQ specified\n");
1143 return -EINVAL;
1144 }
1145
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001146 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001147 if (!pdata) {
1148 dev_err(&spi->dev, "no platform_data\n");
1149 return -EINVAL;
1150 }
1151
Alexander Aring3fa27572014-03-15 09:29:06 +01001152 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001153 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1154 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001155 if (rc)
1156 return rc;
1157 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001158
1159 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001160 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1161 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001162 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001163 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001164 }
1165
1166 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001167 if (gpio_is_valid(pdata->rstn)) {
1168 udelay(1);
1169 gpio_set_value(pdata->rstn, 0);
1170 udelay(1);
1171 gpio_set_value(pdata->rstn, 1);
1172 usleep_range(120, 240);
1173 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001174
Alexander Aring640985e2014-07-03 00:20:43 +02001175 dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001176 if (!dev)
1177 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001178
1179 lp = dev->priv;
1180 lp->dev = dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001181 lp->spi = spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001182 dev->parent = &spi->dev;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001183
Alexander Aringf76014f772014-07-03 00:20:44 +02001184 lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
1185 if (IS_ERR(lp->regmap)) {
1186 rc = PTR_ERR(lp->regmap);
1187 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
1188 rc);
1189 goto free_dev;
1190 }
1191
Alexander Aringc8ee0f52014-07-03 00:20:45 +02001192 rc = at86rf230_detect_device(lp);
1193 if (rc < 0)
1194 goto free_dev;
1195
Alexander Aring4af619a2014-04-24 19:09:05 +02001196 irq_type = irq_get_trigger_type(spi->irq);
Phoebe Buckheister363c2cd2014-06-18 16:28:49 +02001197 if (!irq_type)
1198 irq_type = IRQF_TRIGGER_RISING;
Alexander Aring4af619a2014-04-24 19:09:05 +02001199 if (irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Sascha Herrmann057dad62013-04-14 22:33:29 +00001200 irq_worker = at86rf230_irqwork;
1201 irq_handler = at86rf230_isr;
1202 } else {
1203 irq_worker = at86rf230_irqwork_level;
1204 irq_handler = at86rf230_isr_level;
1205 }
1206
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001207 mutex_init(&lp->bmux);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001208 INIT_WORK(&lp->irqwork, irq_worker);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001209 spin_lock_init(&lp->lock);
1210 init_completion(&lp->tx_complete);
1211
1212 spi_set_drvdata(spi, lp);
1213
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001214 rc = at86rf230_hw_init(lp);
1215 if (rc)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001216 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001217
Alexander Aring19626942014-04-24 19:09:15 +02001218 /* Read irq status register to reset irq line */
1219 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001220 if (rc)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001221 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001222
Phoebe Buckheister363c2cd2014-06-18 16:28:49 +02001223 rc = devm_request_irq(&spi->dev, spi->irq, irq_handler,
1224 IRQF_SHARED | irq_type,
Alexander Aring19626942014-04-24 19:09:15 +02001225 dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001226 if (rc)
Alexander Aring652355c2014-04-24 19:09:08 +02001227 goto err_hw_init;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001228
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001229 rc = ieee802154_register_device(lp->dev);
1230 if (rc)
Alexander Aring652355c2014-04-24 19:09:08 +02001231 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001232
1233 return rc;
1234
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001235err_hw_init:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001236 flush_work(&lp->irqwork);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001237 mutex_destroy(&lp->bmux);
Alexander Aring640985e2014-07-03 00:20:43 +02001238free_dev:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001239 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001240
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001241 return rc;
1242}
1243
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001244static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001245{
1246 struct at86rf230_local *lp = spi_get_drvdata(spi);
1247
Alexander Aring17e84a92014-03-31 03:26:51 +02001248 /* mask all at86rf230 irq's */
1249 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001250 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001251 flush_work(&lp->irqwork);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001252 mutex_destroy(&lp->bmux);
1253 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001254 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001255
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001256 return 0;
1257}
1258
Alexander Aring1086b4f2014-04-24 19:09:11 +02001259static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001260 { .compatible = "atmel,at86rf230", },
1261 { .compatible = "atmel,at86rf231", },
1262 { .compatible = "atmel,at86rf233", },
1263 { .compatible = "atmel,at86rf212", },
1264 { },
1265};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001266MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001267
Alexander Aring90b15522014-04-24 19:09:12 +02001268static const struct spi_device_id at86rf230_device_id[] = {
1269 { .name = "at86rf230", },
1270 { .name = "at86rf231", },
1271 { .name = "at86rf233", },
1272 { .name = "at86rf212", },
1273 { },
1274};
1275MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1276
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001277static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001278 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001279 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001280 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001281 .name = "at86rf230",
1282 .owner = THIS_MODULE,
1283 },
1284 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001285 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001286};
1287
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001288module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001289
1290MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1291MODULE_LICENSE("GPL v2");