blob: 1331152561f90cbabe90ce1b755b4e70665b9973 [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>
34#include <linux/skbuff.h>
Alexander Aringfa2d3e92014-03-15 09:29:07 +010035#include <linux/of_gpio.h>
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000036
37#include <net/mac802154.h>
38#include <net/wpan-phy.h>
39
40struct at86rf230_local {
41 struct spi_device *spi;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000042
43 u8 part;
44 u8 vers;
45
46 u8 buf[2];
47 struct mutex bmux;
48
49 struct work_struct irqwork;
50 struct completion tx_complete;
51
52 struct ieee802154_dev *dev;
53
54 spinlock_t lock;
Sascha Herrmann057dad62013-04-14 22:33:29 +000055 bool irq_busy;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000056 bool is_tx;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +010057 bool tx_aret;
Phoebe Buckheister6ca00192014-02-17 11:34:12 +010058
59 int rssi_base_val;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000060};
61
Jean Sacren44a6bd82014-02-25 22:38:29 -070062static bool is_rf212(struct at86rf230_local *local)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +010063{
64 return local->part == 7;
65}
66
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +000067#define RG_TRX_STATUS (0x01)
68#define SR_TRX_STATUS 0x01, 0x1f, 0
69#define SR_RESERVED_01_3 0x01, 0x20, 5
70#define SR_CCA_STATUS 0x01, 0x40, 6
71#define SR_CCA_DONE 0x01, 0x80, 7
72#define RG_TRX_STATE (0x02)
73#define SR_TRX_CMD 0x02, 0x1f, 0
74#define SR_TRAC_STATUS 0x02, 0xe0, 5
75#define RG_TRX_CTRL_0 (0x03)
76#define SR_CLKM_CTRL 0x03, 0x07, 0
77#define SR_CLKM_SHA_SEL 0x03, 0x08, 3
78#define SR_PAD_IO_CLKM 0x03, 0x30, 4
79#define SR_PAD_IO 0x03, 0xc0, 6
80#define RG_TRX_CTRL_1 (0x04)
81#define SR_IRQ_POLARITY 0x04, 0x01, 0
82#define SR_IRQ_MASK_MODE 0x04, 0x02, 1
83#define SR_SPI_CMD_MODE 0x04, 0x0c, 2
84#define SR_RX_BL_CTRL 0x04, 0x10, 4
85#define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
86#define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
87#define SR_PA_EXT_EN 0x04, 0x80, 7
88#define RG_PHY_TX_PWR (0x05)
89#define SR_TX_PWR 0x05, 0x0f, 0
90#define SR_PA_LT 0x05, 0x30, 4
91#define SR_PA_BUF_LT 0x05, 0xc0, 6
92#define RG_PHY_RSSI (0x06)
93#define SR_RSSI 0x06, 0x1f, 0
94#define SR_RND_VALUE 0x06, 0x60, 5
95#define SR_RX_CRC_VALID 0x06, 0x80, 7
96#define RG_PHY_ED_LEVEL (0x07)
97#define SR_ED_LEVEL 0x07, 0xff, 0
98#define RG_PHY_CC_CCA (0x08)
99#define SR_CHANNEL 0x08, 0x1f, 0
100#define SR_CCA_MODE 0x08, 0x60, 5
101#define SR_CCA_REQUEST 0x08, 0x80, 7
102#define RG_CCA_THRES (0x09)
103#define SR_CCA_ED_THRES 0x09, 0x0f, 0
104#define SR_RESERVED_09_1 0x09, 0xf0, 4
105#define RG_RX_CTRL (0x0a)
106#define SR_PDT_THRES 0x0a, 0x0f, 0
107#define SR_RESERVED_0a_1 0x0a, 0xf0, 4
108#define RG_SFD_VALUE (0x0b)
109#define SR_SFD_VALUE 0x0b, 0xff, 0
110#define RG_TRX_CTRL_2 (0x0c)
111#define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100112#define SR_SUB_MODE 0x0c, 0x04, 2
113#define SR_BPSK_QPSK 0x0c, 0x08, 3
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100114#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
115#define SR_RESERVED_0c_5 0x0c, 0x60, 5
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000116#define SR_RX_SAFE_MODE 0x0c, 0x80, 7
117#define RG_ANT_DIV (0x0d)
118#define SR_ANT_CTRL 0x0d, 0x03, 0
119#define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
120#define SR_ANT_DIV_EN 0x0d, 0x08, 3
121#define SR_RESERVED_0d_2 0x0d, 0x70, 4
122#define SR_ANT_SEL 0x0d, 0x80, 7
123#define RG_IRQ_MASK (0x0e)
124#define SR_IRQ_MASK 0x0e, 0xff, 0
125#define RG_IRQ_STATUS (0x0f)
126#define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
127#define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
128#define SR_IRQ_2_RX_START 0x0f, 0x04, 2
129#define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
130#define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
131#define SR_IRQ_5_AMI 0x0f, 0x20, 5
132#define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
133#define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
134#define RG_VREG_CTRL (0x10)
135#define SR_RESERVED_10_6 0x10, 0x03, 0
136#define SR_DVDD_OK 0x10, 0x04, 2
137#define SR_DVREG_EXT 0x10, 0x08, 3
138#define SR_RESERVED_10_3 0x10, 0x30, 4
139#define SR_AVDD_OK 0x10, 0x40, 6
140#define SR_AVREG_EXT 0x10, 0x80, 7
141#define RG_BATMON (0x11)
142#define SR_BATMON_VTH 0x11, 0x0f, 0
143#define SR_BATMON_HR 0x11, 0x10, 4
144#define SR_BATMON_OK 0x11, 0x20, 5
145#define SR_RESERVED_11_1 0x11, 0xc0, 6
146#define RG_XOSC_CTRL (0x12)
147#define SR_XTAL_TRIM 0x12, 0x0f, 0
148#define SR_XTAL_MODE 0x12, 0xf0, 4
149#define RG_RX_SYN (0x15)
150#define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
151#define SR_RESERVED_15_2 0x15, 0x70, 4
152#define SR_RX_PDT_DIS 0x15, 0x80, 7
153#define RG_XAH_CTRL_1 (0x17)
154#define SR_RESERVED_17_8 0x17, 0x01, 0
155#define SR_AACK_PROM_MODE 0x17, 0x02, 1
156#define SR_AACK_ACK_TIME 0x17, 0x04, 2
157#define SR_RESERVED_17_5 0x17, 0x08, 3
158#define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
159#define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100160#define SR_CSMA_LBT_MODE 0x17, 0x40, 6
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000161#define SR_RESERVED_17_1 0x17, 0x80, 7
162#define RG_FTN_CTRL (0x18)
163#define SR_RESERVED_18_2 0x18, 0x7f, 0
164#define SR_FTN_START 0x18, 0x80, 7
165#define RG_PLL_CF (0x1a)
166#define SR_RESERVED_1a_2 0x1a, 0x7f, 0
167#define SR_PLL_CF_START 0x1a, 0x80, 7
168#define RG_PLL_DCU (0x1b)
169#define SR_RESERVED_1b_3 0x1b, 0x3f, 0
170#define SR_RESERVED_1b_2 0x1b, 0x40, 6
171#define SR_PLL_DCU_START 0x1b, 0x80, 7
172#define RG_PART_NUM (0x1c)
173#define SR_PART_NUM 0x1c, 0xff, 0
174#define RG_VERSION_NUM (0x1d)
175#define SR_VERSION_NUM 0x1d, 0xff, 0
176#define RG_MAN_ID_0 (0x1e)
177#define SR_MAN_ID_0 0x1e, 0xff, 0
178#define RG_MAN_ID_1 (0x1f)
179#define SR_MAN_ID_1 0x1f, 0xff, 0
180#define RG_SHORT_ADDR_0 (0x20)
181#define SR_SHORT_ADDR_0 0x20, 0xff, 0
182#define RG_SHORT_ADDR_1 (0x21)
183#define SR_SHORT_ADDR_1 0x21, 0xff, 0
184#define RG_PAN_ID_0 (0x22)
185#define SR_PAN_ID_0 0x22, 0xff, 0
186#define RG_PAN_ID_1 (0x23)
187#define SR_PAN_ID_1 0x23, 0xff, 0
188#define RG_IEEE_ADDR_0 (0x24)
189#define SR_IEEE_ADDR_0 0x24, 0xff, 0
190#define RG_IEEE_ADDR_1 (0x25)
191#define SR_IEEE_ADDR_1 0x25, 0xff, 0
192#define RG_IEEE_ADDR_2 (0x26)
193#define SR_IEEE_ADDR_2 0x26, 0xff, 0
194#define RG_IEEE_ADDR_3 (0x27)
195#define SR_IEEE_ADDR_3 0x27, 0xff, 0
196#define RG_IEEE_ADDR_4 (0x28)
197#define SR_IEEE_ADDR_4 0x28, 0xff, 0
198#define RG_IEEE_ADDR_5 (0x29)
199#define SR_IEEE_ADDR_5 0x29, 0xff, 0
200#define RG_IEEE_ADDR_6 (0x2a)
201#define SR_IEEE_ADDR_6 0x2a, 0xff, 0
202#define RG_IEEE_ADDR_7 (0x2b)
203#define SR_IEEE_ADDR_7 0x2b, 0xff, 0
204#define RG_XAH_CTRL_0 (0x2c)
205#define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
206#define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
207#define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
208#define RG_CSMA_SEED_0 (0x2d)
209#define SR_CSMA_SEED_0 0x2d, 0xff, 0
210#define RG_CSMA_SEED_1 (0x2e)
211#define SR_CSMA_SEED_1 0x2e, 0x07, 0
212#define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
213#define SR_AACK_DIS_ACK 0x2e, 0x10, 4
214#define SR_AACK_SET_PD 0x2e, 0x20, 5
215#define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
216#define RG_CSMA_BE (0x2f)
217#define SR_MIN_BE 0x2f, 0x0f, 0
218#define SR_MAX_BE 0x2f, 0xf0, 4
219
220#define CMD_REG 0x80
221#define CMD_REG_MASK 0x3f
222#define CMD_WRITE 0x40
223#define CMD_FB 0x20
224
225#define IRQ_BAT_LOW (1 << 7)
226#define IRQ_TRX_UR (1 << 6)
227#define IRQ_AMI (1 << 5)
228#define IRQ_CCA_ED (1 << 4)
229#define IRQ_TRX_END (1 << 3)
230#define IRQ_RX_START (1 << 2)
231#define IRQ_PLL_UNL (1 << 1)
232#define IRQ_PLL_LOCK (1 << 0)
233
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000234#define IRQ_ACTIVE_HIGH 0
235#define IRQ_ACTIVE_LOW 1
236
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000237#define STATE_P_ON 0x00 /* BUSY */
238#define STATE_BUSY_RX 0x01
239#define STATE_BUSY_TX 0x02
240#define STATE_FORCE_TRX_OFF 0x03
241#define STATE_FORCE_TX_ON 0x04 /* IDLE */
242/* 0x05 */ /* INVALID_PARAMETER */
243#define STATE_RX_ON 0x06
244/* 0x07 */ /* SUCCESS */
245#define STATE_TRX_OFF 0x08
246#define STATE_TX_ON 0x09
247/* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
248#define STATE_SLEEP 0x0F
Thomas Stilwell48d5dba2014-03-10 19:29:25 -0500249#define STATE_PREP_DEEP_SLEEP 0x10
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000250#define STATE_BUSY_RX_AACK 0x11
251#define STATE_BUSY_TX_ARET 0x12
stefan@datenfreihafen.org028889b2013-03-26 12:41:31 +0000252#define STATE_RX_AACK_ON 0x16
253#define STATE_TX_ARET_ON 0x19
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000254#define STATE_RX_ON_NOCLK 0x1C
255#define STATE_RX_AACK_ON_NOCLK 0x1D
256#define STATE_BUSY_RX_AACK_NOCLK 0x1E
257#define STATE_TRANSITION_IN_PROGRESS 0x1F
258
259static int
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100260__at86rf230_detect_device(struct spi_device *spi, u16 *man_id, u8 *part,
261 u8 *version)
262{
263 u8 data[4];
264 u8 *buf = kmalloc(2, GFP_KERNEL);
265 int status;
266 struct spi_message msg;
267 struct spi_transfer xfer = {
268 .len = 2,
269 .tx_buf = buf,
270 .rx_buf = buf,
271 };
272 u8 reg;
273
274 if (!buf)
275 return -ENOMEM;
276
277 for (reg = RG_PART_NUM; reg <= RG_MAN_ID_1; reg++) {
278 buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
279 buf[1] = 0xff;
280 dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
281 spi_message_init(&msg);
282 spi_message_add_tail(&xfer, &msg);
283
284 status = spi_sync(spi, &msg);
285 dev_vdbg(&spi->dev, "status = %d\n", status);
286 if (msg.status)
287 status = msg.status;
288
289 dev_vdbg(&spi->dev, "status = %d\n", status);
290 dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
291 dev_vdbg(&spi->dev, "buf[1] = %02x\n", buf[1]);
292
293 if (status == 0)
294 data[reg - RG_PART_NUM] = buf[1];
295 else
296 break;
297 }
298
299 if (status == 0) {
300 *part = data[0];
301 *version = data[1];
302 *man_id = (data[3] << 8) | data[2];
303 }
304
305 kfree(buf);
306
307 return status;
308}
309
310static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000311__at86rf230_write(struct at86rf230_local *lp, u8 addr, u8 data)
312{
313 u8 *buf = lp->buf;
314 int status;
315 struct spi_message msg;
316 struct spi_transfer xfer = {
317 .len = 2,
318 .tx_buf = buf,
319 };
320
321 buf[0] = (addr & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
322 buf[1] = data;
323 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
324 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
325 spi_message_init(&msg);
326 spi_message_add_tail(&xfer, &msg);
327
328 status = spi_sync(lp->spi, &msg);
329 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
330 if (msg.status)
331 status = msg.status;
332
333 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
334 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
335 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
336
337 return status;
338}
339
340static int
341__at86rf230_read_subreg(struct at86rf230_local *lp,
342 u8 addr, u8 mask, int shift, u8 *data)
343{
344 u8 *buf = lp->buf;
345 int status;
346 struct spi_message msg;
347 struct spi_transfer xfer = {
348 .len = 2,
349 .tx_buf = buf,
350 .rx_buf = buf,
351 };
352
353 buf[0] = (addr & CMD_REG_MASK) | CMD_REG;
354 buf[1] = 0xff;
355 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
356 spi_message_init(&msg);
357 spi_message_add_tail(&xfer, &msg);
358
359 status = spi_sync(lp->spi, &msg);
360 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
361 if (msg.status)
362 status = msg.status;
363
364 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
365 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
366 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
367
368 if (status == 0)
Alexander Aring21687462014-04-14 18:48:02 +0200369 *data = (buf[1] & mask) >> shift;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000370
371 return status;
372}
373
374static int
375at86rf230_read_subreg(struct at86rf230_local *lp,
376 u8 addr, u8 mask, int shift, u8 *data)
377{
378 int status;
379
380 mutex_lock(&lp->bmux);
381 status = __at86rf230_read_subreg(lp, addr, mask, shift, data);
382 mutex_unlock(&lp->bmux);
383
384 return status;
385}
386
387static int
388at86rf230_write_subreg(struct at86rf230_local *lp,
389 u8 addr, u8 mask, int shift, u8 data)
390{
391 int status;
392 u8 val;
393
394 mutex_lock(&lp->bmux);
395 status = __at86rf230_read_subreg(lp, addr, 0xff, 0, &val);
396 if (status)
397 goto out;
398
399 val &= ~mask;
400 val |= (data << shift) & mask;
401
402 status = __at86rf230_write(lp, addr, val);
403out:
404 mutex_unlock(&lp->bmux);
405
406 return status;
407}
408
409static int
410at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len)
411{
412 u8 *buf = lp->buf;
413 int status;
414 struct spi_message msg;
415 struct spi_transfer xfer_head = {
416 .len = 2,
417 .tx_buf = buf,
418
419 };
420 struct spi_transfer xfer_buf = {
421 .len = len,
422 .tx_buf = data,
423 };
424
425 mutex_lock(&lp->bmux);
426 buf[0] = CMD_WRITE | CMD_FB;
427 buf[1] = len + 2; /* 2 bytes for CRC that isn't written */
428
429 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
430 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
431
432 spi_message_init(&msg);
433 spi_message_add_tail(&xfer_head, &msg);
434 spi_message_add_tail(&xfer_buf, &msg);
435
436 status = spi_sync(lp->spi, &msg);
437 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
438 if (msg.status)
439 status = msg.status;
440
441 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
442 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
443 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
444
445 mutex_unlock(&lp->bmux);
446 return status;
447}
448
449static int
450at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi)
451{
452 u8 *buf = lp->buf;
453 int status;
454 struct spi_message msg;
455 struct spi_transfer xfer_head = {
456 .len = 2,
457 .tx_buf = buf,
458 .rx_buf = buf,
459 };
460 struct spi_transfer xfer_head1 = {
461 .len = 2,
462 .tx_buf = buf,
463 .rx_buf = buf,
464 };
465 struct spi_transfer xfer_buf = {
466 .len = 0,
467 .rx_buf = data,
468 };
469
470 mutex_lock(&lp->bmux);
471
472 buf[0] = CMD_FB;
473 buf[1] = 0x00;
474
475 spi_message_init(&msg);
476 spi_message_add_tail(&xfer_head, &msg);
477
478 status = spi_sync(lp->spi, &msg);
479 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
480
481 xfer_buf.len = *(buf + 1) + 1;
482 *len = buf[1];
483
484 buf[0] = CMD_FB;
485 buf[1] = 0x00;
486
487 spi_message_init(&msg);
488 spi_message_add_tail(&xfer_head1, &msg);
489 spi_message_add_tail(&xfer_buf, &msg);
490
491 status = spi_sync(lp->spi, &msg);
492
493 if (msg.status)
494 status = msg.status;
495
496 dev_vdbg(&lp->spi->dev, "status = %d\n", status);
497 dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
498 dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
499
500 if (status) {
501 if (lqi && (*len > lp->buf[1]))
502 *lqi = data[lp->buf[1]];
503 }
504 mutex_unlock(&lp->bmux);
505
506 return status;
507}
508
509static int
510at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
511{
512 might_sleep();
513 BUG_ON(!level);
514 *level = 0xbe;
515 return 0;
516}
517
518static int
519at86rf230_state(struct ieee802154_dev *dev, int state)
520{
521 struct at86rf230_local *lp = dev->priv;
522 int rc;
523 u8 val;
524 u8 desired_status;
525
526 might_sleep();
527
528 if (state == STATE_FORCE_TX_ON)
529 desired_status = STATE_TX_ON;
530 else if (state == STATE_FORCE_TRX_OFF)
531 desired_status = STATE_TRX_OFF;
532 else
533 desired_status = state;
534
535 do {
536 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
537 if (rc)
538 goto err;
539 } while (val == STATE_TRANSITION_IN_PROGRESS);
540
541 if (val == desired_status)
542 return 0;
543
544 /* state is equal to phy states */
545 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state);
546 if (rc)
547 goto err;
548
549 do {
550 rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
551 if (rc)
552 goto err;
553 } while (val == STATE_TRANSITION_IN_PROGRESS);
554
555
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100556 if (val == desired_status ||
557 (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) ||
558 (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000559 return 0;
560
561 pr_err("unexpected state change: %d, asked for %d\n", val, state);
562 return -EBUSY;
563
564err:
565 pr_err("error: %d\n", rc);
566 return rc;
567}
568
569static int
570at86rf230_start(struct ieee802154_dev *dev)
571{
572 struct at86rf230_local *lp = dev->priv;
573 u8 rc;
574
575 rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
576 if (rc)
577 return rc;
578
Alexander Aring7332fcb2014-03-15 09:29:03 +0100579 rc = at86rf230_state(dev, STATE_TX_ON);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100580 if (rc)
581 return rc;
582
Phoebe Buckheister5b520bb2014-02-17 11:34:07 +0100583 return at86rf230_state(dev, STATE_RX_AACK_ON);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000584}
585
586static void
587at86rf230_stop(struct ieee802154_dev *dev)
588{
589 at86rf230_state(dev, STATE_FORCE_TRX_OFF);
590}
591
592static int
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100593at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel)
594{
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100595 lp->rssi_base_val = -91;
596
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100597 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
598}
599
600static int
601at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
602{
603 int rc;
604
605 if (channel == 0)
606 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
607 else
608 rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
609 if (rc < 0)
610 return rc;
611
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100612 if (page == 0) {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100613 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100614 lp->rssi_base_val = -100;
615 } else {
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100616 rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100617 lp->rssi_base_val = -98;
618 }
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100619 if (rc < 0)
620 return rc;
621
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100622 return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
623}
624
625static int
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000626at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
627{
628 struct at86rf230_local *lp = dev->priv;
629 int rc;
630
631 might_sleep();
632
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100633 if (page < 0 || page > 31 ||
634 !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000635 WARN_ON(1);
636 return -EINVAL;
637 }
638
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100639 if (is_rf212(lp))
640 rc = at86rf212_set_channel(lp, page, channel);
641 else
642 rc = at86rf230_set_channel(lp, page, channel);
643 if (rc < 0)
644 return rc;
645
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000646 msleep(1); /* Wait for PLL */
647 dev->phy->current_channel = channel;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +0100648 dev->phy->current_page = page;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000649
650 return 0;
651}
652
653static int
654at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
655{
656 struct at86rf230_local *lp = dev->priv;
657 int rc;
658 unsigned long flags;
659
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100660 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000661 if (lp->irq_busy) {
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100662 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000663 return -EBUSY;
664 }
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100665 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000666
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000667 might_sleep();
668
669 rc = at86rf230_state(dev, STATE_FORCE_TX_ON);
670 if (rc)
671 goto err;
672
673 spin_lock_irqsave(&lp->lock, flags);
674 lp->is_tx = 1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800675 reinit_completion(&lp->tx_complete);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000676 spin_unlock_irqrestore(&lp->lock, flags);
677
678 rc = at86rf230_write_fbuf(lp, skb->data, skb->len);
679 if (rc)
680 goto err_rx;
681
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100682 if (lp->tx_aret) {
683 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ARET_ON);
684 if (rc)
685 goto err_rx;
686 }
687
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000688 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX);
689 if (rc)
690 goto err_rx;
691
692 rc = wait_for_completion_interruptible(&lp->tx_complete);
693 if (rc < 0)
694 goto err_rx;
695
Alexander Aringee695592014-04-24 19:09:14 +0200696 return at86rf230_start(dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000697err_rx:
698 at86rf230_start(dev);
699err:
700 pr_err("error: %d\n", rc);
701
702 spin_lock_irqsave(&lp->lock, flags);
703 lp->is_tx = 0;
704 spin_unlock_irqrestore(&lp->lock, flags);
705
706 return rc;
707}
708
709static int at86rf230_rx(struct at86rf230_local *lp)
710{
711 u8 len = 128, lqi = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000712 struct sk_buff *skb;
713
714 skb = alloc_skb(len, GFP_KERNEL);
715
716 if (!skb)
717 return -ENOMEM;
718
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000719 if (at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi))
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000720 goto err;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000721
722 if (len < 2)
723 goto err;
724
725 skb_trim(skb, len - 2); /* We do not put CRC into the frame */
726
727 ieee802154_rx_irqsafe(lp->dev, skb, lqi);
728
alex.bluesman.smirnov@gmail.com23c34212012-07-01 20:18:32 +0000729 dev_dbg(&lp->spi->dev, "READ_FBUF: %d %x\n", len, lqi);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000730
731 return 0;
732err:
733 pr_debug("received frame is too small\n");
734
735 kfree_skb(skb);
736 return -EINVAL;
737}
738
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000739static int
740at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
741 struct ieee802154_hw_addr_filt *filt,
742 unsigned long changed)
743{
744 struct at86rf230_local *lp = dev->priv;
745
746 if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100747 u16 addr = le16_to_cpu(filt->short_addr);
748
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000749 dev_vdbg(&lp->spi->dev,
750 "at86rf230_set_hw_addr_filt called for saddr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100751 __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
752 __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000753 }
754
755 if (changed & IEEE802515_AFILT_PANID_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100756 u16 pan = le16_to_cpu(filt->pan_id);
757
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000758 dev_vdbg(&lp->spi->dev,
759 "at86rf230_set_hw_addr_filt called for pan id\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100760 __at86rf230_write(lp, RG_PAN_ID_0, pan);
761 __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000762 }
763
764 if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100765 u8 i, addr[8];
766
767 memcpy(addr, &filt->ieee_addr, 8);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000768 dev_vdbg(&lp->spi->dev,
769 "at86rf230_set_hw_addr_filt called for IEEE addr\n");
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100770 for (i = 0; i < 8; i++)
771 __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000772 }
773
774 if (changed & IEEE802515_AFILT_PANC_CHANGED) {
775 dev_vdbg(&lp->spi->dev,
776 "at86rf230_set_hw_addr_filt called for panc change\n");
777 if (filt->pan_coord)
778 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
779 else
780 at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
781 }
782
783 return 0;
784}
785
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100786static int
787at86rf212_set_txpower(struct ieee802154_dev *dev, int db)
788{
789 struct at86rf230_local *lp = dev->priv;
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100790
791 /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
792 * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
793 * 0dB.
794 * thus, supported values for db range from -26 to 5, for 31dB of
795 * reduction to 0dB of reduction.
796 */
797 if (db > 5 || db < -26)
798 return -EINVAL;
799
800 db = -(db - 5);
801
Jean Sacren677676c2014-03-01 15:54:36 -0700802 return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100803}
804
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100805static int
806at86rf212_set_lbt(struct ieee802154_dev *dev, bool on)
807{
808 struct at86rf230_local *lp = dev->priv;
809
810 return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
811}
812
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100813static int
814at86rf212_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
815{
816 struct at86rf230_local *lp = dev->priv;
817
818 return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
819}
820
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100821static int
822at86rf212_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
823{
824 struct at86rf230_local *lp = dev->priv;
825 int desens_steps;
826
827 if (level < lp->rssi_base_val || level > 30)
828 return -EINVAL;
829
830 desens_steps = (level - lp->rssi_base_val) * 100 / 207;
831
832 return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, desens_steps);
833}
834
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100835static int
836at86rf212_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
837 u8 retries)
838{
839 struct at86rf230_local *lp = dev->priv;
840 int rc;
841
842 if (min_be > max_be || max_be > 8 || retries > 5)
843 return -EINVAL;
844
845 rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
846 if (rc)
847 return rc;
848
849 rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
850 if (rc)
851 return rc;
852
Alexander Aring39d7f322014-04-05 13:49:26 +0200853 return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100854}
855
856static int
857at86rf212_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
858{
859 struct at86rf230_local *lp = dev->priv;
860 int rc = 0;
861
862 if (retries < -1 || retries > 15)
863 return -EINVAL;
864
865 lp->tx_aret = retries >= 0;
866
867 if (retries >= 0)
868 rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
869
870 return rc;
871}
872
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000873static struct ieee802154_ops at86rf230_ops = {
874 .owner = THIS_MODULE,
875 .xmit = at86rf230_xmit,
876 .ed = at86rf230_ed,
877 .set_channel = at86rf230_channel,
878 .start = at86rf230_start,
879 .stop = at86rf230_stop,
stefan@datenfreihafen.org14867742013-03-26 12:41:30 +0000880 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000881};
882
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100883static struct ieee802154_ops at86rf212_ops = {
884 .owner = THIS_MODULE,
885 .xmit = at86rf230_xmit,
886 .ed = at86rf230_ed,
887 .set_channel = at86rf230_channel,
888 .start = at86rf230_start,
889 .stop = at86rf230_stop,
890 .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100891 .set_txpower = at86rf212_set_txpower,
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100892 .set_lbt = at86rf212_set_lbt,
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100893 .set_cca_mode = at86rf212_set_cca_mode,
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100894 .set_cca_ed_level = at86rf212_set_cca_ed_level,
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100895 .set_csma_params = at86rf212_set_csma_params,
896 .set_frame_retries = at86rf212_set_frame_retries,
Phoebe Buckheister8fad3462014-02-17 11:34:06 +0100897};
898
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000899static void at86rf230_irqwork(struct work_struct *work)
900{
901 struct at86rf230_local *lp =
902 container_of(work, struct at86rf230_local, irqwork);
903 u8 status = 0, val;
904 int rc;
905 unsigned long flags;
906
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000907 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &val);
908 status |= val;
909
910 status &= ~IRQ_PLL_LOCK; /* ignore */
911 status &= ~IRQ_RX_START; /* ignore */
912 status &= ~IRQ_AMI; /* ignore */
913 status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/
914
915 if (status & IRQ_TRX_END) {
916 status &= ~IRQ_TRX_END;
Alexander Aring7e814612014-03-15 09:29:04 +0100917 spin_lock_irqsave(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000918 if (lp->is_tx) {
919 lp->is_tx = 0;
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000920 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000921 complete(&lp->tx_complete);
922 } else {
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000923 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000924 at86rf230_rx(lp);
925 }
926 }
927
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000928 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000929 lp->irq_busy = 0;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000930 spin_unlock_irqrestore(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000931}
932
933static void at86rf230_irqwork_level(struct work_struct *work)
934{
935 struct at86rf230_local *lp =
936 container_of(work, struct at86rf230_local, irqwork);
937
938 at86rf230_irqwork(work);
alex.bluesman.smirnov@gmail.com5b00f2e2012-07-10 21:22:43 +0000939
940 enable_irq(lp->spi->irq);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000941}
942
943static irqreturn_t at86rf230_isr(int irq, void *data)
944{
945 struct at86rf230_local *lp = data;
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100946 unsigned long flags;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000947
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100948 spin_lock_irqsave(&lp->lock, flags);
Sascha Herrmann057dad62013-04-14 22:33:29 +0000949 lp->irq_busy = 1;
Alexander Aring6e07a1e2014-03-12 08:21:24 +0100950 spin_unlock_irqrestore(&lp->lock, flags);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000951
952 schedule_work(&lp->irqwork);
953
954 return IRQ_HANDLED;
955}
956
Sascha Herrmann057dad62013-04-14 22:33:29 +0000957static irqreturn_t at86rf230_isr_level(int irq, void *data)
958{
959 disable_irq_nosync(irq);
960
961 return at86rf230_isr(irq, data);
962}
963
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000964static int at86rf230_irq_polarity(struct at86rf230_local *lp, int pol)
965{
966 return at86rf230_write_subreg(lp, SR_IRQ_POLARITY, pol);
967}
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000968
969static int at86rf230_hw_init(struct at86rf230_local *lp)
970{
Alexander Aring4af619a2014-04-24 19:09:05 +0200971 int rc, irq_pol, irq_type;
Alexander Aring1cc9fc52014-04-24 19:09:17 +0200972 u8 dvdd;
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100973 u8 csma_seed[2];
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000974
Phoebe Buckheister7dcbd222014-02-17 11:34:13 +0100975 rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF);
976 if (rc)
977 return rc;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000978
Alexander Aring4af619a2014-04-24 19:09:05 +0200979 irq_type = irq_get_trigger_type(lp->spi->irq);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000980 /* configure irq polarity, defaults to high active */
Alexander Aring4af619a2014-04-24 19:09:05 +0200981 if (irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
Sascha Herrmann43b5abe2013-04-14 22:33:28 +0000982 irq_pol = IRQ_ACTIVE_LOW;
983 else
984 irq_pol = IRQ_ACTIVE_HIGH;
985
986 rc = at86rf230_irq_polarity(lp, irq_pol);
987 if (rc)
988 return rc;
989
Sascha Herrmann057dad62013-04-14 22:33:29 +0000990 rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +0000991 if (rc)
992 return rc;
993
Phoebe Buckheisterf2fdd672014-02-17 11:34:15 +0100994 get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
995 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
996 if (rc)
997 return rc;
998 rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
999 if (rc)
1000 return rc;
1001
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001002 /* CLKM changes are applied immediately */
1003 rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
1004 if (rc)
1005 return rc;
1006
1007 /* Turn CLKM Off */
1008 rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
1009 if (rc)
1010 return rc;
1011 /* Wait the next SLEEP cycle */
1012 msleep(100);
1013
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001014 rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001015 if (rc)
1016 return rc;
Alexander Aring1cc9fc52014-04-24 19:09:17 +02001017 if (!dvdd) {
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001018 dev_err(&lp->spi->dev, "DVDD error\n");
1019 return -EINVAL;
1020 }
1021
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001022 return 0;
1023}
1024
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001025static struct at86rf230_platform_data *
1026at86rf230_get_pdata(struct spi_device *spi)
1027{
1028 struct at86rf230_platform_data *pdata;
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001029
1030 if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
1031 return spi->dev.platform_data;
1032
1033 pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
1034 if (!pdata)
1035 goto done;
1036
1037 pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
1038 pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
1039
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001040 spi->dev.platform_data = pdata;
1041done:
1042 return pdata;
1043}
1044
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001045static int at86rf230_probe(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001046{
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001047 struct at86rf230_platform_data *pdata;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001048 struct ieee802154_dev *dev;
1049 struct at86rf230_local *lp;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001050 u16 man_id = 0;
1051 u8 part = 0, version = 0, status;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001052 irq_handler_t irq_handler;
1053 work_func_t irq_worker;
Alexander Aring4af619a2014-04-24 19:09:05 +02001054 int rc, irq_type;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001055 const char *chip;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001056 struct ieee802154_ops *ops = NULL;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001057
1058 if (!spi->irq) {
1059 dev_err(&spi->dev, "no IRQ specified\n");
1060 return -EINVAL;
1061 }
1062
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001063 pdata = at86rf230_get_pdata(spi);
Sascha Herrmann43b5abe2013-04-14 22:33:28 +00001064 if (!pdata) {
1065 dev_err(&spi->dev, "no platform_data\n");
1066 return -EINVAL;
1067 }
1068
Alexander Aring3fa27572014-03-15 09:29:06 +01001069 if (gpio_is_valid(pdata->rstn)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001070 rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
1071 GPIOF_OUT_INIT_HIGH, "rstn");
Alexander Aring3fa27572014-03-15 09:29:06 +01001072 if (rc)
1073 return rc;
1074 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001075
1076 if (gpio_is_valid(pdata->slp_tr)) {
Alexander Aring0679e292014-04-24 19:09:09 +02001077 rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
1078 GPIOF_OUT_INIT_LOW, "slp_tr");
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001079 if (rc)
Alexander Aring0679e292014-04-24 19:09:09 +02001080 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001081 }
1082
1083 /* Reset */
Alexander Aring3fa27572014-03-15 09:29:06 +01001084 if (gpio_is_valid(pdata->rstn)) {
1085 udelay(1);
1086 gpio_set_value(pdata->rstn, 0);
1087 udelay(1);
1088 gpio_set_value(pdata->rstn, 1);
1089 usleep_range(120, 240);
1090 }
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001091
1092 rc = __at86rf230_detect_device(spi, &man_id, &part, &version);
1093 if (rc < 0)
Alexander Aring0679e292014-04-24 19:09:09 +02001094 return rc;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001095
1096 if (man_id != 0x001f) {
1097 dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
1098 man_id >> 8, man_id & 0xFF);
Alexander Aring0679e292014-04-24 19:09:09 +02001099 return -EINVAL;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001100 }
1101
1102 switch (part) {
1103 case 2:
1104 chip = "at86rf230";
1105 /* FIXME: should be easy to support; */
1106 break;
1107 case 3:
1108 chip = "at86rf231";
1109 ops = &at86rf230_ops;
1110 break;
1111 case 7:
1112 chip = "at86rf212";
1113 if (version == 1)
1114 ops = &at86rf212_ops;
1115 break;
Thomas Stilwell48d5dba2014-03-10 19:29:25 -05001116 case 11:
1117 chip = "at86rf233";
1118 ops = &at86rf230_ops;
1119 break;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001120 default:
1121 chip = "UNKNOWN";
1122 break;
1123 }
1124
1125 dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version);
Alexander Aring0679e292014-04-24 19:09:09 +02001126 if (!ops)
1127 return -ENOTSUPP;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001128
1129 dev = ieee802154_alloc_device(sizeof(*lp), ops);
Alexander Aring0679e292014-04-24 19:09:09 +02001130 if (!dev)
1131 return -ENOMEM;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001132
1133 lp = dev->priv;
1134 lp->dev = dev;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001135 lp->part = part;
1136 lp->vers = version;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001137
1138 lp->spi = spi;
1139
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001140 dev->parent = &spi->dev;
1141 dev->extra_tx_headroom = 0;
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001142 dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001143
Alexander Aring4af619a2014-04-24 19:09:05 +02001144 irq_type = irq_get_trigger_type(spi->irq);
1145 if (irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
Sascha Herrmann057dad62013-04-14 22:33:29 +00001146 irq_worker = at86rf230_irqwork;
1147 irq_handler = at86rf230_isr;
1148 } else {
1149 irq_worker = at86rf230_irqwork_level;
1150 irq_handler = at86rf230_isr_level;
1151 }
1152
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001153 mutex_init(&lp->bmux);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001154 INIT_WORK(&lp->irqwork, irq_worker);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001155 spin_lock_init(&lp->lock);
1156 init_completion(&lp->tx_complete);
1157
1158 spi_set_drvdata(spi, lp);
1159
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001160 if (is_rf212(lp)) {
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001161 dev->phy->channels_supported[0] = 0x00007FF;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001162 dev->phy->channels_supported[2] = 0x00007FF;
1163 } else {
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001164 dev->phy->channels_supported[0] = 0x7FFF800;
Phoebe Buckheister643e53c2014-02-17 11:34:09 +01001165 }
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001166
1167 rc = at86rf230_hw_init(lp);
1168 if (rc)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001169 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001170
Alexander Aring19626942014-04-24 19:09:15 +02001171 /* Read irq status register to reset irq line */
1172 rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001173 if (rc)
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001174 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001175
Alexander Aring19626942014-04-24 19:09:15 +02001176 rc = devm_request_irq(&spi->dev, spi->irq, irq_handler, IRQF_SHARED,
1177 dev_name(&spi->dev), lp);
Sascha Herrmann057dad62013-04-14 22:33:29 +00001178 if (rc)
Alexander Aring652355c2014-04-24 19:09:08 +02001179 goto err_hw_init;
Sascha Herrmann057dad62013-04-14 22:33:29 +00001180
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001181 rc = ieee802154_register_device(lp->dev);
1182 if (rc)
Alexander Aring652355c2014-04-24 19:09:08 +02001183 goto err_hw_init;
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001184
1185 return rc;
1186
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001187err_hw_init:
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001188 flush_work(&lp->irqwork);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001189 mutex_destroy(&lp->bmux);
1190 ieee802154_free_device(lp->dev);
Phoebe Buckheister8fad3462014-02-17 11:34:06 +01001191
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001192 return rc;
1193}
1194
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001195static int at86rf230_remove(struct spi_device *spi)
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001196{
1197 struct at86rf230_local *lp = spi_get_drvdata(spi);
1198
Alexander Aring17e84a92014-03-31 03:26:51 +02001199 /* mask all at86rf230 irq's */
1200 at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001201 ieee802154_unregister_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001202 flush_work(&lp->irqwork);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001203 mutex_destroy(&lp->bmux);
1204 ieee802154_free_device(lp->dev);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001205 dev_dbg(&spi->dev, "unregistered at86rf230\n");
Alexander Aring0679e292014-04-24 19:09:09 +02001206
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001207 return 0;
1208}
1209
Alexander Aring1086b4f2014-04-24 19:09:11 +02001210static const struct of_device_id at86rf230_of_match[] = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001211 { .compatible = "atmel,at86rf230", },
1212 { .compatible = "atmel,at86rf231", },
1213 { .compatible = "atmel,at86rf233", },
1214 { .compatible = "atmel,at86rf212", },
1215 { },
1216};
Alexander Aring835cb7d2014-04-24 19:09:10 +02001217MODULE_DEVICE_TABLE(of, at86rf230_of_match);
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001218
Alexander Aring90b15522014-04-24 19:09:12 +02001219static const struct spi_device_id at86rf230_device_id[] = {
1220 { .name = "at86rf230", },
1221 { .name = "at86rf231", },
1222 { .name = "at86rf233", },
1223 { .name = "at86rf212", },
1224 { },
1225};
1226MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
1227
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001228static struct spi_driver at86rf230_driver = {
Alexander Aring90b15522014-04-24 19:09:12 +02001229 .id_table = at86rf230_device_id,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001230 .driver = {
Alexander Aringfa2d3e92014-03-15 09:29:07 +01001231 .of_match_table = of_match_ptr(at86rf230_of_match),
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001232 .name = "at86rf230",
1233 .owner = THIS_MODULE,
1234 },
1235 .probe = at86rf230_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001236 .remove = at86rf230_remove,
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001237};
1238
alex.bluesman.smirnov@gmail.com395a5732012-08-26 05:10:10 +00001239module_spi_driver(at86rf230_driver);
alex.bluesman.smirnov@gmail.com7b8e19b2012-06-25 23:24:53 +00001240
1241MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
1242MODULE_LICENSE("GPL v2");