forked from Jaffe-/nRF905
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnrf905.c
829 lines (598 loc) · 22.3 KB
/
nrf905.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spi/spi.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/uaccess.h>
#include <linux/of_gpio.h>
#include <linux/poll.h>
static struct class *nrf905_class = NULL;
static struct device *nrf905_device = NULL;
static int nrf905_major;
struct buffer {
uint8_t (*data)[32];
int rpos;
int wpos;
size_t size;
};
struct nrf905_drvdata {
dev_t devt;
spinlock_t spi_lock;
struct spi_device *spi;
struct list_head device_entry;
unsigned users;
int gpio_pwr_up;
int gpio_trx_ce;
int gpio_tx_en;
int gpio_dr;
wait_queue_head_t inq;
enum { NRF905_MODE_OFF, NRF905_MODE_RX, NRF905_MODE_TX } mode;
struct buffer buf;
int listen;
};
static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);
static DEFINE_MUTEX(nrf905_cdev_lock);
static DECLARE_WAIT_QUEUE_HEAD(nrf905_cdev_wq);
/* ************************************************************************** *
* Low-level communication with the chip *
* ************************************************************************** */
static int nrf905_spi_sync(struct spi_device *spi, uint8_t *txbytes, uint8_t *rxbytes, uint8_t n) {
struct spi_message msg;
struct spi_transfer xfer;
spi_message_init(&msg);
memset(&xfer, 0, sizeof(xfer));
xfer.tx_buf = txbytes;
xfer.rx_buf = rxbytes;
xfer.len = n;
xfer.bits_per_word = 0;
spi_message_add_tail(&xfer, &msg);
return spi_sync(spi, &msg);
}
static void nrf905_spi_w_config(struct spi_device *spi, uint8_t address, uint8_t byte) {
uint8_t txbytes[2];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = address & 0x0F;
txbytes[1] = byte;
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
}
static uint8_t nrf905_spi_r_config(struct spi_device *spi, uint8_t address) {
uint8_t txbytes[2];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = (address & 0x0F) | 0x10;
txbytes[1] = 0xFF; // dummy byte for reading the response
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
return rxbytes[1];
}
static void nrf905_spi_w_tx_payload(struct spi_device *spi, const uint8_t *payload, uint8_t count) {
uint8_t txbytes[count + 1];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = 0x20;
memcpy(&txbytes[1], payload, count);
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
}
static void nrf905_spi_w_tx_address(struct spi_device *spi, const uint8_t *address, size_t count) {
uint8_t txbytes[5];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = 0x22;
memcpy(&txbytes[1], address, count);
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
}
static void nrf905_spi_r_tx_address(struct spi_device *spi, uint8_t *address, size_t count) {
uint8_t txbytes[5];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = 0x23;
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
memcpy(address, &rxbytes[1], count);
}
static void nrf905_spi_r_rx_payload(struct spi_device *spi, uint8_t *payload, uint8_t count) {
uint8_t txbytes[33];
uint8_t rxbytes[sizeof(txbytes)];
txbytes[0] = 0x24;
nrf905_spi_sync(spi, txbytes, rxbytes, sizeof(txbytes));
memcpy(payload, &rxbytes[1], count);
}
static void nrf905_spi_w_rx_address(struct spi_device *spi, const uint8_t *address, size_t count) {
int i;
for (i = 0; i < count; i++) {
nrf905_spi_w_config(spi, 5 + i, address[i]);
}
}
static void nrf905_spi_r_rx_address(struct spi_device *spi, uint8_t *address, size_t count) {
int i;
for (i = 0; i < count; i++) {
address[i] = nrf905_spi_r_config(spi, 5 + i);
}
}
/* ************************************************************************** *
* Buffer functions *
* ************************************************************************** */
static void nrf905_buf_write(struct buffer *buf, uint8_t *src)
{
memcpy(buf->data[buf->wpos], src, 32);
if (++buf->wpos == buf->size)
buf->wpos = 0;
}
static void nrf905_buf_read(struct buffer *buf, uint8_t *dst)
{
memcpy(dst, buf->data[buf->rpos], 32);
if (++buf->rpos == buf->size)
buf->rpos = 0;
}
static int nrf905_buf_readable(const struct buffer *buf)
{
return buf->rpos != buf->wpos;
}
static int nrf905_buf_spacefree(const struct buffer *buf)
{
if (buf->rpos == buf->wpos)
return buf->size - 1;
else
return ((buf->rpos + buf->size - buf->wpos) % buf->size) - 1;
}
/* ************************************************************************** *
* Sysfs attributes *
* ************************************************************************** */
/** @brief Write handler for the tx_address sysfs attribute */
static ssize_t nrf905_attr_tx_address_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
struct spi_device *spi = to_spi_device(dev);
if (count != 4) {
return -EINVAL;
}
nrf905_spi_w_tx_address(spi, buf, count);
return count;
}
/** @brief Read handler for the tx_address sysfs attribute */
static ssize_t nrf905_attr_tx_address_read(struct device *dev, struct device_attribute *attr, char *buf) {
struct spi_device *spi = to_spi_device(dev);
nrf905_spi_r_tx_address(spi, buf, 4);
return 4;
}
/** @brief Device attribute for configuring the tx address */
static DEVICE_ATTR(tx_address, 0664, nrf905_attr_tx_address_read, nrf905_attr_tx_address_write);
/** @brief Write handler for the rx_address sysfs attribute */
static ssize_t nrf905_attr_rx_address_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
struct spi_device *spi = to_spi_device(dev);
if ((count < 1) || (count > 4)) {
return -EINVAL;
}
nrf905_spi_w_rx_address(spi, buf, count);
return count;
}
/** @brief Read handler for the rx_address sysfs attribute */
static ssize_t nrf905_attr_rx_address_read(struct device *dev, struct device_attribute *attr, char *buf) {
struct spi_device *spi = to_spi_device(dev);
nrf905_spi_r_rx_address(spi, buf, 4);
return 4;
}
/** @brief Device attribute for configuring the rx address */
static DEVICE_ATTR(rx_address, 0664, nrf905_attr_rx_address_read, nrf905_attr_rx_address_write);
/** @brief Write handler for the frequency sysfs attribute */
static ssize_t nrf905_attr_frequency_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
struct spi_device *spi = to_spi_device(dev);
uint32_t frequency;
uint8_t hfreq_pll;
uint16_t channel;
uint8_t config[2];
if (sscanf(buf, "%u", &frequency) != 1) {
return -EINVAL;
}
if ((frequency >= 422400) && (frequency < 473600)) {
hfreq_pll = 0;
// round to 0.1MHz
frequency = (frequency / 100) * 100;
channel = (frequency - 422400) / 100;
} else if ((frequency >= 844800) && (frequency < 947200)) {
hfreq_pll = 1;
// round to 0.2MHz
frequency = (frequency / 200) * 200;
channel = (frequency - 844800) / 200;
} else {
return -EINVAL;
}
dev_info(dev, "frequency: %u\n", frequency);
dev_info(dev, "hfreq_pll: %hhu\n", hfreq_pll);
dev_info(dev, "channel: %hu\n", channel);
if (channel > 0x1FF) {
dev_warn(dev, "channel %hu out of range!\n", channel);
return -EINVAL;
}
config[0] = nrf905_spi_r_config(spi, 0);
config[1] = nrf905_spi_r_config(spi, 1);
config[0] = (config[0] & 0x00) | (channel & 0x00FF);
config[1] = (config[1] & 0xFC) | ((channel & 0xFF00) >> 8) | (hfreq_pll << 1);
nrf905_spi_w_config(spi, 0, config[0]);
nrf905_spi_w_config(spi, 1, config[1]);
return count;
}
/** @brief Read handler for the frequency sysfs attribute */
static ssize_t nrf905_attr_frequency_read(struct device *dev, struct device_attribute *attr, char *buf) {
struct spi_device *spi = to_spi_device(dev);
uint32_t frequency;
uint16_t channel;
uint8_t hfreq_pll;
uint8_t config[2];
config[0] = nrf905_spi_r_config(spi, 0);
config[1] = nrf905_spi_r_config(spi, 1);
channel = config[0] + ((config[1] & 0x01) << 8);
hfreq_pll = (config[1] & 0x02) >> 1;
frequency = (422400 + channel * 100) * (1 + hfreq_pll);
return sprintf(buf, "%u", frequency);
}
/** @brief Device attribute for configuring the frequency (ch_no + hfreq_pll together) */
static DEVICE_ATTR(frequency, 0664, nrf905_attr_frequency_read, nrf905_attr_frequency_write);
/** @brief Write handler for the pa_pwr sysfs attribute */
static ssize_t nrf905_attr_pa_pwr_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
struct spi_device *spi = to_spi_device(dev);
uint8_t config;
uint8_t pa_pwr;
if (sscanf(buf, "%hhu", &pa_pwr) != 1) {
return -EINVAL;
}
dev_info(dev, "pa_pwr: %hhu\n", pa_pwr);
if (pa_pwr > 3) {
dev_warn(dev, "pa_pwr %hhu out of range!\n", pa_pwr);
return -EINVAL;
}
config = nrf905_spi_r_config(spi, 1);
config = (config & 0xF3) | (pa_pwr << 2);
nrf905_spi_w_config(spi, 1, config);
return count;
}
/** @brief Read handler for the pa_pwr sysfs attribute */
static ssize_t nrf905_attr_pa_pwr_read(struct device *dev, struct device_attribute *attr, char *buf) {
struct spi_device *spi = to_spi_device(dev);
uint8_t config;
uint8_t pa_pwr;
config = nrf905_spi_r_config(spi, 1);
pa_pwr = (config & 0x0C) >> 2;
return sprintf(buf, "%hhu", pa_pwr);
}
/** @brief Device attribute for configuring the power amplifier */
static DEVICE_ATTR(pa_pwr, 0664, nrf905_attr_pa_pwr_read, nrf905_attr_pa_pwr_write);
/** @brief Write handler for the listen sysfs attribute **/
static ssize_t nrf905_attr_listen_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
struct nrf905_drvdata *drvdata = dev_get_drvdata(dev);
if (count != 1) {
return -EINVAL;
}
mutex_lock(&nrf905_cdev_lock);
// If changing from passive to listening, immediately set mode to RX
if (!drvdata->listen && *buf == '1') {
drvdata->listen = 1;
gpio_set_value(drvdata->gpio_trx_ce, 1);
drvdata->mode = NRF905_MODE_RX;
} else if (drvdata->listen && *buf == '0') {
drvdata->listen = 0;
// If changing from listening to passive, make sure that no readers are waiting
if (!waitqueue_active(&drvdata->inq)) {
gpio_set_value(drvdata->gpio_trx_ce, 0);
drvdata->mode = NRF905_MODE_OFF;
}
}
mutex_unlock(&nrf905_cdev_lock);
return count;
}
/** @brief Read handler for the listen sysfs attribute */
static ssize_t nrf905_attr_listen_read(struct device *dev, struct device_attribute *attr, char *buf) {
struct nrf905_drvdata *drvdata = dev_get_drvdata(dev);
return sprintf(buf, "%d", drvdata->listen);
}
/** @brief Device attribute for configuring listening **/
static DEVICE_ATTR(listen, 0664, nrf905_attr_listen_read, nrf905_attr_listen_write);
/** @brief An array containing all the sysfs attributes */
static struct attribute *nrf905_attributes[] = {
&dev_attr_rx_address.attr,
&dev_attr_tx_address.attr,
&dev_attr_frequency.attr,
&dev_attr_pa_pwr.attr,
&dev_attr_listen.attr,
NULL,
};
/** @brief The attribute group for creating and destroying all the sysfs attributes at once instead of one at a time */
static struct attribute_group nrf905_attribute_group = {
.attrs = nrf905_attributes
};
static irqreturn_t dr_irq(int irq, void *dev_id) {
struct nrf905_drvdata *drvdata = dev_id;
uint8_t rxbuf[32];
int value = gpio_get_value(drvdata->gpio_dr);
if (value) {
if (drvdata->mode == NRF905_MODE_TX) {
wake_up_interruptible(&nrf905_cdev_wq);
} else {
mutex_lock(&nrf905_cdev_lock);
nrf905_spi_r_rx_payload(drvdata->spi, rxbuf, 32);
if (drvdata->mode == NRF905_MODE_RX && nrf905_buf_spacefree(&drvdata->buf)) {
nrf905_buf_write(&drvdata->buf, rxbuf);
mutex_unlock(&nrf905_cdev_lock);
wake_up_interruptible(&drvdata->inq);
} else {
mutex_unlock(&nrf905_cdev_lock);
}
}
}
return IRQ_HANDLED;
}
static unsigned int nrf905_cdev_poll(struct file *filp, poll_table *wait) {
struct nrf905_drvdata *drvdata = filp->private_data;
unsigned int mask = POLLOUT | POLLWRNORM;
mutex_lock(&nrf905_cdev_lock);
poll_wait(filp, &drvdata->inq, wait);
if (nrf905_buf_readable(&drvdata->buf))
mask |= POLLIN | POLLRDNORM;
mutex_unlock(&nrf905_cdev_lock);
return mask;
}
static ssize_t nrf905_cdev_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos) {
struct nrf905_drvdata *drvdata = filp->private_data;
long status;
uint8_t rxbuf[32];
if (count != 32) {
return -EMSGSIZE;
}
mutex_lock(&nrf905_cdev_lock);
if (!drvdata->listen) {
gpio_set_value(drvdata->gpio_trx_ce, 1);
drvdata->mode = NRF905_MODE_RX;
}
while (!nrf905_buf_readable(&drvdata->buf)) {
mutex_unlock(&nrf905_cdev_lock);
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(drvdata->inq, nrf905_buf_readable(&drvdata->buf)))
return -ERESTARTSYS;
mutex_lock(&nrf905_cdev_lock);
}
nrf905_buf_read(&drvdata->buf, rxbuf);
if (!drvdata->listen) {
gpio_set_value(drvdata->gpio_trx_ce, 0);
drvdata->mode = NRF905_MODE_OFF;
}
mutex_unlock(&nrf905_cdev_lock);
status = copy_to_user(buf, rxbuf, count);
if (status < 0) {
return status;
} else {
return count - status;
}
}
static ssize_t nrf905_cdev_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos) {
struct nrf905_drvdata *drvdata = filp->private_data;
long status;
uint8_t txbuf[32];
if (count != 32) {
return -EMSGSIZE;
}
status = copy_from_user(txbuf, buf, count);
if (status < 0) {
return -EINVAL;
}
mutex_lock(&nrf905_cdev_lock);
nrf905_spi_w_tx_payload(drvdata->spi, txbuf, count - status);
gpio_set_value(drvdata->gpio_tx_en, 1);
if (drvdata->mode == NRF905_MODE_OFF) {
gpio_set_value(drvdata->gpio_trx_ce, 1);
}
drvdata->mode = NRF905_MODE_TX;
if (wait_event_interruptible(nrf905_cdev_wq, gpio_get_value(drvdata->gpio_dr))) {
gpio_set_value(drvdata->gpio_trx_ce, 0);
mutex_unlock(&nrf905_cdev_lock);
return -ERESTARTSYS;
}
gpio_set_value(drvdata->gpio_tx_en, 0);
if (!drvdata->listen) {
gpio_set_value(drvdata->gpio_trx_ce, 0);
drvdata->mode = NRF905_MODE_OFF;
} else {
drvdata->mode = NRF905_MODE_RX;
}
mutex_unlock(&nrf905_cdev_lock);
return count - status;
}
static int nrf905_cdev_open(struct inode *inode, struct file *filp) {
struct nrf905_drvdata *drvdata;
int err = -ENXIO;
mutex_lock(&device_list_lock);
list_for_each_entry(drvdata, &device_list, device_entry) {
if (drvdata->devt == inode->i_rdev) {
err = 0;
break;
}
}
if (err) {
dev_warn(&drvdata->spi->dev, "nothing for minor %d\n", iminor(inode));
goto err_find_dev;
}
drvdata->users++;
filp->private_data = drvdata;
nonseekable_open(inode, filp);
mutex_unlock(&device_list_lock);
return err;
err_find_dev:
mutex_unlock(&device_list_lock);
return err;
}
static int nrf905_cdev_release(struct inode *inode, struct file *filp) {
struct nrf905_drvdata *drvdata;
mutex_lock(&device_list_lock);
drvdata = filp->private_data;
filp->private_data = NULL;
drvdata->users--;
if (!drvdata->listen) {
mutex_lock(&nrf905_cdev_lock);
gpio_set_value(drvdata->gpio_trx_ce, 0);
drvdata->mode = NRF905_MODE_OFF;
mutex_unlock(&nrf905_cdev_lock);
}
if (!drvdata->users) {
int dofree;
spin_lock_irq(&drvdata->spi_lock);
dofree = (drvdata->spi == NULL);
spin_unlock_irq(&drvdata->spi_lock);
if (dofree) {
kfree(drvdata);
}
}
mutex_unlock(&device_list_lock);
return 0;
}
/** @brief Character device file operations */
static struct file_operations nrf905_fops = {
.owner = THIS_MODULE,
.write = nrf905_cdev_write,
.read = nrf905_cdev_read,
.open = nrf905_cdev_open,
.release = nrf905_cdev_release,
.poll = nrf905_cdev_poll,
};
static int nrf905_probe(struct spi_device *spi) {
struct nrf905_drvdata *drvdata;
int err;
dev_info(&spi->dev, "%s\n", __func__);
drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
if (!drvdata) {
return -ENOMEM;
}
// Allocate read buffer
drvdata->buf.size = 32;
drvdata->buf.rpos = 0;
drvdata->buf.wpos = 0;
drvdata->buf.data = kzalloc(sizeof(uint8_t) * 32 * drvdata->buf.size, GFP_KERNEL);
if (!drvdata->buf.data) {
return -ENOMEM;
}
drvdata->gpio_pwr_up = of_get_named_gpio(spi->dev.of_node, "pwr_up_gpio", 0);
drvdata->gpio_trx_ce = of_get_named_gpio(spi->dev.of_node, "trx_ce_gpio", 0);
drvdata->gpio_tx_en = of_get_named_gpio(spi->dev.of_node, "tx_en_gpio", 0);
drvdata->gpio_dr = of_get_named_gpio(spi->dev.of_node, "dr_gpio", 0);
dev_info(&spi->dev, "gpio_pwr_up: %d\n", drvdata->gpio_pwr_up);
dev_info(&spi->dev, "gpio_trx_ce: %d\n", drvdata->gpio_trx_ce);
dev_info(&spi->dev, "gpio_tx_en: %d\n", drvdata->gpio_tx_en);
dev_info(&spi->dev, "gpio_dr: %d\n", drvdata->gpio_dr);
err = gpio_request_one(drvdata->gpio_pwr_up, GPIOF_DIR_OUT | GPIOF_INIT_LOW, "pwr_up");
if (err < 0) {
dev_err(&spi->dev, "could not obtain pwr_up pin %d\n", drvdata->gpio_pwr_up);
return err;
}
err = gpio_request_one(drvdata->gpio_trx_ce, GPIOF_DIR_OUT | GPIOF_INIT_LOW, "trx_ce");
if (err < 0) {
dev_err(&spi->dev, "could not obtain trx_ce pin %d\n", drvdata->gpio_trx_ce);
return err;
}
err = gpio_request_one(drvdata->gpio_tx_en, GPIOF_DIR_OUT | GPIOF_INIT_LOW, "tx_en");
if (err < 0) {
dev_err(&spi->dev, "could not obtain tx_en pin %d\n", drvdata->gpio_tx_en);
return err;
}
err = gpio_request_one(drvdata->gpio_dr, GPIOF_DIR_IN, "dr");
if (err < 0) {
dev_err(&spi->dev, "could not obtain dr pin %d\n", drvdata->gpio_dr);
return err;
}
err = request_irq(gpio_to_irq(drvdata->gpio_dr), dr_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "dr_irq", drvdata);
if (err < 0) {
dev_err(&spi->dev, "unable to request irq for dr pin %d\n", drvdata->gpio_dr);
return err;
}
gpio_set_value(drvdata->gpio_pwr_up, 0);
// transition to PWR_DWN is not specified in the datasheet
// 10ms should be more than enough, considering that the wakeup time is 3ms
msleep(10);
gpio_set_value(drvdata->gpio_pwr_up, 1);
// Default to non-listening and off mode
gpio_set_value(drvdata->gpio_trx_ce, 0);
gpio_set_value(drvdata->gpio_tx_en, 0);
drvdata->listen = 0;
drvdata->mode = NRF905_MODE_OFF;
init_waitqueue_head(&drvdata->inq);
// transition PWR_DWN -> ST_BY mode, i.e. wake up
msleep(3);
spi->master->mode_bits = SPI_MODE_0;
spi->mode = SPI_MODE_0;
spi->bits_per_word = 8;
err = spi_setup(spi);
if (err < 0) {
return err;
}
// crystal oscillator frequency = 16MHz
nrf905_spi_w_config(spi, 9, 0xDF);
drvdata->spi = spi;
spin_lock_init(&drvdata->spi_lock);
INIT_LIST_HEAD(&drvdata->device_entry);
mutex_lock(&device_list_lock);
/* Create /dev and sysfs devices */
drvdata->devt = MKDEV(nrf905_major, 0);
nrf905_device = device_create(nrf905_class, &spi->dev, drvdata->devt, drvdata, "nrf905");
if (IS_ERR(nrf905_device)) {
err = PTR_ERR(nrf905_device);
dev_err(&spi->dev, "Error %d while trying to create %s\n", err, "nrf905");
return err;
} else {
list_add(&drvdata->device_entry, &device_list);
}
mutex_unlock(&device_list_lock);
spi_set_drvdata(spi, drvdata);
err = sysfs_create_group(&spi->dev.kobj, &nrf905_attribute_group);
return 0;
}
static int nrf905_remove(struct spi_device *spi) {
struct nrf905_drvdata *drvdata = spi_get_drvdata(spi);
dev_info(&spi->dev, "%s\n", __func__);
spin_lock_irq(&drvdata->spi_lock);
drvdata->spi = NULL;
spin_unlock_irq(&drvdata->spi_lock);
mutex_lock(&device_list_lock);
list_del(&drvdata->device_entry);
sysfs_remove_group(&spi->dev.kobj, &nrf905_attribute_group);
device_destroy(nrf905_class, nrf905_device->devt);
mutex_unlock(&device_list_lock);
gpio_direction_output(drvdata->gpio_dr, 0);
free_irq(gpio_to_irq(drvdata->gpio_dr), drvdata);
gpio_free(drvdata->gpio_dr);
gpio_direction_output(drvdata->gpio_tx_en, 0);
gpio_free(drvdata->gpio_tx_en);
gpio_direction_output(drvdata->gpio_trx_ce, 0);
gpio_free(drvdata->gpio_trx_ce);
gpio_direction_output(drvdata->gpio_pwr_up, 0);
gpio_free(drvdata->gpio_pwr_up);
return 0;
}
static struct spi_driver nrf905_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "nrf905",
},
.probe = nrf905_probe,
.remove = __exit_p(nrf905_remove),
};
int nrf905_init(void) {
pr_info("%s\n", __func__);
nrf905_class = class_create(THIS_MODULE, "nrf905");
if (IS_ERR(nrf905_class)) {
pr_err("failed to register device class '%s'\n", "nrf905");
return PTR_ERR(nrf905_class);
}
nrf905_major = register_chrdev(0, "nrf905", &nrf905_fops);
if (nrf905_major < 0) {
return nrf905_major;
}
return spi_register_driver(&nrf905_driver);
}
void nrf905_exit(void) {
pr_info("%s\n", __func__);
spi_unregister_driver(&nrf905_driver);
unregister_chrdev(nrf905_major, "nrf905");
class_destroy(nrf905_class);
class_unregister(nrf905_class);
}
module_init(nrf905_init);
module_exit(nrf905_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andrija Prčić <[email protected]>");
MODULE_DESCRIPTION("Simple driver for the Nordic nRF905 chip");