blob: 788adb654f32d17ce5336f7da4367de84f379b14 [file] [log] [blame]
Alan Ott3731a332012-09-02 15:44:13 +00001/*
2 * Driver for Microchip MRF24J40 802.15.4 Wireless-PAN Networking controller
3 *
4 * Copyright (C) 2012 Alan Ott <alan@signal11.us>
5 * Signal 11 Software
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Alan Ott3731a332012-09-02 15:44:13 +000016 */
17
18#include <linux/spi/spi.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
Alexander Aringb0156792015-09-21 11:24:30 +020021#include <linux/regmap.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020022#include <linux/ieee802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020023#include <net/cfg802154.h>
Alan Ott3731a332012-09-02 15:44:13 +000024#include <net/mac802154.h>
25
26/* MRF24J40 Short Address Registers */
Alexander Aringc9f883f2015-09-21 11:24:22 +020027#define REG_RXMCR 0x00 /* Receive MAC control */
28#define REG_PANIDL 0x01 /* PAN ID (low) */
29#define REG_PANIDH 0x02 /* PAN ID (high) */
30#define REG_SADRL 0x03 /* Short address (low) */
31#define REG_SADRH 0x04 /* Short address (high) */
32#define REG_EADR0 0x05 /* Long address (low) (high is EADR7) */
Alexander Aring554b4942015-09-21 11:24:29 +020033#define REG_EADR1 0x06
34#define REG_EADR2 0x07
35#define REG_EADR3 0x08
36#define REG_EADR4 0x09
37#define REG_EADR5 0x0A
38#define REG_EADR6 0x0B
39#define REG_EADR7 0x0C
40#define REG_RXFLUSH 0x0D
41#define REG_ORDER 0x10
Alexander Aringc9f883f2015-09-21 11:24:22 +020042#define REG_TXMCR 0x11 /* Transmit MAC control */
Alexander Aring554b4942015-09-21 11:24:29 +020043#define REG_ACKTMOUT 0x12
44#define REG_ESLOTG1 0x13
45#define REG_SYMTICKL 0x14
46#define REG_SYMTICKH 0x15
Alexander Aringc9f883f2015-09-21 11:24:22 +020047#define REG_PACON0 0x16 /* Power Amplifier Control */
48#define REG_PACON1 0x17 /* Power Amplifier Control */
49#define REG_PACON2 0x18 /* Power Amplifier Control */
Alexander Aring554b4942015-09-21 11:24:29 +020050#define REG_TXBCON0 0x1A
Alexander Aringc9f883f2015-09-21 11:24:22 +020051#define REG_TXNCON 0x1B /* Transmit Normal FIFO Control */
Alexander Aring554b4942015-09-21 11:24:29 +020052#define REG_TXG1CON 0x1C
53#define REG_TXG2CON 0x1D
54#define REG_ESLOTG23 0x1E
55#define REG_ESLOTG45 0x1F
56#define REG_ESLOTG67 0x20
57#define REG_TXPEND 0x21
58#define REG_WAKECON 0x22
59#define REG_FROMOFFSET 0x23
Alexander Aringc9f883f2015-09-21 11:24:22 +020060#define REG_TXSTAT 0x24 /* TX MAC Status Register */
Alexander Aring554b4942015-09-21 11:24:29 +020061#define REG_TXBCON1 0x25
62#define REG_GATECLK 0x26
63#define REG_TXTIME 0x27
64#define REG_HSYMTMRL 0x28
65#define REG_HSYMTMRH 0x29
Alexander Aringc9f883f2015-09-21 11:24:22 +020066#define REG_SOFTRST 0x2A /* Soft Reset */
Alexander Aring554b4942015-09-21 11:24:29 +020067#define REG_SECCON0 0x2C
68#define REG_SECCON1 0x2D
Alexander Aringc9f883f2015-09-21 11:24:22 +020069#define REG_TXSTBL 0x2E /* TX Stabilization */
Alexander Aring554b4942015-09-21 11:24:29 +020070#define REG_RXSR 0x30
Alexander Aringc9f883f2015-09-21 11:24:22 +020071#define REG_INTSTAT 0x31 /* Interrupt Status */
72#define REG_INTCON 0x32 /* Interrupt Control */
73#define REG_GPIO 0x33 /* GPIO */
74#define REG_TRISGPIO 0x34 /* GPIO direction */
Alexander Aring554b4942015-09-21 11:24:29 +020075#define REG_SLPACK 0x35
Alexander Aringc9f883f2015-09-21 11:24:22 +020076#define REG_RFCTL 0x36 /* RF Control Mode Register */
Alexander Aring554b4942015-09-21 11:24:29 +020077#define REG_SECCR2 0x37
78#define REG_BBREG0 0x38
Alexander Aringc9f883f2015-09-21 11:24:22 +020079#define REG_BBREG1 0x39 /* Baseband Registers */
80#define REG_BBREG2 0x3A /* */
Alexander Aring554b4942015-09-21 11:24:29 +020081#define REG_BBREG3 0x3B
82#define REG_BBREG4 0x3C
Alexander Aringc9f883f2015-09-21 11:24:22 +020083#define REG_BBREG6 0x3E /* */
84#define REG_CCAEDTH 0x3F /* Energy Detection Threshold */
Alan Ott3731a332012-09-02 15:44:13 +000085
86/* MRF24J40 Long Address Registers */
Alexander Aringc9f883f2015-09-21 11:24:22 +020087#define REG_RFCON0 0x200 /* RF Control Registers */
88#define REG_RFCON1 0x201
89#define REG_RFCON2 0x202
90#define REG_RFCON3 0x203
91#define REG_RFCON5 0x205
92#define REG_RFCON6 0x206
93#define REG_RFCON7 0x207
94#define REG_RFCON8 0x208
Alexander Aring554b4942015-09-21 11:24:29 +020095#define REG_SLPCAL0 0x209
96#define REG_SLPCAL1 0x20A
97#define REG_SLPCAL2 0x20B
98#define REG_RFSTATE 0x20F
Alexander Aringc9f883f2015-09-21 11:24:22 +020099#define REG_RSSI 0x210
100#define REG_SLPCON0 0x211 /* Sleep Clock Control Registers */
101#define REG_SLPCON1 0x220
102#define REG_WAKETIMEL 0x222 /* Wake-up Time Match Value Low */
103#define REG_WAKETIMEH 0x223 /* Wake-up Time Match Value High */
Alexander Aring554b4942015-09-21 11:24:29 +0200104#define REG_REMCNTL 0x224
105#define REG_REMCNTH 0x225
106#define REG_MAINCNT0 0x226
107#define REG_MAINCNT1 0x227
108#define REG_MAINCNT2 0x228
109#define REG_MAINCNT3 0x229
Alexander Aringc9f883f2015-09-21 11:24:22 +0200110#define REG_TESTMODE 0x22F /* Test mode */
Alexander Aring554b4942015-09-21 11:24:29 +0200111#define REG_ASSOEAR0 0x230
112#define REG_ASSOEAR1 0x231
113#define REG_ASSOEAR2 0x232
114#define REG_ASSOEAR3 0x233
115#define REG_ASSOEAR4 0x234
116#define REG_ASSOEAR5 0x235
117#define REG_ASSOEAR6 0x236
118#define REG_ASSOEAR7 0x237
119#define REG_ASSOSAR0 0x238
120#define REG_ASSOSAR1 0x239
121#define REG_UNONCE0 0x240
122#define REG_UNONCE1 0x241
123#define REG_UNONCE2 0x242
124#define REG_UNONCE3 0x243
125#define REG_UNONCE4 0x244
126#define REG_UNONCE5 0x245
127#define REG_UNONCE6 0x246
128#define REG_UNONCE7 0x247
129#define REG_UNONCE8 0x248
130#define REG_UNONCE9 0x249
131#define REG_UNONCE10 0x24A
132#define REG_UNONCE11 0x24B
133#define REG_UNONCE12 0x24C
Alexander Aringc9f883f2015-09-21 11:24:22 +0200134#define REG_RX_FIFO 0x300 /* Receive FIFO */
Alan Ott3731a332012-09-02 15:44:13 +0000135
136/* Device configuration: Only channels 11-26 on page 0 are supported. */
137#define MRF24J40_CHAN_MIN 11
138#define MRF24J40_CHAN_MAX 26
139#define CHANNEL_MASK (((u32)1 << (MRF24J40_CHAN_MAX + 1)) \
140 - ((u32)1 << MRF24J40_CHAN_MIN))
141
142#define TX_FIFO_SIZE 128 /* From datasheet */
143#define RX_FIFO_SIZE 144 /* From datasheet */
144#define SET_CHANNEL_DELAY_US 192 /* From datasheet */
145
Simon Vincentdb9e0ee2014-10-06 10:39:45 +0100146enum mrf24j40_modules { MRF24J40, MRF24J40MA, MRF24J40MC };
147
Alan Ott3731a332012-09-02 15:44:13 +0000148/* Device Private Data */
149struct mrf24j40 {
150 struct spi_device *spi;
Alexander Aring5a504392014-10-25 17:16:34 +0200151 struct ieee802154_hw *hw;
Alan Ott3731a332012-09-02 15:44:13 +0000152
Alexander Aringb0156792015-09-21 11:24:30 +0200153 struct regmap *regmap_short;
154 struct regmap *regmap_long;
Alan Ott3731a332012-09-02 15:44:13 +0000155 struct mutex buffer_mutex; /* only used to protect buf */
156 struct completion tx_complete;
Alan Ott3731a332012-09-02 15:44:13 +0000157 u8 *buf; /* 3 bytes. Used for SPI single-register transfers. */
158};
159
Alexander Aringb0156792015-09-21 11:24:30 +0200160/* regmap information for short address register access */
161#define MRF24J40_SHORT_WRITE 0x01
162#define MRF24J40_SHORT_READ 0x00
163#define MRF24J40_SHORT_NUMREGS 0x3F
164
165/* regmap information for long address register access */
166#define MRF24J40_LONG_ACCESS 0x80
167#define MRF24J40_LONG_NUMREGS 0x38F
168
Alan Ott3731a332012-09-02 15:44:13 +0000169/* Read/Write SPI Commands for Short and Long Address registers. */
170#define MRF24J40_READSHORT(reg) ((reg) << 1)
171#define MRF24J40_WRITESHORT(reg) ((reg) << 1 | 1)
172#define MRF24J40_READLONG(reg) (1 << 15 | (reg) << 5)
173#define MRF24J40_WRITELONG(reg) (1 << 15 | (reg) << 5 | 1 << 4)
174
Alan Ottcf82dab2013-03-18 12:06:42 +0000175/* The datasheet indicates the theoretical maximum for SCK to be 10MHz */
176#define MAX_SPI_SPEED_HZ 10000000
Alan Ott3731a332012-09-02 15:44:13 +0000177
178#define printdev(X) (&X->spi->dev)
179
Alexander Aringb0156792015-09-21 11:24:30 +0200180static bool
181mrf24j40_short_reg_writeable(struct device *dev, unsigned int reg)
182{
183 switch (reg) {
184 case REG_RXMCR:
185 case REG_PANIDL:
186 case REG_PANIDH:
187 case REG_SADRL:
188 case REG_SADRH:
189 case REG_EADR0:
190 case REG_EADR1:
191 case REG_EADR2:
192 case REG_EADR3:
193 case REG_EADR4:
194 case REG_EADR5:
195 case REG_EADR6:
196 case REG_EADR7:
197 case REG_RXFLUSH:
198 case REG_ORDER:
199 case REG_TXMCR:
200 case REG_ACKTMOUT:
201 case REG_ESLOTG1:
202 case REG_SYMTICKL:
203 case REG_SYMTICKH:
204 case REG_PACON0:
205 case REG_PACON1:
206 case REG_PACON2:
207 case REG_TXBCON0:
208 case REG_TXNCON:
209 case REG_TXG1CON:
210 case REG_TXG2CON:
211 case REG_ESLOTG23:
212 case REG_ESLOTG45:
213 case REG_ESLOTG67:
214 case REG_TXPEND:
215 case REG_WAKECON:
216 case REG_FROMOFFSET:
217 case REG_TXBCON1:
218 case REG_GATECLK:
219 case REG_TXTIME:
220 case REG_HSYMTMRL:
221 case REG_HSYMTMRH:
222 case REG_SOFTRST:
223 case REG_SECCON0:
224 case REG_SECCON1:
225 case REG_TXSTBL:
226 case REG_RXSR:
227 case REG_INTCON:
228 case REG_TRISGPIO:
229 case REG_GPIO:
230 case REG_RFCTL:
231 case REG_SLPACK:
232 case REG_BBREG0:
233 case REG_BBREG1:
234 case REG_BBREG2:
235 case REG_BBREG3:
236 case REG_BBREG4:
237 case REG_BBREG6:
238 case REG_CCAEDTH:
239 return true;
240 default:
241 return false;
242 }
243}
244
245static bool
246mrf24j40_short_reg_readable(struct device *dev, unsigned int reg)
247{
248 bool rc;
249
250 /* all writeable are also readable */
251 rc = mrf24j40_short_reg_writeable(dev, reg);
252 if (rc)
253 return rc;
254
255 /* readonly regs */
256 switch (reg) {
257 case REG_TXSTAT:
258 case REG_INTSTAT:
259 return true;
260 default:
261 return false;
262 }
263}
264
265static bool
266mrf24j40_short_reg_volatile(struct device *dev, unsigned int reg)
267{
268 /* can be changed during runtime */
269 switch (reg) {
270 case REG_TXSTAT:
271 case REG_INTSTAT:
272 case REG_RXFLUSH:
273 case REG_TXNCON:
274 case REG_SOFTRST:
275 case REG_RFCTL:
276 case REG_TXBCON0:
277 case REG_TXG1CON:
278 case REG_TXG2CON:
279 case REG_TXBCON1:
280 case REG_SECCON0:
281 case REG_RXSR:
282 case REG_SLPACK:
283 case REG_SECCR2:
284 case REG_BBREG6:
285 /* use them in spi_async and regmap so it's volatile */
286 case REG_BBREG1:
287 return true;
288 default:
289 return false;
290 }
291}
292
293static bool
294mrf24j40_short_reg_precious(struct device *dev, unsigned int reg)
295{
296 /* don't clear irq line on read */
297 switch (reg) {
298 case REG_INTSTAT:
299 return true;
300 default:
301 return false;
302 }
303}
304
305static const struct regmap_config mrf24j40_short_regmap = {
306 .name = "mrf24j40_short",
307 .reg_bits = 7,
308 .val_bits = 8,
309 .pad_bits = 1,
310 .write_flag_mask = MRF24J40_SHORT_WRITE,
311 .read_flag_mask = MRF24J40_SHORT_READ,
312 .cache_type = REGCACHE_RBTREE,
313 .max_register = MRF24J40_SHORT_NUMREGS,
314 .writeable_reg = mrf24j40_short_reg_writeable,
315 .readable_reg = mrf24j40_short_reg_readable,
316 .volatile_reg = mrf24j40_short_reg_volatile,
317 .precious_reg = mrf24j40_short_reg_precious,
318};
319
320static bool
321mrf24j40_long_reg_writeable(struct device *dev, unsigned int reg)
322{
323 switch (reg) {
324 case REG_RFCON0:
325 case REG_RFCON1:
326 case REG_RFCON2:
327 case REG_RFCON3:
328 case REG_RFCON5:
329 case REG_RFCON6:
330 case REG_RFCON7:
331 case REG_RFCON8:
332 case REG_SLPCAL2:
333 case REG_SLPCON0:
334 case REG_SLPCON1:
335 case REG_WAKETIMEL:
336 case REG_WAKETIMEH:
337 case REG_REMCNTL:
338 case REG_REMCNTH:
339 case REG_MAINCNT0:
340 case REG_MAINCNT1:
341 case REG_MAINCNT2:
342 case REG_MAINCNT3:
343 case REG_TESTMODE:
344 case REG_ASSOEAR0:
345 case REG_ASSOEAR1:
346 case REG_ASSOEAR2:
347 case REG_ASSOEAR3:
348 case REG_ASSOEAR4:
349 case REG_ASSOEAR5:
350 case REG_ASSOEAR6:
351 case REG_ASSOEAR7:
352 case REG_ASSOSAR0:
353 case REG_ASSOSAR1:
354 case REG_UNONCE0:
355 case REG_UNONCE1:
356 case REG_UNONCE2:
357 case REG_UNONCE3:
358 case REG_UNONCE4:
359 case REG_UNONCE5:
360 case REG_UNONCE6:
361 case REG_UNONCE7:
362 case REG_UNONCE8:
363 case REG_UNONCE9:
364 case REG_UNONCE10:
365 case REG_UNONCE11:
366 case REG_UNONCE12:
367 return true;
368 default:
369 return false;
370 }
371}
372
373static bool
374mrf24j40_long_reg_readable(struct device *dev, unsigned int reg)
375{
376 bool rc;
377
378 /* all writeable are also readable */
379 rc = mrf24j40_long_reg_writeable(dev, reg);
380 if (rc)
381 return rc;
382
383 /* readonly regs */
384 switch (reg) {
385 case REG_SLPCAL0:
386 case REG_SLPCAL1:
387 case REG_RFSTATE:
388 case REG_RSSI:
389 return true;
390 default:
391 return false;
392 }
393}
394
395static bool
396mrf24j40_long_reg_volatile(struct device *dev, unsigned int reg)
397{
398 /* can be changed during runtime */
399 switch (reg) {
400 case REG_SLPCAL0:
401 case REG_SLPCAL1:
402 case REG_SLPCAL2:
403 case REG_RFSTATE:
404 case REG_RSSI:
405 case REG_MAINCNT3:
406 return true;
407 default:
408 return false;
409 }
410}
411
412static const struct regmap_config mrf24j40_long_regmap = {
413 .name = "mrf24j40_long",
414 .reg_bits = 11,
415 .val_bits = 8,
416 .pad_bits = 5,
417 .write_flag_mask = MRF24J40_LONG_ACCESS,
418 .read_flag_mask = MRF24J40_LONG_ACCESS,
419 .cache_type = REGCACHE_RBTREE,
420 .max_register = MRF24J40_LONG_NUMREGS,
421 .writeable_reg = mrf24j40_long_reg_writeable,
422 .readable_reg = mrf24j40_long_reg_readable,
423 .volatile_reg = mrf24j40_long_reg_volatile,
424};
425
426static int mrf24j40_long_regmap_write(void *context, const void *data,
427 size_t count)
428{
429 struct spi_device *spi = context;
430 u8 buf[3];
431
432 if (count > 3)
433 return -EINVAL;
434
435 /* regmap supports read/write mask only in frist byte
436 * long write access need to set the 12th bit, so we
437 * make special handling for write.
438 */
439 memcpy(buf, data, count);
440 buf[1] |= (1 << 4);
441
442 return spi_write(spi, buf, count);
443}
444
445static int
446mrf24j40_long_regmap_read(void *context, const void *reg, size_t reg_size,
447 void *val, size_t val_size)
448{
449 struct spi_device *spi = context;
450
451 return spi_write_then_read(spi, reg, reg_size, val, val_size);
452}
453
454static const struct regmap_bus mrf24j40_long_regmap_bus = {
455 .write = mrf24j40_long_regmap_write,
456 .read = mrf24j40_long_regmap_read,
457 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
458 .val_format_endian_default = REGMAP_ENDIAN_BIG,
459};
460
Alan Ott3731a332012-09-02 15:44:13 +0000461static int write_short_reg(struct mrf24j40 *devrec, u8 reg, u8 value)
462{
463 int ret;
464 struct spi_message msg;
465 struct spi_transfer xfer = {
466 .len = 2,
467 .tx_buf = devrec->buf,
468 .rx_buf = devrec->buf,
469 };
470
471 spi_message_init(&msg);
472 spi_message_add_tail(&xfer, &msg);
473
474 mutex_lock(&devrec->buffer_mutex);
475 devrec->buf[0] = MRF24J40_WRITESHORT(reg);
476 devrec->buf[1] = value;
477
478 ret = spi_sync(devrec->spi, &msg);
479 if (ret)
480 dev_err(printdev(devrec),
481 "SPI write Failed for short register 0x%hhx\n", reg);
482
483 mutex_unlock(&devrec->buffer_mutex);
484 return ret;
485}
486
487static int read_short_reg(struct mrf24j40 *devrec, u8 reg, u8 *val)
488{
489 int ret = -1;
490 struct spi_message msg;
491 struct spi_transfer xfer = {
492 .len = 2,
493 .tx_buf = devrec->buf,
494 .rx_buf = devrec->buf,
495 };
496
497 spi_message_init(&msg);
498 spi_message_add_tail(&xfer, &msg);
499
500 mutex_lock(&devrec->buffer_mutex);
501 devrec->buf[0] = MRF24J40_READSHORT(reg);
502 devrec->buf[1] = 0;
503
504 ret = spi_sync(devrec->spi, &msg);
505 if (ret)
506 dev_err(printdev(devrec),
507 "SPI read Failed for short register 0x%hhx\n", reg);
508 else
509 *val = devrec->buf[1];
510
511 mutex_unlock(&devrec->buffer_mutex);
512 return ret;
513}
514
515static int read_long_reg(struct mrf24j40 *devrec, u16 reg, u8 *value)
516{
517 int ret;
518 u16 cmd;
519 struct spi_message msg;
520 struct spi_transfer xfer = {
521 .len = 3,
522 .tx_buf = devrec->buf,
523 .rx_buf = devrec->buf,
524 };
525
526 spi_message_init(&msg);
527 spi_message_add_tail(&xfer, &msg);
528
529 cmd = MRF24J40_READLONG(reg);
530 mutex_lock(&devrec->buffer_mutex);
531 devrec->buf[0] = cmd >> 8 & 0xff;
532 devrec->buf[1] = cmd & 0xff;
533 devrec->buf[2] = 0;
534
535 ret = spi_sync(devrec->spi, &msg);
536 if (ret)
537 dev_err(printdev(devrec),
538 "SPI read Failed for long register 0x%hx\n", reg);
539 else
540 *value = devrec->buf[2];
541
542 mutex_unlock(&devrec->buffer_mutex);
543 return ret;
544}
545
Alan Ott3731a332012-09-02 15:44:13 +0000546/* This function relies on an undocumented write method. Once a write command
547 and address is set, as many bytes of data as desired can be clocked into
548 the device. The datasheet only shows setting one byte at a time. */
549static int write_tx_buf(struct mrf24j40 *devrec, u16 reg,
550 const u8 *data, size_t length)
551{
552 int ret;
553 u16 cmd;
554 u8 lengths[2];
555 struct spi_message msg;
556 struct spi_transfer addr_xfer = {
557 .len = 2,
558 .tx_buf = devrec->buf,
559 };
560 struct spi_transfer lengths_xfer = {
561 .len = 2,
562 .tx_buf = &lengths, /* TODO: Is DMA really required for SPI? */
563 };
564 struct spi_transfer data_xfer = {
565 .len = length,
566 .tx_buf = data,
567 };
568
569 /* Range check the length. 2 bytes are used for the length fields.*/
570 if (length > TX_FIFO_SIZE-2) {
571 dev_err(printdev(devrec), "write_tx_buf() was passed too large a buffer. Performing short write.\n");
572 length = TX_FIFO_SIZE-2;
573 }
574
575 spi_message_init(&msg);
576 spi_message_add_tail(&addr_xfer, &msg);
577 spi_message_add_tail(&lengths_xfer, &msg);
578 spi_message_add_tail(&data_xfer, &msg);
579
580 cmd = MRF24J40_WRITELONG(reg);
581 mutex_lock(&devrec->buffer_mutex);
582 devrec->buf[0] = cmd >> 8 & 0xff;
583 devrec->buf[1] = cmd & 0xff;
584 lengths[0] = 0x0; /* Header Length. Set to 0 for now. TODO */
585 lengths[1] = length; /* Total length */
586
587 ret = spi_sync(devrec->spi, &msg);
588 if (ret)
589 dev_err(printdev(devrec), "SPI write Failed for TX buf\n");
590
591 mutex_unlock(&devrec->buffer_mutex);
592 return ret;
593}
594
595static int mrf24j40_read_rx_buf(struct mrf24j40 *devrec,
596 u8 *data, u8 *len, u8 *lqi)
597{
598 u8 rx_len;
599 u8 addr[2];
600 u8 lqi_rssi[2];
601 u16 cmd;
602 int ret;
603 struct spi_message msg;
604 struct spi_transfer addr_xfer = {
605 .len = 2,
606 .tx_buf = &addr,
607 };
608 struct spi_transfer data_xfer = {
609 .len = 0x0, /* set below */
610 .rx_buf = data,
611 };
612 struct spi_transfer status_xfer = {
613 .len = 2,
614 .rx_buf = &lqi_rssi,
615 };
616
617 /* Get the length of the data in the RX FIFO. The length in this
618 * register exclues the 1-byte length field at the beginning. */
619 ret = read_long_reg(devrec, REG_RX_FIFO, &rx_len);
620 if (ret)
621 goto out;
622
623 /* Range check the RX FIFO length, accounting for the one-byte
Stefan Schmidt5c1be062014-12-12 12:45:32 +0100624 * length field at the beginning. */
Alan Ott3731a332012-09-02 15:44:13 +0000625 if (rx_len > RX_FIFO_SIZE-1) {
626 dev_err(printdev(devrec), "Invalid length read from device. Performing short read.\n");
627 rx_len = RX_FIFO_SIZE-1;
628 }
629
630 if (rx_len > *len) {
631 /* Passed in buffer wasn't big enough. Should never happen. */
632 dev_err(printdev(devrec), "Buffer not big enough. Performing short read\n");
633 rx_len = *len;
634 }
635
636 /* Set up the commands to read the data. */
637 cmd = MRF24J40_READLONG(REG_RX_FIFO+1);
638 addr[0] = cmd >> 8 & 0xff;
639 addr[1] = cmd & 0xff;
640 data_xfer.len = rx_len;
641
642 spi_message_init(&msg);
643 spi_message_add_tail(&addr_xfer, &msg);
644 spi_message_add_tail(&data_xfer, &msg);
645 spi_message_add_tail(&status_xfer, &msg);
646
647 ret = spi_sync(devrec->spi, &msg);
648 if (ret) {
649 dev_err(printdev(devrec), "SPI RX Buffer Read Failed.\n");
650 goto out;
651 }
652
653 *lqi = lqi_rssi[0];
654 *len = rx_len;
655
656#ifdef DEBUG
657 print_hex_dump(KERN_DEBUG, "mrf24j40 rx: ",
Stefan Schmidtce261bc2014-12-12 12:45:33 +0100658 DUMP_PREFIX_OFFSET, 16, 1, data, *len, 0);
Varka Bhadramca079ad2014-09-24 12:21:32 +0200659 pr_debug("mrf24j40 rx: lqi: %02hhx rssi: %02hhx\n",
660 lqi_rssi[0], lqi_rssi[1]);
Alan Ott3731a332012-09-02 15:44:13 +0000661#endif
662
663out:
664 return ret;
665}
666
Alexander Aring5a504392014-10-25 17:16:34 +0200667static int mrf24j40_tx(struct ieee802154_hw *hw, struct sk_buff *skb)
Alan Ott3731a332012-09-02 15:44:13 +0000668{
Alexander Aring5a504392014-10-25 17:16:34 +0200669 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000670 u8 val;
671 int ret = 0;
672
673 dev_dbg(printdev(devrec), "tx packet of %d bytes\n", skb->len);
674
675 ret = write_tx_buf(devrec, 0x000, skb->data, skb->len);
676 if (ret)
677 goto err;
678
Wolfram Sang16735d02013-11-14 14:32:02 -0800679 reinit_completion(&devrec->tx_complete);
Alan Ott9757f1d2013-10-05 23:52:22 -0400680
Alan Ott3731a332012-09-02 15:44:13 +0000681 /* Set TXNTRIG bit of TXNCON to send packet */
682 ret = read_short_reg(devrec, REG_TXNCON, &val);
683 if (ret)
684 goto err;
685 val |= 0x1;
Alan Ottcbde8122013-04-05 10:34:51 +0000686 /* Set TXNACKREQ if the ACK bit is set in the packet. */
687 if (skb->data[0] & IEEE802154_FC_ACK_REQ)
688 val |= 0x4;
Alan Ott3731a332012-09-02 15:44:13 +0000689 write_short_reg(devrec, REG_TXNCON, val);
690
Alan Ott3731a332012-09-02 15:44:13 +0000691 /* Wait for the device to send the TX complete interrupt. */
692 ret = wait_for_completion_interruptible_timeout(
693 &devrec->tx_complete,
694 5 * HZ);
695 if (ret == -ERESTARTSYS)
696 goto err;
697 if (ret == 0) {
Alan Ott7a1c2312013-03-18 12:06:41 +0000698 dev_warn(printdev(devrec), "Timeout waiting for TX interrupt\n");
Alan Ott3731a332012-09-02 15:44:13 +0000699 ret = -ETIMEDOUT;
700 goto err;
701 }
702
703 /* Check for send error from the device. */
704 ret = read_short_reg(devrec, REG_TXSTAT, &val);
705 if (ret)
706 goto err;
707 if (val & 0x1) {
Alan Ottcbde8122013-04-05 10:34:51 +0000708 dev_dbg(printdev(devrec), "Error Sending. Retry count exceeded\n");
Alan Ott3731a332012-09-02 15:44:13 +0000709 ret = -ECOMM; /* TODO: Better error code ? */
710 } else
711 dev_dbg(printdev(devrec), "Packet Sent\n");
712
713err:
714
715 return ret;
716}
717
Alexander Aring5a504392014-10-25 17:16:34 +0200718static int mrf24j40_ed(struct ieee802154_hw *hw, u8 *level)
Alan Ott3731a332012-09-02 15:44:13 +0000719{
720 /* TODO: */
Varka Bhadramca079ad2014-09-24 12:21:32 +0200721 pr_warn("mrf24j40: ed not implemented\n");
Alan Ott3731a332012-09-02 15:44:13 +0000722 *level = 0;
723 return 0;
724}
725
Alexander Aring5a504392014-10-25 17:16:34 +0200726static int mrf24j40_start(struct ieee802154_hw *hw)
Alan Ott3731a332012-09-02 15:44:13 +0000727{
Alexander Aring5a504392014-10-25 17:16:34 +0200728 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000729
730 dev_dbg(printdev(devrec), "start\n");
731
Alexander Aring42c71482015-09-21 11:24:31 +0200732 /* Clear TXNIE and RXIE. Enable interrupts */
733 return regmap_update_bits(devrec->regmap_short, REG_INTCON,
734 0x01 | 0x08, 0x00);
Alan Ott3731a332012-09-02 15:44:13 +0000735}
736
Alexander Aring5a504392014-10-25 17:16:34 +0200737static void mrf24j40_stop(struct ieee802154_hw *hw)
Alan Ott3731a332012-09-02 15:44:13 +0000738{
Alexander Aring5a504392014-10-25 17:16:34 +0200739 struct mrf24j40 *devrec = hw->priv;
Varka Bhadram529160d2014-09-24 12:21:30 +0200740
Alan Ott3731a332012-09-02 15:44:13 +0000741 dev_dbg(printdev(devrec), "stop\n");
742
Alexander Aring42c71482015-09-21 11:24:31 +0200743 /* Set TXNIE and RXIE. Disable Interrupts */
744 regmap_update_bits(devrec->regmap_short, REG_INTCON, 0x01 | 0x08,
745 0x01 | 0x08);
Alan Ott3731a332012-09-02 15:44:13 +0000746}
747
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100748static int mrf24j40_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
Alan Ott3731a332012-09-02 15:44:13 +0000749{
Alexander Aring5a504392014-10-25 17:16:34 +0200750 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000751 u8 val;
752 int ret;
753
754 dev_dbg(printdev(devrec), "Set Channel %d\n", channel);
755
756 WARN_ON(page != 0);
757 WARN_ON(channel < MRF24J40_CHAN_MIN);
758 WARN_ON(channel > MRF24J40_CHAN_MAX);
759
760 /* Set Channel TODO */
761 val = (channel-11) << 4 | 0x03;
Alexander Aring42c71482015-09-21 11:24:31 +0200762 ret = regmap_update_bits(devrec->regmap_long, REG_RFCON0, 0xf0, val);
Alan Ott3731a332012-09-02 15:44:13 +0000763 if (ret)
764 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000765
Alexander Aring42c71482015-09-21 11:24:31 +0200766 /* RF Reset */
767 ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x04);
768 if (ret)
769 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000770
Alexander Aring42c71482015-09-21 11:24:31 +0200771 ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x00);
772 if (!ret)
773 udelay(SET_CHANNEL_DELAY_US); /* per datasheet */
774
775 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000776}
777
Alexander Aring5a504392014-10-25 17:16:34 +0200778static int mrf24j40_filter(struct ieee802154_hw *hw,
Alan Ott3731a332012-09-02 15:44:13 +0000779 struct ieee802154_hw_addr_filt *filt,
780 unsigned long changed)
781{
Alexander Aring5a504392014-10-25 17:16:34 +0200782 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000783
784 dev_dbg(printdev(devrec), "filter\n");
785
Alexander Aring57205c12014-10-25 05:25:09 +0200786 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000787 /* Short Addr */
788 u8 addrh, addrl;
Varka Bhadram529160d2014-09-24 12:21:30 +0200789
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100790 addrh = le16_to_cpu(filt->short_addr) >> 8 & 0xff;
791 addrl = le16_to_cpu(filt->short_addr) & 0xff;
Alan Ott3731a332012-09-02 15:44:13 +0000792
Alexander Aring42c71482015-09-21 11:24:31 +0200793 regmap_write(devrec->regmap_short, REG_SADRH, addrh);
794 regmap_write(devrec->regmap_short, REG_SADRL, addrl);
Alan Ott3731a332012-09-02 15:44:13 +0000795 dev_dbg(printdev(devrec),
796 "Set short addr to %04hx\n", filt->short_addr);
797 }
798
Alexander Aring57205c12014-10-25 05:25:09 +0200799 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000800 /* Device Address */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100801 u8 i, addr[8];
802
803 memcpy(addr, &filt->ieee_addr, 8);
Alan Ott3731a332012-09-02 15:44:13 +0000804 for (i = 0; i < 8; i++)
Alexander Aring42c71482015-09-21 11:24:31 +0200805 regmap_write(devrec->regmap_short, REG_EADR0 + i,
806 addr[i]);
Alan Ott3731a332012-09-02 15:44:13 +0000807
808#ifdef DEBUG
Varka Bhadramca079ad2014-09-24 12:21:32 +0200809 pr_debug("Set long addr to: ");
Alan Ott3731a332012-09-02 15:44:13 +0000810 for (i = 0; i < 8; i++)
Varka Bhadramca079ad2014-09-24 12:21:32 +0200811 pr_debug("%02hhx ", addr[7 - i]);
812 pr_debug("\n");
Alan Ott3731a332012-09-02 15:44:13 +0000813#endif
814 }
815
Alexander Aring57205c12014-10-25 05:25:09 +0200816 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000817 /* PAN ID */
818 u8 panidl, panidh;
Varka Bhadram529160d2014-09-24 12:21:30 +0200819
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100820 panidh = le16_to_cpu(filt->pan_id) >> 8 & 0xff;
821 panidl = le16_to_cpu(filt->pan_id) & 0xff;
Alexander Aring42c71482015-09-21 11:24:31 +0200822 regmap_write(devrec->regmap_short, REG_PANIDH, panidh);
823 regmap_write(devrec->regmap_short, REG_PANIDL, panidl);
Alan Ott3731a332012-09-02 15:44:13 +0000824
825 dev_dbg(printdev(devrec), "Set PANID to %04hx\n", filt->pan_id);
826 }
827
Alexander Aring57205c12014-10-25 05:25:09 +0200828 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000829 /* Pan Coordinator */
830 u8 val;
831 int ret;
832
Alexander Aring42c71482015-09-21 11:24:31 +0200833 if (filt->pan_coord)
834 val = 0x8;
835 else
836 val = 0x0;
837 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x8,
838 val);
Alan Ott3731a332012-09-02 15:44:13 +0000839 if (ret)
840 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000841
842 /* REG_SLOTTED is maintained as default (unslotted/CSMA-CA).
843 * REG_ORDER is maintained as default (no beacon/superframe).
844 */
845
846 dev_dbg(printdev(devrec), "Set Pan Coord to %s\n",
Stefan Schmidtce261bc2014-12-12 12:45:33 +0100847 filt->pan_coord ? "on" : "off");
Alan Ott3731a332012-09-02 15:44:13 +0000848 }
849
850 return 0;
851}
852
853static int mrf24j40_handle_rx(struct mrf24j40 *devrec)
854{
855 u8 len = RX_FIFO_SIZE;
856 u8 lqi = 0;
857 u8 val;
858 int ret = 0;
Stefan Schmidte5719b62015-06-09 10:52:26 +0200859 int ret2;
Alan Ott3731a332012-09-02 15:44:13 +0000860 struct sk_buff *skb;
861
862 /* Turn off reception of packets off the air. This prevents the
863 * device from overwriting the buffer while we're reading it. */
864 ret = read_short_reg(devrec, REG_BBREG1, &val);
865 if (ret)
866 goto out;
867 val |= 4; /* SET RXDECINV */
868 write_short_reg(devrec, REG_BBREG1, val);
869
Alexander Aring61a22812014-10-27 17:13:29 +0100870 skb = dev_alloc_skb(len);
Alan Ott3731a332012-09-02 15:44:13 +0000871 if (!skb) {
872 ret = -ENOMEM;
873 goto out;
874 }
875
876 ret = mrf24j40_read_rx_buf(devrec, skb_put(skb, len), &len, &lqi);
877 if (ret < 0) {
878 dev_err(printdev(devrec), "Failure reading RX FIFO\n");
879 kfree_skb(skb);
880 ret = -EINVAL;
881 goto out;
882 }
883
Alan Ott3731a332012-09-02 15:44:13 +0000884 /* TODO: Other drivers call ieee20154_rx_irqsafe() here (eg: cc2040,
885 * also from a workqueue). I think irqsafe is not necessary here.
886 * Can someone confirm? */
Alexander Aring5a504392014-10-25 17:16:34 +0200887 ieee802154_rx_irqsafe(devrec->hw, skb, lqi);
Alan Ott3731a332012-09-02 15:44:13 +0000888
889 dev_dbg(printdev(devrec), "RX Handled\n");
890
891out:
892 /* Turn back on reception of packets off the air. */
Stefan Schmidte5719b62015-06-09 10:52:26 +0200893 ret2 = read_short_reg(devrec, REG_BBREG1, &val);
894 if (ret2)
895 return ret2;
Alan Ott3731a332012-09-02 15:44:13 +0000896 val &= ~0x4; /* Clear RXDECINV */
897 write_short_reg(devrec, REG_BBREG1, val);
898
899 return ret;
900}
901
Alexander Aring16301862014-10-28 18:21:18 +0100902static const struct ieee802154_ops mrf24j40_ops = {
Alan Ott3731a332012-09-02 15:44:13 +0000903 .owner = THIS_MODULE,
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100904 .xmit_sync = mrf24j40_tx,
Alan Ott3731a332012-09-02 15:44:13 +0000905 .ed = mrf24j40_ed,
906 .start = mrf24j40_start,
907 .stop = mrf24j40_stop,
908 .set_channel = mrf24j40_set_channel,
909 .set_hw_addr_filt = mrf24j40_filter,
910};
911
912static irqreturn_t mrf24j40_isr(int irq, void *data)
913{
914 struct mrf24j40 *devrec = data;
Alan Ott3731a332012-09-02 15:44:13 +0000915 u8 intstat;
916 int ret;
917
918 /* Read the interrupt status */
919 ret = read_short_reg(devrec, REG_INTSTAT, &intstat);
920 if (ret)
921 goto out;
922
923 /* Check for TX complete */
924 if (intstat & 0x1)
925 complete(&devrec->tx_complete);
926
927 /* Check for Rx */
928 if (intstat & 0x8)
929 mrf24j40_handle_rx(devrec);
930
931out:
Alan Ott4a4e1da2013-10-05 23:52:23 -0400932 return IRQ_HANDLED;
Alan Ott3731a332012-09-02 15:44:13 +0000933}
934
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530935static int mrf24j40_hw_init(struct mrf24j40 *devrec)
936{
937 int ret;
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530938
939 /* Initialize the device.
940 From datasheet section 3.2: Initialization. */
Alexander Aring42c71482015-09-21 11:24:31 +0200941 ret = regmap_write(devrec->regmap_short, REG_SOFTRST, 0x07);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530942 if (ret)
943 goto err_ret;
944
Alexander Aring42c71482015-09-21 11:24:31 +0200945 ret = regmap_write(devrec->regmap_short, REG_PACON2, 0x98);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530946 if (ret)
947 goto err_ret;
948
Alexander Aring42c71482015-09-21 11:24:31 +0200949 ret = regmap_write(devrec->regmap_short, REG_TXSTBL, 0x95);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530950 if (ret)
951 goto err_ret;
952
Alexander Aring42c71482015-09-21 11:24:31 +0200953 ret = regmap_write(devrec->regmap_long, REG_RFCON0, 0x03);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530954 if (ret)
955 goto err_ret;
956
Alexander Aring42c71482015-09-21 11:24:31 +0200957 ret = regmap_write(devrec->regmap_long, REG_RFCON1, 0x01);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530958 if (ret)
959 goto err_ret;
960
Alexander Aring42c71482015-09-21 11:24:31 +0200961 ret = regmap_write(devrec->regmap_long, REG_RFCON2, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530962 if (ret)
963 goto err_ret;
964
Alexander Aring42c71482015-09-21 11:24:31 +0200965 ret = regmap_write(devrec->regmap_long, REG_RFCON6, 0x90);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530966 if (ret)
967 goto err_ret;
968
Alexander Aring42c71482015-09-21 11:24:31 +0200969 ret = regmap_write(devrec->regmap_long, REG_RFCON7, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530970 if (ret)
971 goto err_ret;
972
Alexander Aring42c71482015-09-21 11:24:31 +0200973 ret = regmap_write(devrec->regmap_long, REG_RFCON8, 0x10);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530974 if (ret)
975 goto err_ret;
976
Alexander Aring42c71482015-09-21 11:24:31 +0200977 ret = regmap_write(devrec->regmap_long, REG_SLPCON1, 0x21);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530978 if (ret)
979 goto err_ret;
980
Alexander Aring42c71482015-09-21 11:24:31 +0200981 ret = regmap_write(devrec->regmap_short, REG_BBREG2, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530982 if (ret)
983 goto err_ret;
984
Alexander Aring42c71482015-09-21 11:24:31 +0200985 ret = regmap_write(devrec->regmap_short, REG_CCAEDTH, 0x60);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530986 if (ret)
987 goto err_ret;
988
Alexander Aring42c71482015-09-21 11:24:31 +0200989 ret = regmap_write(devrec->regmap_short, REG_BBREG6, 0x40);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530990 if (ret)
991 goto err_ret;
992
Alexander Aring42c71482015-09-21 11:24:31 +0200993 ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x04);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530994 if (ret)
995 goto err_ret;
996
Alexander Aring42c71482015-09-21 11:24:31 +0200997 ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x0);
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530998 if (ret)
999 goto err_ret;
1000
1001 udelay(192);
1002
1003 /* Set RX Mode. RXMCR<1:0>: 0x0 normal, 0x1 promisc, 0x2 error */
Alexander Aring42c71482015-09-21 11:24:31 +02001004 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x03, 0x00);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301005 if (ret)
1006 goto err_ret;
1007
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001008 if (spi_get_device_id(devrec->spi)->driver_data == MRF24J40MC) {
1009 /* Enable external amplifier.
1010 * From MRF24J40MC datasheet section 1.3: Operation.
1011 */
Alexander Aring42c71482015-09-21 11:24:31 +02001012 regmap_update_bits(devrec->regmap_long, REG_TESTMODE, 0x07,
1013 0x07);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001014
Alexander Aring42c71482015-09-21 11:24:31 +02001015 /* Set GPIO3 as output. */
1016 regmap_update_bits(devrec->regmap_short, REG_TRISGPIO, 0x08,
1017 0x08);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001018
Alexander Aring42c71482015-09-21 11:24:31 +02001019 /* Set GPIO3 HIGH to enable U5 voltage regulator */
1020 regmap_update_bits(devrec->regmap_short, REG_GPIO, 0x08, 0x08);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001021
1022 /* Reduce TX pwr to meet FCC requirements.
1023 * From MRF24J40MC datasheet section 3.1.1
1024 */
Alexander Aring42c71482015-09-21 11:24:31 +02001025 regmap_write(devrec->regmap_long, REG_RFCON3, 0x28);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001026 }
1027
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301028 return 0;
1029
1030err_ret:
1031 return ret;
1032}
1033
Alexander Aring766928f2015-09-21 11:24:27 +02001034static void mrf24j40_phy_setup(struct mrf24j40 *devrec)
1035{
Alexander Aringd344c912015-09-21 11:24:28 +02001036 ieee802154_random_extended_addr(&devrec->hw->phy->perm_extended_addr);
Alexander Aring766928f2015-09-21 11:24:27 +02001037 devrec->hw->phy->current_channel = 11;
1038}
1039
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001040static int mrf24j40_probe(struct spi_device *spi)
Alan Ott3731a332012-09-02 15:44:13 +00001041{
1042 int ret = -ENOMEM;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001043 struct ieee802154_hw *hw;
Alan Ott3731a332012-09-02 15:44:13 +00001044 struct mrf24j40 *devrec;
1045
Varka Bhadramca079ad2014-09-24 12:21:32 +02001046 dev_info(&spi->dev, "probe(). IRQ: %d\n", spi->irq);
Alan Ott3731a332012-09-02 15:44:13 +00001047
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001048 /* Register with the 802154 subsystem */
1049
1050 hw = ieee802154_alloc_hw(sizeof(*devrec), &mrf24j40_ops);
1051 if (!hw)
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301052 goto err_ret;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001053
1054 devrec = hw->priv;
1055 devrec->spi = spi;
1056 spi_set_drvdata(spi, devrec);
1057 devrec->hw = hw;
1058 devrec->hw->parent = &spi->dev;
1059 devrec->hw->phy->supported.channels[0] = CHANNEL_MASK;
Alexander Aringab40ff72015-09-21 11:24:32 +02001060 devrec->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001061
Alexander Aringb0156792015-09-21 11:24:30 +02001062 devrec->regmap_short = devm_regmap_init_spi(spi,
1063 &mrf24j40_short_regmap);
1064 if (IS_ERR(devrec->regmap_short)) {
1065 ret = PTR_ERR(devrec->regmap_short);
1066 dev_err(&spi->dev, "Failed to allocate short register map: %d\n",
1067 ret);
1068 goto err_register_device;
1069 }
1070
1071 devrec->regmap_long = devm_regmap_init(&spi->dev,
1072 &mrf24j40_long_regmap_bus,
1073 spi, &mrf24j40_long_regmap);
1074 if (IS_ERR(devrec->regmap_long)) {
1075 ret = PTR_ERR(devrec->regmap_long);
1076 dev_err(&spi->dev, "Failed to allocate long register map: %d\n",
1077 ret);
1078 goto err_register_device;
1079 }
1080
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301081 devrec->buf = devm_kzalloc(&spi->dev, 3, GFP_KERNEL);
Alan Ott3731a332012-09-02 15:44:13 +00001082 if (!devrec->buf)
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001083 goto err_register_device;
Alan Ott3731a332012-09-02 15:44:13 +00001084
Alexander Aring78aedb62015-09-21 11:24:25 +02001085 if (spi->max_speed_hz > MAX_SPI_SPEED_HZ) {
1086 dev_warn(&spi->dev, "spi clock above possible maximum: %d",
1087 MAX_SPI_SPEED_HZ);
1088 return -EINVAL;
1089 }
Alan Ott3731a332012-09-02 15:44:13 +00001090
1091 mutex_init(&devrec->buffer_mutex);
1092 init_completion(&devrec->tx_complete);
Alan Ott3731a332012-09-02 15:44:13 +00001093
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301094 ret = mrf24j40_hw_init(devrec);
Alan Ott3731a332012-09-02 15:44:13 +00001095 if (ret)
Alexander Aringa339e182015-09-21 11:24:24 +02001096 goto err_register_device;
Alan Ott3731a332012-09-02 15:44:13 +00001097
Alexander Aring766928f2015-09-21 11:24:27 +02001098 mrf24j40_phy_setup(devrec);
1099
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301100 ret = devm_request_threaded_irq(&spi->dev,
1101 spi->irq,
1102 NULL,
1103 mrf24j40_isr,
1104 IRQF_TRIGGER_LOW|IRQF_ONESHOT,
1105 dev_name(&spi->dev),
1106 devrec);
Alan Ott3731a332012-09-02 15:44:13 +00001107
1108 if (ret) {
1109 dev_err(printdev(devrec), "Unable to get IRQ");
Alexander Aringa339e182015-09-21 11:24:24 +02001110 goto err_register_device;
Alan Ott3731a332012-09-02 15:44:13 +00001111 }
1112
Alexander Aringa339e182015-09-21 11:24:24 +02001113 dev_dbg(printdev(devrec), "registered mrf24j40\n");
1114 ret = ieee802154_register_hw(devrec->hw);
1115 if (ret)
1116 goto err_register_device;
1117
Alan Ott3731a332012-09-02 15:44:13 +00001118 return 0;
1119
Alan Ott3731a332012-09-02 15:44:13 +00001120err_register_device:
Alexander Aring5a504392014-10-25 17:16:34 +02001121 ieee802154_free_hw(devrec->hw);
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301122err_ret:
Alan Ott3731a332012-09-02 15:44:13 +00001123 return ret;
1124}
1125
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001126static int mrf24j40_remove(struct spi_device *spi)
Alan Ott3731a332012-09-02 15:44:13 +00001127{
Jingoo Han4fa0a0e2013-04-05 20:34:18 +00001128 struct mrf24j40 *devrec = spi_get_drvdata(spi);
Alan Ott3731a332012-09-02 15:44:13 +00001129
1130 dev_dbg(printdev(devrec), "remove\n");
1131
Alexander Aring5a504392014-10-25 17:16:34 +02001132 ieee802154_unregister_hw(devrec->hw);
1133 ieee802154_free_hw(devrec->hw);
Alan Ott3731a332012-09-02 15:44:13 +00001134 /* TODO: Will ieee802154_free_device() wait until ->xmit() is
1135 * complete? */
1136
Alan Ott3731a332012-09-02 15:44:13 +00001137 return 0;
1138}
1139
Alexander Aring2e6fd642015-09-21 11:24:26 +02001140static const struct of_device_id mrf24j40_of_match[] = {
1141 { .compatible = "microchip,mrf24j40", .data = (void *)MRF24J40 },
1142 { .compatible = "microchip,mrf24j40ma", .data = (void *)MRF24J40MA },
1143 { .compatible = "microchip,mrf24j40mc", .data = (void *)MRF24J40MC },
1144 { },
1145};
1146MODULE_DEVICE_TABLE(of, mrf24j40_of_match);
1147
Alan Ott3731a332012-09-02 15:44:13 +00001148static const struct spi_device_id mrf24j40_ids[] = {
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001149 { "mrf24j40", MRF24J40 },
1150 { "mrf24j40ma", MRF24J40MA },
1151 { "mrf24j40mc", MRF24J40MC },
Alan Ott3731a332012-09-02 15:44:13 +00001152 { },
1153};
1154MODULE_DEVICE_TABLE(spi, mrf24j40_ids);
1155
1156static struct spi_driver mrf24j40_driver = {
1157 .driver = {
Alexander Aring2e6fd642015-09-21 11:24:26 +02001158 .of_match_table = of_match_ptr(mrf24j40_of_match),
Alan Ott3731a332012-09-02 15:44:13 +00001159 .name = "mrf24j40",
Alan Ott3731a332012-09-02 15:44:13 +00001160 .owner = THIS_MODULE,
1161 },
1162 .id_table = mrf24j40_ids,
1163 .probe = mrf24j40_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001164 .remove = mrf24j40_remove,
Alan Ott3731a332012-09-02 15:44:13 +00001165};
1166
Wei Yongjun3d4a1312013-04-08 20:34:44 +00001167module_spi_driver(mrf24j40_driver);
Alan Ott3731a332012-09-02 15:44:13 +00001168
1169MODULE_LICENSE("GPL");
1170MODULE_AUTHOR("Alan Ott");
1171MODULE_DESCRIPTION("MRF24J40 SPI 802.15.4 Controller Driver");