-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfm73.c
715 lines (600 loc) · 18.2 KB
/
rfm73.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
* Simple network device driver for HopeRF RFM73 the SPI RF devices
*
* Copyright (C) 2018-2020 Logic Elements s.r.o.
* Jiri Novotny <jiri.novotny@logicelements.cz>
*
* Based on: Simple network device driver for nrf24l01+ the SPI rf devices
* Copyright (C) 2016 Wolfle
* Bob Guo <wolfle@softhome.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/init.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/if.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/compat.h>
#include <linux/circ_buf.h>
#include <linux/spi/spi.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/timekeeping.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/skbuff.h>
#include <asm/uaccess.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <net/sock.h>
#include "rfm73.h"
struct rfm73_data {
struct spi_device *spi;
struct net_device *dev;
/* gpios */
int ceGpio;
int irqGpio;
/* Packets awaiting transmission */
struct sk_buff *tx_skb;
struct work_struct tx_work;
struct work_struct irq_work;
/* buffers and length are used for spi transfers */
struct spi_transfer t[2];
/* payload */
uint8_t tx_buf[32];
uint8_t rx_buf[32];
uint8_t *recv;
struct mutex rf_lock;
/* some settings */
uint8_t addrLen;
uint8_t payloadLen;
uint8_t channel;
/* extra statistics */
int lastLen;
};
static uint8_t Bank1_Reg0_13[BANK1_ENTRIES][4] = {
{0x40, 0x4B, 0x01, 0xE2}, /* 0 */
{0xC0, 0x4B, 0x00, 0x00}, /* 1 */
{0xD0, 0xFC, 0x8C, 0x02}, /* 2 */
{0x99, 0x00, 0x39, 0x41}, /* 3 */
{0xD9, 0xB6, 0x82, 0x1B}, /* 4 {0xD9,0x96,0x82,0x1B}, enable high sen, no low power*/
{0x24, 0x06, 0x7F, 0xA6}, /* 5 rssi {0x24, 0x02, 0x7F, 0xA6} */
{0x00, 0x00, 0x00, 0x00}, /* 6 */
{0x00, 0x00, 0x00, 0x00}, /* 7 */
{0x00, 0x00, 0x00, 0x00}, /* 8 */
{0x00, 0x00, 0x00, 0x00}, /* 9 */
{0x00, 0x00, 0x00, 0x00}, /* A */
{0x00, 0x00, 0x00, 0x00}, /* B */
{0x00, 0x12, 0x73, 0x05}, /* C */
{0x36, 0xB4, 0x80, 0x00} /* D */
};
static uint8_t Bank1_Reg14[] = {0x41,0x10,0x04,0x82,0x20,0x08,0x08,0xF2,0x7D,0xEF,0xFF};
/*static const uint8_t Bank1_Reg14[] = {0x41,0x20,0x08,0x04,0x81,0x20,0xCF,0xF7,0xFE,0xFF,0xFF}; */
/* ------------------------------------------------------------------------- */
static ssize_t rfm73_initialize(struct rfm73_data *rfm73);
/* ------------------------------------------------------------------------- */
static ssize_t rfm73_readRegsDest(struct rfm73_data *rfm73, uint8_t reg, uint8_t cnt, uint8_t *dest)
{
rfm73->tx_buf[0] = R_REGISTER | reg;
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = rfm73->rx_buf;
rfm73->t[0].len = 1;
rfm73->t[1].tx_buf = NULL;
rfm73->t[1].rx_buf = dest;
rfm73->recv = dest;
rfm73->t[1].len = cnt;
return spi_sync_transfer(rfm73->spi, rfm73->t, 2);
}
static ssize_t rfm73_readRegs(struct rfm73_data *rfm73, uint8_t reg, uint8_t cnt)
{
return rfm73_readRegsDest(rfm73, reg, cnt, rfm73->rx_buf);
}
/* set register to val*/
static ssize_t rfm73_writeReg(struct rfm73_data *rfm73, uint8_t reg, uint8_t val)
{
rfm73->tx_buf[0] = W_REGISTER | reg;
rfm73->tx_buf[1] = val;
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = NULL;
rfm73->t[0].len = 2;
return spi_sync_transfer(rfm73->spi, rfm73->t, 1);
}
static ssize_t rfm73_writeRegs(struct rfm73_data *rfm73, uint8_t reg, uint8_t *vals, uint8_t cnt)
{
rfm73->tx_buf[0] = W_REGISTER | reg;
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = NULL;
rfm73->t[0].len = 1;
rfm73->t[1].tx_buf = vals;
rfm73->t[1].rx_buf = NULL;
rfm73->t[1].len = cnt;
return spi_sync_transfer(rfm73->spi, rfm73->t, 2);
}
static ssize_t rfm73_readFifo(struct rfm73_data *rfm73, uint8_t *dest, uint8_t cnt)
{
rfm73->tx_buf[0] = R_RX_PAYLOAD;
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = rfm73->rx_buf;
rfm73->t[0].len = 1;
rfm73->t[1].tx_buf = NULL;
rfm73->t[1].rx_buf = dest;
rfm73->t[1].len = cnt;
return spi_sync_transfer(rfm73->spi, rfm73->t, 2);
}
static ssize_t rfm73_writeFifo(struct rfm73_data *rfm73, uint8_t *vals, uint8_t len)
{
#if EN_DYN_PL
rfm73->tx_buf[0] = W_TX_PAYLOAD_NOACK;
#else
rfm73->tx_buf[0] = W_TX_PAYLOAD;
#endif
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = rfm73->rx_buf;
rfm73->t[0].len = 1;
rfm73->t[1].tx_buf = vals;
rfm73->t[1].rx_buf = NULL;
rfm73->t[1].len = len;
return spi_sync_transfer(rfm73->spi, rfm73->t, 2);
}
static ssize_t rfm73_cmd(struct rfm73_data *rfm73, uint8_t cmd, uint8_t payload, uint8_t payLen, uint8_t *status)
{
rfm73->tx_buf[0] = cmd;
rfm73->tx_buf[1] = payload;
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = status;
rfm73->t[0].len = 1 + payLen;
return spi_sync_transfer(rfm73->spi, rfm73->t, 1);
}
/* ------------------------------------------------------------------------- */
/* explicit call for setting channel */
static ssize_t set_channel(struct rfm73_data *rfm73, uint8_t channel)
{
rfm73->channel = channel;
return rfm73_writeReg(rfm73, RF_CH, channel);
}
/* ------------------------------------------------------------------------- */
static void rfm73_tx_work_handler(struct work_struct *work)
{
struct rfm73_data *rfm73 = container_of(work, struct rfm73_data, tx_work);
BUG_ON(!rfm73->tx_skb);
mutex_lock(&rfm73->rf_lock);
/* we cancel receive */
gpio_set_value(rfm73->ceGpio, 0);
rfm73_writeReg(rfm73, CONFIG, DEVICE_TX);
mutex_unlock(&rfm73->rf_lock);
#if !EN_DYN_PL
skb_put_zero(rfm73->tx_skb, (rfm73->payloadLen + rfm73->addrLen) - rfm73->tx_skb->len);
#endif
rfm73_writeRegs(rfm73, TX_ADDR, rfm73->tx_skb->data, rfm73->addrLen);
rfm73_writeFifo(rfm73, rfm73->tx_skb->data + rfm73->addrLen, rfm73->tx_skb->len - rfm73->addrLen);
gpio_set_value(rfm73->ceGpio, 1);
}
static netdev_tx_t rfm73_send_packet(struct sk_buff *skb, struct net_device *dev)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
netif_stop_queue(dev);
rfm73->tx_skb = skb;
schedule_work(&rfm73->tx_work);
return NETDEV_TX_OK;
}
/* ------------------------------------------------------------------------- */
static irqreturn_t rfm73_handler(int irq, void *dev)
{
struct rfm73_data *rfm73 = (struct rfm73_data *) dev;
/*
* Can't do anything in interrupt context because we need to
* block (spi_sync() is blocking) so fire of the interrupt
* handling workqueue.
* Remember that we access rfm73 registers through SPI bus
* via spi_sync() call.
*/
schedule_work(&rfm73->irq_work);
return IRQ_HANDLED;
}
static void rfm73_irq_work_handler(struct work_struct *work)
{
struct rfm73_data *rfm73 = container_of(work, struct rfm73_data, irq_work);
struct sk_buff *skb;
uint8_t *tmp;
uint8_t status;
uint8_t pipe;
mutex_lock(&rfm73->rf_lock);
rfm73_cmd(rfm73, NOP, 0, 0, &status);
if (status & IRQ_RX_DATA_READY)
{
/* Get payload length */
#if EN_DYN_PL
rfm73_readRegsDest(rfm73, R_RX_PL_WID, 1, &rfm73->payloadLen);
#endif
pipe = (status & 0x0e) >> 1;
if (!(skb = netdev_alloc_skb(rfm73->dev, rfm73->payloadLen + rfm73->addrLen)))
{
dev_err(&rfm73->spi->dev,"memory squeeze, dropping packet\n");
rfm73->dev->stats.rx_dropped++;
}
else
{
tmp = skb_put(skb, rfm73->payloadLen + rfm73->addrLen);
if (pipe < 2)
{
rfm73_readRegsDest(rfm73, RX_ADDR_P0 + pipe, rfm73->addrLen, tmp);
}
else
{
rfm73_readRegsDest(rfm73, RX_ADDR_P1, rfm73->addrLen, tmp);
rfm73_readRegsDest(rfm73, RX_ADDR_P0 + pipe, 1, tmp);
}
/* Read the RX FIFO to skb */
rfm73_readFifo(rfm73, tmp + rfm73->addrLen, rfm73->payloadLen);
skb->pkt_type = PACKET_HOST;
skb->dev = rfm73->dev;
skb->protocol = htons(ETH_P_NONE);
skb_reset_mac_header(skb);
skb->ip_summed = CHECKSUM_UNNECESSARY;
rfm73->dev->stats.rx_bytes += skb->len;
rfm73->dev->stats.rx_packets++;
netif_receive_skb(skb);
}
}
if (status & IRQ_TX_DATA_SENT)
{
gpio_set_value(rfm73->ceGpio, 0);
rfm73->dev->stats.tx_bytes += rfm73->tx_skb->len;
rfm73->dev->stats.tx_packets++;
dev_kfree_skb_any(rfm73->tx_skb);
rfm73->tx_skb = NULL;
netif_wake_queue(rfm73->dev);
}
if (status & IRQ_MAX_RT)
{
rfm73->dev->stats.tx_dropped++;
dev_err(&rfm73->spi->dev, "Max retransmit");
}
if (status & IRQ_TX_FIFO_FULL)
{
rfm73_cmd(rfm73, FLUSH_TX, 0, 0, NULL);
dev_info(&rfm73->spi->dev, "TX FIFO flushed");
}
rfm73_writeReg(rfm73, STATUS, status & 0x70); // clear RFM70 irq flag
/* always return to RX state */
rfm73_writeReg(rfm73, CONFIG, DEVICE_RX);
gpio_set_value(rfm73->ceGpio, 1);
mutex_unlock(&rfm73->rf_lock);
}
/* ------------------------------------------------------------------------- */
static ssize_t rfm73_initialize(struct rfm73_data *rfm73)
{
uint8_t tmp = 0x53;
rfm73_readRegs(rfm73, STATUS, 1);
/* check actual bank */
if (0 == (rfm73->recv[0] & 0x80))
{
/* switch to bank1 */
rfm73_cmd(rfm73, ACTIVATE, tmp, 1, NULL);
}
rfm73_readRegs(rfm73, 0x08, 4); /* read chip id */
if (0x63 != (rfm73->recv[0]))
return -ENODEV;
/* init bank1 */
for (tmp = 0; tmp < 14; tmp++)
{
rfm73_writeRegs(rfm73, tmp, Bank1_Reg0_13[tmp], 4);
}
rfm73_writeRegs(rfm73, 0x0e, Bank1_Reg14, 11);
/* toggle to bank 0 */
tmp = 0x53;
rfm73_cmd(rfm73, ACTIVATE, tmp, 1, NULL);
rfm73_writeReg(rfm73, FEATURE, 0x01);
rfm73_readRegs(rfm73, FEATURE, 1);
if (0 == rfm73->recv[0])
{
tmp = 0x73;
rfm73_cmd(rfm73, ACTIVATE, tmp, 1, NULL);
}
rfm73_writeReg(rfm73, CONFIG, DEVICE_RX);
rfm73_writeReg(rfm73, EN_RXADDR, 0x0F);
rfm73_writeReg(rfm73, SETUP_AW, 0x02);
rfm73_writeReg(rfm73, SETUP_RETR, 0x20);
rfm73_writeReg(rfm73, RF_CH, 0x00);
rfm73_writeReg(rfm73, RF_SETUP, 0x27);
/*
rfm73_writeReg(rfm73, STATUS, 0x70);
rfm73_writeReg(rfm73, OBSERVE_TX, 0x00);
rfm73_writeReg(rfm73, CD, 0x00);
rfm73_writeReg(rfm73, FIFO_STATUS, 0x00);
*/
#if EN_DYN_PL
rfm73_writeReg(rfm73, EN_AA, 0x0F);
rfm73_writeReg(rfm73, DYNPD, 0x0F);
rfm73_writeReg(rfm73, FEATURE, 0x05);
#else
rfm73_writeReg(rfm73, EN_AA, 0x00);
/* (DST) SRC CTRL DATA CRC */
rfm73_writeReg(rfm73, RX_PW_P0, rfm73->payloadLen);
rfm73_writeReg(rfm73, RX_PW_P1, rfm73->payloadLen);
rfm73_writeReg(rfm73, RX_PW_P2, rfm73->payloadLen);
rfm73_writeReg(rfm73, RX_PW_P3, rfm73->payloadLen);
#endif
return 0;
}
static int rfm73_set_mac_address(struct net_device *dev, void *addr)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
uint8_t ptr = 2;
if (dev->flags & IFF_UP)
{
return -EBUSY;
}
memcpy(dev->dev_addr, addr + 2, 10);
rfm73_writeRegs(rfm73, RX_ADDR_P0, addr + ptr, rfm73->addrLen);
ptr += rfm73->addrLen;
rfm73_writeRegs(rfm73, RX_ADDR_P1, addr + ptr, rfm73->addrLen);
ptr += rfm73->addrLen;
rfm73_writeRegs(rfm73, RX_ADDR_P2, addr + ptr, 1);
ptr++;
rfm73_writeRegs(rfm73, RX_ADDR_P3, addr + ptr, 1);
return 0;
}
static int rfm73_open(struct net_device *dev)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
int res;
if (0 == dev->dev_addr[0])
{
rfm73_readRegsDest(rfm73, RX_ADDR_P0, rfm73->addrLen, dev->dev_addr);
res = rfm73->addrLen;
rfm73_readRegsDest(rfm73, RX_ADDR_P1, rfm73->addrLen, dev->dev_addr + res);
res += rfm73->addrLen;
rfm73_readRegsDest(rfm73, RX_ADDR_P2, 1, dev->dev_addr + res);
res += 1;
rfm73_readRegsDest(rfm73, RX_ADDR_P3, 1, dev->dev_addr + res);
}
res = request_threaded_irq(dev->irq, NULL, rfm73_handler, IRQF_ONESHOT | IRQF_TRIGGER_FALLING, "rfm73", rfm73);
if (res)
{
dev_err(&rfm73->spi->dev,"open return %d",res);
}
else
{
rfm73_cmd(rfm73, FLUSH_RX, 0, 0, NULL);
rfm73_cmd(rfm73, FLUSH_TX, 0, 0, NULL);
rfm73_writeReg(rfm73, CONFIG, DEVICE_RX);
gpio_set_value(rfm73->ceGpio, 1);
netif_start_queue(dev);
}
return res;
}
static int rfm73_stop(struct net_device *dev)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
gpio_set_value(rfm73->ceGpio, 0);
netif_stop_queue(dev);
free_irq(dev->irq, rfm73);
if (rfm73->tx_skb)
{
dev_kfree_skb(rfm73->tx_skb);
}
return 0;
}
static int rfm73_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
switch (cmd) {
case GET_CHANNEL:
ifr->ifr_flags = rfm73->channel;
break;
case SET_CHANNEL:
set_channel(rfm73, ifr->ifr_flags);
break;
case GET_REG:
rfm73_readRegs(rfm73, ifr->ifr_flags, 1);
ifr->ifr_flags = rfm73->recv[0];
break;
default:
break;
}
return 0;
}
static struct net_device_stats *rfm73_stats(struct net_device *dev)
{
return &dev->stats;
}
static const struct net_device_ops netdev_ops = {
.ndo_open = rfm73_open,
.ndo_stop = rfm73_stop,
.ndo_start_xmit = rfm73_send_packet,
.ndo_do_ioctl = rfm73_ioctl,
.ndo_set_mac_address = rfm73_set_mac_address,
.ndo_get_stats = rfm73_stats,
};
/*-------------------------------------------------------------------------*/
static void dev_setup(struct net_device *dev)
{
struct rfm73_data *rfm73 = netdev_priv(dev);
dev->netdev_ops = &netdev_ops;
dev->flags |= IFF_NOARP | IFF_POINTOPOINT;
dev->features |= NETIF_F_HW_CSUM;
dev->type = ARPHRD_NONE;
dev->mtu = 36; /* rfm73->addrLen + rfm73->payloadLen */
dev->addr_len = 10; /* 4B unicast, 4B broadcast, 2x1B multicast*/
dev->tx_queue_len = 100;
rfm73->dev = dev;
}
static int rfm73_probe(struct spi_device *spi)
{
struct net_device *dev;
struct rfm73_data *rfm73;
struct device_node *on = spi->dev.of_node;
int gpioIrq;
int gpioCe;
int status = 0;
//get params from device tree
if (!on)
{
dev_err(&spi->dev,"No rfm73 device node found in device tree.\n");
return -ENODEV;
}
gpioIrq = of_get_named_gpio(on, "rfm73,irq", 0);
gpioCe = of_get_named_gpio(on, "rfm73,ce", 0);
if (gpioIrq == -EPROBE_DEFER || gpioCe == -EPROBE_DEFER)
{
return -EPROBE_DEFER;
}
if (gpioIrq < 0 || gpioCe < 0)
{
dev_err(&spi->dev, "No gpio-irq or gpio-ce in device tree node.\n");
return -ENXIO;
}
if (gpio_is_valid(gpioIrq))
{
if (devm_gpio_request_one(&spi->dev, gpioIrq, GPIOF_IN, "rfm73 irq"))
{
dev_err(&spi->dev, "gpio irq request failed.\n");
return -ENXIO;
}
}
spi->irq = gpio_to_irq(gpioIrq);
if (spi->irq < 0)
{
dev_err(&spi->dev,"Invalid irq for gpio-irq.\n");
return -ENXIO;
}
if (gpio_is_valid(gpioCe))
{
if (devm_gpio_request_one(&spi->dev, gpioCe, GPIOF_OUT_INIT_LOW, "rfm73 ce"))
{
dev_err(&spi->dev, "gpio ce request failed.\n");
return -ENXIO;
}
}
/* Allocate driver data */
dev = alloc_netdev(sizeof(struct rfm73_data), "rf%d", NET_NAME_ENUM, dev_setup);
if (!dev)
{
dev_err(&spi->dev,"Allocate net dev failed.\n");
return -ENOMEM;
}
rfm73 = netdev_priv(dev);
rfm73->spi = spi;
spi_set_drvdata(spi, rfm73);
dev->irq = spi->irq;
rfm73->ceGpio = gpioCe;
rfm73->irqGpio = gpioIrq;
/* predefined */
rfm73->addrLen = 4;
rfm73->payloadLen = 32;
// spi init
gpio_set_value(rfm73->ceGpio, 0);
spi = spi_dev_get(rfm73->spi);
if (spi == NULL)
{
return -ESHUTDOWN;
}
status = spi_setup(spi);
spi_dev_put(spi);
memset(&rfm73->t[0], 0, sizeof(struct spi_transfer));
memset(&rfm73->t[1], 0, sizeof(struct spi_transfer));
rfm73->t[0].tx_buf = rfm73->tx_buf;
rfm73->t[0].rx_buf = NULL;
rfm73->t[0].delay_usecs = 0;
rfm73->t[1].tx_buf = NULL;
rfm73->t[1].rx_buf = rfm73->rx_buf;
rfm73->t[1].delay_usecs = 0;
mutex_init(&rfm73->rf_lock);
INIT_WORK(&rfm73->irq_work, rfm73_irq_work_handler);
INIT_WORK(&rfm73->tx_work, rfm73_tx_work_handler);
status = rfm73_initialize(rfm73);
if (status != 0)
{
dev_err(&spi->dev,"RFM73 error (%d)\n", status);
free_netdev(rfm73->dev);
rfm73->spi = NULL;
spi_set_drvdata(spi, NULL);
devm_gpio_free(&spi->dev, rfm73->irqGpio);
devm_gpio_free(&spi->dev, rfm73->ceGpio);
}
else
{
status = register_netdev(dev);
dev_info(&spi->dev,"RFM Version 1.5\n");
}
return status;
}
static int __exit_call rfm73_remove(struct spi_device *spi)
{
struct rfm73_data *rfm73 = spi_get_drvdata(spi);
gpio_set_value(rfm73->ceGpio, 0);
unregister_netdev(rfm73->dev);
free_netdev(rfm73->dev);
rfm73->spi = NULL;
spi_set_drvdata(spi, NULL);
devm_gpio_free(&spi->dev, rfm73->irqGpio);
devm_gpio_free(&spi->dev, rfm73->ceGpio);
return 0;
}
static int __maybe_unused rfm73_suspend(struct device *dev)
{
struct rfm73_data *rfm73 = dev_get_drvdata(dev);
if (netif_running(rfm73->dev))
{
// TODO: set sleep mode
}
return 0;
}
static int __maybe_unused rfm73_resume(struct device *dev)
{
struct rfm73_data *rfm73 = dev_get_drvdata(dev);
//FIXME: if the interface is opened
if (netif_running(rfm73->dev))
{
// TODO: exit sleep mode
}
return 0;
}
static SIMPLE_DEV_PM_OPS(rfm73_pm, rfm73_suspend, rfm73_resume);
static const struct of_device_id rfm73_dt_ids[] = {
{ .compatible = "hoperf,rfm73" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, rfm73_dt_ids);
static struct spi_driver rfm73_spi_driver = {
.driver = {
.name = "rfm73",
.pm = &rfm73_pm,
.of_match_table = rfm73_dt_ids,
},
.probe = rfm73_probe,
.remove = rfm73_remove,
};
static int __init rfm73_init(void)
{
return spi_register_driver(&rfm73_spi_driver);
}
module_init(rfm73_init);
static void __exit rfm73_exit(void)
{
spi_unregister_driver(&rfm73_spi_driver);
}
module_exit(rfm73_exit);
MODULE_AUTHOR("Jiri Novotny, <jiri.novotny@logicelements.cz>");
MODULE_DESCRIPTION("HopeRF RFM73 SPI device interface");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:rfm73");