24 #include <linux/module.h>
29 #define REG_RXMCR 0x00
30 #define REG_PANIDL 0x01
31 #define REG_PANIDH 0x02
32 #define REG_SADRL 0x03
33 #define REG_SADRH 0x04
34 #define REG_EADR0 0x05
35 #define REG_TXMCR 0x11
36 #define REG_PACON0 0x16
37 #define REG_PACON1 0x17
38 #define REG_PACON2 0x18
39 #define REG_TXNCON 0x1B
40 #define REG_TXSTAT 0x24
41 #define REG_SOFTRST 0x2A
42 #define REG_TXSTBL 0x2E
43 #define REG_INTSTAT 0x31
44 #define REG_INTCON 0x32
45 #define REG_RFCTL 0x36
46 #define REG_BBREG1 0x39
47 #define REG_BBREG2 0x3A
48 #define REG_BBREG6 0x3E
49 #define REG_CCAEDTH 0x3F
52 #define REG_RFCON0 0x200
53 #define REG_RFCON1 0x201
54 #define REG_RFCON2 0x202
55 #define REG_RFCON3 0x203
56 #define REG_RFCON5 0x205
57 #define REG_RFCON6 0x206
58 #define REG_RFCON7 0x207
59 #define REG_RFCON8 0x208
60 #define REG_RSSI 0x210
61 #define REG_SLPCON0 0x211
62 #define REG_SLPCON1 0x220
63 #define REG_WAKETIMEL 0x222
64 #define REG_WAKETIMEH 0x223
65 #define REG_RX_FIFO 0x300
68 #define MRF24J40_CHAN_MIN 11
69 #define MRF24J40_CHAN_MAX 26
70 #define CHANNEL_MASK (((u32)1 << (MRF24J40_CHAN_MAX + 1)) \
71 - ((u32)1 << MRF24J40_CHAN_MIN))
73 #define TX_FIFO_SIZE 128
74 #define RX_FIFO_SIZE 144
75 #define SET_CHANNEL_DELAY_US 192
89 #define MRF24J40_READSHORT(reg) ((reg) << 1)
90 #define MRF24J40_WRITESHORT(reg) ((reg) << 1 | 1)
91 #define MRF24J40_READLONG(reg) (1 << 15 | (reg) << 5)
92 #define MRF24J40_WRITELONG(reg) (1 << 15 | (reg) << 5 | 1 << 4)
96 #define MAX_SPI_SPEED_HZ 1000000
98 #define printdev(X) (&X->spi->dev)
106 .tx_buf = devrec->
buf,
107 .rx_buf = devrec->
buf,
110 spi_message_init(&msg);
111 spi_message_add_tail(&xfer, &msg);
120 "SPI write Failed for short register 0x%hhx\n", reg);
132 .tx_buf = devrec->
buf,
133 .rx_buf = devrec->
buf,
136 spi_message_init(&
msg);
137 spi_message_add_tail(&xfer, &
msg);
146 "SPI read Failed for short register 0x%hhx\n", reg);
148 *val = devrec->
buf[1];
161 .tx_buf = devrec->
buf,
162 .rx_buf = devrec->
buf,
165 spi_message_init(&
msg);
166 spi_message_add_tail(&xfer, &
msg);
170 devrec->
buf[0] = cmd >> 8 & 0xff;
171 devrec->
buf[1] = cmd & 0xff;
177 "SPI read Failed for long register 0x%hx\n", reg);
179 *value = devrec->
buf[2];
185 static int write_long_reg(
struct mrf24j40 *devrec,
u16 reg,
u8 val)
192 .tx_buf = devrec->
buf,
193 .rx_buf = devrec->
buf,
196 spi_message_init(&
msg);
197 spi_message_add_tail(&xfer, &
msg);
201 devrec->
buf[0] = cmd >> 8 & 0xff;
202 devrec->
buf[1] = cmd & 0xff;
208 "SPI write Failed for long register 0x%hx\n", reg);
217 static int write_tx_buf(
struct mrf24j40 *devrec,
u16 reg,
226 .tx_buf = devrec->
buf,
239 dev_err(
printdev(devrec),
"write_tx_buf() was passed too large a buffer. Performing short write.\n");
243 spi_message_init(&
msg);
244 spi_message_add_tail(&addr_xfer, &
msg);
245 spi_message_add_tail(&lengths_xfer, &
msg);
246 spi_message_add_tail(&data_xfer, &
msg);
250 devrec->
buf[0] = cmd >> 8 & 0xff;
251 devrec->
buf[1] = cmd & 0xff;
263 static int mrf24j40_read_rx_buf(
struct mrf24j40 *devrec,
294 dev_err(
printdev(devrec),
"Invalid length read from device. Performing short read.\n");
300 dev_err(
printdev(devrec),
"Buffer not big enough. Performing short read\n");
306 addr[0] = cmd >> 8 & 0xff;
307 addr[1] = cmd & 0xff;
308 data_xfer.
len = rx_len;
310 spi_message_init(&
msg);
311 spi_message_add_tail(&addr_xfer, &
msg);
312 spi_message_add_tail(&data_xfer, &
msg);
313 spi_message_add_tail(&status_xfer, &
msg);
328 lqi_rssi[0], lqi_rssi[1]);
343 ret = write_tx_buf(devrec, 0x000, skb->
data, skb->
len);
348 ret = read_short_reg(devrec,
REG_TXNCON, &val);
369 ret = read_short_reg(devrec,
REG_TXSTAT, &val);
399 ret = read_short_reg(devrec,
REG_INTCON, &val);
415 ret = read_short_reg(devrec,
REG_INTCON, &val);
438 val = (channel-11) << 4 | 0x03;
442 ret = read_short_reg(devrec,
REG_RFCTL, &val);
469 write_short_reg(devrec,
REG_SADRH, addrh);
470 write_short_reg(devrec,
REG_SADRL, addrl);
472 "Set short addr to %04hx\n", filt->
short_addr);
478 for (i = 0; i < 8; i++)
484 for (i = 0; i < 8; i++)
493 panidh = filt->
pan_id >> 8 & 0xff;
494 panidl = filt->
pan_id & 0xff;
506 ret = read_short_reg(devrec,
REG_RXMCR, &val);
526 static int mrf24j40_handle_rx(
struct mrf24j40 *devrec)
536 ret = read_short_reg(devrec,
REG_BBREG1, &val);
548 ret = mrf24j40_read_rx_buf(devrec,
skb_put(skb, len), &len, &lqi);
568 ret = read_short_reg(devrec,
REG_BBREG1, &val);
581 .start = mrf24j40_start,
582 .stop = mrf24j40_stop,
583 .set_channel = mrf24j40_set_channel,
584 .set_hw_addr_filt = mrf24j40_filter,
587 static irqreturn_t mrf24j40_isr(
int irq,
void *data)
605 ret = read_short_reg(devrec,
REG_INTSTAT, &intstat);
615 mrf24j40_handle_rx(devrec);
652 devrec->
dev->priv = devrec;
653 devrec->
dev->parent = &devrec->
spi->dev;
660 goto err_register_device;
677 write_short_reg(devrec,
REG_RFCTL, 0x04);
682 ret = read_short_reg(devrec,
REG_RXMCR, &val);
747 .id_table = mrf24j40_ids,
748 .probe = mrf24j40_probe,
752 static int __init mrf24j40_init(
void)
757 static void __exit mrf24j40_exit(
void)
759 spi_unregister_driver(&mrf24j40_driver);