blob: 146ad57165ea593270bd2fd6dd89b39fc81267d9 [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;
Alexander Aring6844a0e2015-09-21 11:24:34 +0200155
156 /* for writing txfifo */
157 struct spi_message tx_msg;
158 u8 tx_hdr_buf[2];
159 struct spi_transfer tx_hdr_trx;
160 u8 tx_len_buf[2];
161 struct spi_transfer tx_len_trx;
162 struct spi_transfer tx_buf_trx;
163 struct sk_buff *tx_skb;
164
165 /* post transmit message to send frame out */
166 struct spi_message tx_post_msg;
167 u8 tx_post_buf[2];
168 struct spi_transfer tx_post_trx;
169
Alexander Aringc91a3012015-09-21 11:24:35 +0200170 /* for protect/unprotect/read length rxfifo */
171 struct spi_message rx_msg;
172 u8 rx_buf[3];
173 struct spi_transfer rx_trx;
174
175 /* receive handling */
176 struct spi_message rx_buf_msg;
177 u8 rx_addr_buf[2];
178 struct spi_transfer rx_addr_trx;
179 u8 rx_lqi_buf[2];
180 struct spi_transfer rx_lqi_trx;
181 u8 rx_fifo_buf[RX_FIFO_SIZE];
182 struct spi_transfer rx_fifo_buf_trx;
183
Alexander Aring37441612015-09-21 11:24:36 +0200184 /* isr handling for reading intstat */
185 struct spi_message irq_msg;
186 u8 irq_buf[2];
187 struct spi_transfer irq_trx;
Alan Ott3731a332012-09-02 15:44:13 +0000188};
189
Alexander Aringb0156792015-09-21 11:24:30 +0200190/* regmap information for short address register access */
191#define MRF24J40_SHORT_WRITE 0x01
192#define MRF24J40_SHORT_READ 0x00
193#define MRF24J40_SHORT_NUMREGS 0x3F
194
195/* regmap information for long address register access */
196#define MRF24J40_LONG_ACCESS 0x80
197#define MRF24J40_LONG_NUMREGS 0x38F
198
Alan Ott3731a332012-09-02 15:44:13 +0000199/* Read/Write SPI Commands for Short and Long Address registers. */
200#define MRF24J40_READSHORT(reg) ((reg) << 1)
201#define MRF24J40_WRITESHORT(reg) ((reg) << 1 | 1)
202#define MRF24J40_READLONG(reg) (1 << 15 | (reg) << 5)
203#define MRF24J40_WRITELONG(reg) (1 << 15 | (reg) << 5 | 1 << 4)
204
Alan Ottcf82dab2013-03-18 12:06:42 +0000205/* The datasheet indicates the theoretical maximum for SCK to be 10MHz */
206#define MAX_SPI_SPEED_HZ 10000000
Alan Ott3731a332012-09-02 15:44:13 +0000207
208#define printdev(X) (&X->spi->dev)
209
Alexander Aringb0156792015-09-21 11:24:30 +0200210static bool
211mrf24j40_short_reg_writeable(struct device *dev, unsigned int reg)
212{
213 switch (reg) {
214 case REG_RXMCR:
215 case REG_PANIDL:
216 case REG_PANIDH:
217 case REG_SADRL:
218 case REG_SADRH:
219 case REG_EADR0:
220 case REG_EADR1:
221 case REG_EADR2:
222 case REG_EADR3:
223 case REG_EADR4:
224 case REG_EADR5:
225 case REG_EADR6:
226 case REG_EADR7:
227 case REG_RXFLUSH:
228 case REG_ORDER:
229 case REG_TXMCR:
230 case REG_ACKTMOUT:
231 case REG_ESLOTG1:
232 case REG_SYMTICKL:
233 case REG_SYMTICKH:
234 case REG_PACON0:
235 case REG_PACON1:
236 case REG_PACON2:
237 case REG_TXBCON0:
238 case REG_TXNCON:
239 case REG_TXG1CON:
240 case REG_TXG2CON:
241 case REG_ESLOTG23:
242 case REG_ESLOTG45:
243 case REG_ESLOTG67:
244 case REG_TXPEND:
245 case REG_WAKECON:
246 case REG_FROMOFFSET:
247 case REG_TXBCON1:
248 case REG_GATECLK:
249 case REG_TXTIME:
250 case REG_HSYMTMRL:
251 case REG_HSYMTMRH:
252 case REG_SOFTRST:
253 case REG_SECCON0:
254 case REG_SECCON1:
255 case REG_TXSTBL:
256 case REG_RXSR:
257 case REG_INTCON:
258 case REG_TRISGPIO:
259 case REG_GPIO:
260 case REG_RFCTL:
261 case REG_SLPACK:
262 case REG_BBREG0:
263 case REG_BBREG1:
264 case REG_BBREG2:
265 case REG_BBREG3:
266 case REG_BBREG4:
267 case REG_BBREG6:
268 case REG_CCAEDTH:
269 return true;
270 default:
271 return false;
272 }
273}
274
275static bool
276mrf24j40_short_reg_readable(struct device *dev, unsigned int reg)
277{
278 bool rc;
279
280 /* all writeable are also readable */
281 rc = mrf24j40_short_reg_writeable(dev, reg);
282 if (rc)
283 return rc;
284
285 /* readonly regs */
286 switch (reg) {
287 case REG_TXSTAT:
288 case REG_INTSTAT:
289 return true;
290 default:
291 return false;
292 }
293}
294
295static bool
296mrf24j40_short_reg_volatile(struct device *dev, unsigned int reg)
297{
298 /* can be changed during runtime */
299 switch (reg) {
300 case REG_TXSTAT:
301 case REG_INTSTAT:
302 case REG_RXFLUSH:
303 case REG_TXNCON:
304 case REG_SOFTRST:
305 case REG_RFCTL:
306 case REG_TXBCON0:
307 case REG_TXG1CON:
308 case REG_TXG2CON:
309 case REG_TXBCON1:
310 case REG_SECCON0:
311 case REG_RXSR:
312 case REG_SLPACK:
313 case REG_SECCR2:
314 case REG_BBREG6:
315 /* use them in spi_async and regmap so it's volatile */
316 case REG_BBREG1:
317 return true;
318 default:
319 return false;
320 }
321}
322
323static bool
324mrf24j40_short_reg_precious(struct device *dev, unsigned int reg)
325{
326 /* don't clear irq line on read */
327 switch (reg) {
328 case REG_INTSTAT:
329 return true;
330 default:
331 return false;
332 }
333}
334
335static const struct regmap_config mrf24j40_short_regmap = {
336 .name = "mrf24j40_short",
337 .reg_bits = 7,
338 .val_bits = 8,
339 .pad_bits = 1,
340 .write_flag_mask = MRF24J40_SHORT_WRITE,
341 .read_flag_mask = MRF24J40_SHORT_READ,
342 .cache_type = REGCACHE_RBTREE,
343 .max_register = MRF24J40_SHORT_NUMREGS,
344 .writeable_reg = mrf24j40_short_reg_writeable,
345 .readable_reg = mrf24j40_short_reg_readable,
346 .volatile_reg = mrf24j40_short_reg_volatile,
347 .precious_reg = mrf24j40_short_reg_precious,
348};
349
350static bool
351mrf24j40_long_reg_writeable(struct device *dev, unsigned int reg)
352{
353 switch (reg) {
354 case REG_RFCON0:
355 case REG_RFCON1:
356 case REG_RFCON2:
357 case REG_RFCON3:
358 case REG_RFCON5:
359 case REG_RFCON6:
360 case REG_RFCON7:
361 case REG_RFCON8:
362 case REG_SLPCAL2:
363 case REG_SLPCON0:
364 case REG_SLPCON1:
365 case REG_WAKETIMEL:
366 case REG_WAKETIMEH:
367 case REG_REMCNTL:
368 case REG_REMCNTH:
369 case REG_MAINCNT0:
370 case REG_MAINCNT1:
371 case REG_MAINCNT2:
372 case REG_MAINCNT3:
373 case REG_TESTMODE:
374 case REG_ASSOEAR0:
375 case REG_ASSOEAR1:
376 case REG_ASSOEAR2:
377 case REG_ASSOEAR3:
378 case REG_ASSOEAR4:
379 case REG_ASSOEAR5:
380 case REG_ASSOEAR6:
381 case REG_ASSOEAR7:
382 case REG_ASSOSAR0:
383 case REG_ASSOSAR1:
384 case REG_UNONCE0:
385 case REG_UNONCE1:
386 case REG_UNONCE2:
387 case REG_UNONCE3:
388 case REG_UNONCE4:
389 case REG_UNONCE5:
390 case REG_UNONCE6:
391 case REG_UNONCE7:
392 case REG_UNONCE8:
393 case REG_UNONCE9:
394 case REG_UNONCE10:
395 case REG_UNONCE11:
396 case REG_UNONCE12:
397 return true;
398 default:
399 return false;
400 }
401}
402
403static bool
404mrf24j40_long_reg_readable(struct device *dev, unsigned int reg)
405{
406 bool rc;
407
408 /* all writeable are also readable */
409 rc = mrf24j40_long_reg_writeable(dev, reg);
410 if (rc)
411 return rc;
412
413 /* readonly regs */
414 switch (reg) {
415 case REG_SLPCAL0:
416 case REG_SLPCAL1:
417 case REG_RFSTATE:
418 case REG_RSSI:
419 return true;
420 default:
421 return false;
422 }
423}
424
425static bool
426mrf24j40_long_reg_volatile(struct device *dev, unsigned int reg)
427{
428 /* can be changed during runtime */
429 switch (reg) {
430 case REG_SLPCAL0:
431 case REG_SLPCAL1:
432 case REG_SLPCAL2:
433 case REG_RFSTATE:
434 case REG_RSSI:
435 case REG_MAINCNT3:
436 return true;
437 default:
438 return false;
439 }
440}
441
442static const struct regmap_config mrf24j40_long_regmap = {
443 .name = "mrf24j40_long",
444 .reg_bits = 11,
445 .val_bits = 8,
446 .pad_bits = 5,
447 .write_flag_mask = MRF24J40_LONG_ACCESS,
448 .read_flag_mask = MRF24J40_LONG_ACCESS,
449 .cache_type = REGCACHE_RBTREE,
450 .max_register = MRF24J40_LONG_NUMREGS,
451 .writeable_reg = mrf24j40_long_reg_writeable,
452 .readable_reg = mrf24j40_long_reg_readable,
453 .volatile_reg = mrf24j40_long_reg_volatile,
454};
455
456static int mrf24j40_long_regmap_write(void *context, const void *data,
457 size_t count)
458{
459 struct spi_device *spi = context;
460 u8 buf[3];
461
462 if (count > 3)
463 return -EINVAL;
464
465 /* regmap supports read/write mask only in frist byte
466 * long write access need to set the 12th bit, so we
467 * make special handling for write.
468 */
469 memcpy(buf, data, count);
470 buf[1] |= (1 << 4);
471
472 return spi_write(spi, buf, count);
473}
474
475static int
476mrf24j40_long_regmap_read(void *context, const void *reg, size_t reg_size,
477 void *val, size_t val_size)
478{
479 struct spi_device *spi = context;
480
481 return spi_write_then_read(spi, reg, reg_size, val, val_size);
482}
483
484static const struct regmap_bus mrf24j40_long_regmap_bus = {
485 .write = mrf24j40_long_regmap_write,
486 .read = mrf24j40_long_regmap_read,
487 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
488 .val_format_endian_default = REGMAP_ENDIAN_BIG,
489};
490
Alexander Aring6844a0e2015-09-21 11:24:34 +0200491static void write_tx_buf_complete(void *context)
492{
493 struct mrf24j40 *devrec = context;
494 __le16 fc = ieee802154_get_fc_from_skb(devrec->tx_skb);
495 u8 val = 0x01;
496 int ret;
497
498 if (ieee802154_is_ackreq(fc))
499 val |= 0x04;
500
501 devrec->tx_post_msg.complete = NULL;
502 devrec->tx_post_buf[0] = MRF24J40_WRITESHORT(REG_TXNCON);
503 devrec->tx_post_buf[1] = val;
504
505 ret = spi_async(devrec->spi, &devrec->tx_post_msg);
506 if (ret)
507 dev_err(printdev(devrec), "SPI write Failed for transmit buf\n");
508}
509
Alan Ott3731a332012-09-02 15:44:13 +0000510/* This function relies on an undocumented write method. Once a write command
511 and address is set, as many bytes of data as desired can be clocked into
512 the device. The datasheet only shows setting one byte at a time. */
513static int write_tx_buf(struct mrf24j40 *devrec, u16 reg,
514 const u8 *data, size_t length)
515{
Alan Ott3731a332012-09-02 15:44:13 +0000516 u16 cmd;
Alexander Aring6844a0e2015-09-21 11:24:34 +0200517 int ret;
Alan Ott3731a332012-09-02 15:44:13 +0000518
519 /* Range check the length. 2 bytes are used for the length fields.*/
520 if (length > TX_FIFO_SIZE-2) {
521 dev_err(printdev(devrec), "write_tx_buf() was passed too large a buffer. Performing short write.\n");
522 length = TX_FIFO_SIZE-2;
523 }
524
Alan Ott3731a332012-09-02 15:44:13 +0000525 cmd = MRF24J40_WRITELONG(reg);
Alexander Aring6844a0e2015-09-21 11:24:34 +0200526 devrec->tx_hdr_buf[0] = cmd >> 8 & 0xff;
527 devrec->tx_hdr_buf[1] = cmd & 0xff;
528 devrec->tx_len_buf[0] = 0x0; /* Header Length. Set to 0 for now. TODO */
529 devrec->tx_len_buf[1] = length; /* Total length */
530 devrec->tx_buf_trx.tx_buf = data;
531 devrec->tx_buf_trx.len = length;
Alan Ott3731a332012-09-02 15:44:13 +0000532
Alexander Aring6844a0e2015-09-21 11:24:34 +0200533 ret = spi_async(devrec->spi, &devrec->tx_msg);
Alan Ott3731a332012-09-02 15:44:13 +0000534 if (ret)
535 dev_err(printdev(devrec), "SPI write Failed for TX buf\n");
536
Alan Ott3731a332012-09-02 15:44:13 +0000537 return ret;
538}
539
Alexander Aring6844a0e2015-09-21 11:24:34 +0200540static int mrf24j40_tx(struct ieee802154_hw *hw, struct sk_buff *skb)
541{
542 struct mrf24j40 *devrec = hw->priv;
543
544 dev_dbg(printdev(devrec), "tx packet of %d bytes\n", skb->len);
545 devrec->tx_skb = skb;
546
547 return write_tx_buf(devrec, 0x000, skb->data, skb->len);
548}
549
Alexander Aring5a504392014-10-25 17:16:34 +0200550static int mrf24j40_ed(struct ieee802154_hw *hw, u8 *level)
Alan Ott3731a332012-09-02 15:44:13 +0000551{
552 /* TODO: */
Varka Bhadramca079ad2014-09-24 12:21:32 +0200553 pr_warn("mrf24j40: ed not implemented\n");
Alan Ott3731a332012-09-02 15:44:13 +0000554 *level = 0;
555 return 0;
556}
557
Alexander Aring5a504392014-10-25 17:16:34 +0200558static int mrf24j40_start(struct ieee802154_hw *hw)
Alan Ott3731a332012-09-02 15:44:13 +0000559{
Alexander Aring5a504392014-10-25 17:16:34 +0200560 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000561
562 dev_dbg(printdev(devrec), "start\n");
563
Alexander Aring42c71482015-09-21 11:24:31 +0200564 /* Clear TXNIE and RXIE. Enable interrupts */
565 return regmap_update_bits(devrec->regmap_short, REG_INTCON,
566 0x01 | 0x08, 0x00);
Alan Ott3731a332012-09-02 15:44:13 +0000567}
568
Alexander Aring5a504392014-10-25 17:16:34 +0200569static void mrf24j40_stop(struct ieee802154_hw *hw)
Alan Ott3731a332012-09-02 15:44:13 +0000570{
Alexander Aring5a504392014-10-25 17:16:34 +0200571 struct mrf24j40 *devrec = hw->priv;
Varka Bhadram529160d2014-09-24 12:21:30 +0200572
Alan Ott3731a332012-09-02 15:44:13 +0000573 dev_dbg(printdev(devrec), "stop\n");
574
Alexander Aring42c71482015-09-21 11:24:31 +0200575 /* Set TXNIE and RXIE. Disable Interrupts */
576 regmap_update_bits(devrec->regmap_short, REG_INTCON, 0x01 | 0x08,
577 0x01 | 0x08);
Alan Ott3731a332012-09-02 15:44:13 +0000578}
579
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100580static int mrf24j40_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
Alan Ott3731a332012-09-02 15:44:13 +0000581{
Alexander Aring5a504392014-10-25 17:16:34 +0200582 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000583 u8 val;
584 int ret;
585
586 dev_dbg(printdev(devrec), "Set Channel %d\n", channel);
587
588 WARN_ON(page != 0);
589 WARN_ON(channel < MRF24J40_CHAN_MIN);
590 WARN_ON(channel > MRF24J40_CHAN_MAX);
591
592 /* Set Channel TODO */
593 val = (channel-11) << 4 | 0x03;
Alexander Aring42c71482015-09-21 11:24:31 +0200594 ret = regmap_update_bits(devrec->regmap_long, REG_RFCON0, 0xf0, val);
Alan Ott3731a332012-09-02 15:44:13 +0000595 if (ret)
596 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000597
Alexander Aring42c71482015-09-21 11:24:31 +0200598 /* RF Reset */
599 ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x04);
600 if (ret)
601 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000602
Alexander Aring42c71482015-09-21 11:24:31 +0200603 ret = regmap_update_bits(devrec->regmap_short, REG_RFCTL, 0x04, 0x00);
604 if (!ret)
605 udelay(SET_CHANNEL_DELAY_US); /* per datasheet */
606
607 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000608}
609
Alexander Aring5a504392014-10-25 17:16:34 +0200610static int mrf24j40_filter(struct ieee802154_hw *hw,
Alan Ott3731a332012-09-02 15:44:13 +0000611 struct ieee802154_hw_addr_filt *filt,
612 unsigned long changed)
613{
Alexander Aring5a504392014-10-25 17:16:34 +0200614 struct mrf24j40 *devrec = hw->priv;
Alan Ott3731a332012-09-02 15:44:13 +0000615
616 dev_dbg(printdev(devrec), "filter\n");
617
Alexander Aring57205c12014-10-25 05:25:09 +0200618 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000619 /* Short Addr */
620 u8 addrh, addrl;
Varka Bhadram529160d2014-09-24 12:21:30 +0200621
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100622 addrh = le16_to_cpu(filt->short_addr) >> 8 & 0xff;
623 addrl = le16_to_cpu(filt->short_addr) & 0xff;
Alan Ott3731a332012-09-02 15:44:13 +0000624
Alexander Aring42c71482015-09-21 11:24:31 +0200625 regmap_write(devrec->regmap_short, REG_SADRH, addrh);
626 regmap_write(devrec->regmap_short, REG_SADRL, addrl);
Alan Ott3731a332012-09-02 15:44:13 +0000627 dev_dbg(printdev(devrec),
628 "Set short addr to %04hx\n", filt->short_addr);
629 }
630
Alexander Aring57205c12014-10-25 05:25:09 +0200631 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000632 /* Device Address */
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100633 u8 i, addr[8];
634
635 memcpy(addr, &filt->ieee_addr, 8);
Alan Ott3731a332012-09-02 15:44:13 +0000636 for (i = 0; i < 8; i++)
Alexander Aring42c71482015-09-21 11:24:31 +0200637 regmap_write(devrec->regmap_short, REG_EADR0 + i,
638 addr[i]);
Alan Ott3731a332012-09-02 15:44:13 +0000639
640#ifdef DEBUG
Varka Bhadramca079ad2014-09-24 12:21:32 +0200641 pr_debug("Set long addr to: ");
Alan Ott3731a332012-09-02 15:44:13 +0000642 for (i = 0; i < 8; i++)
Varka Bhadramca079ad2014-09-24 12:21:32 +0200643 pr_debug("%02hhx ", addr[7 - i]);
644 pr_debug("\n");
Alan Ott3731a332012-09-02 15:44:13 +0000645#endif
646 }
647
Alexander Aring57205c12014-10-25 05:25:09 +0200648 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000649 /* PAN ID */
650 u8 panidl, panidh;
Varka Bhadram529160d2014-09-24 12:21:30 +0200651
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +0100652 panidh = le16_to_cpu(filt->pan_id) >> 8 & 0xff;
653 panidl = le16_to_cpu(filt->pan_id) & 0xff;
Alexander Aring42c71482015-09-21 11:24:31 +0200654 regmap_write(devrec->regmap_short, REG_PANIDH, panidh);
655 regmap_write(devrec->regmap_short, REG_PANIDL, panidl);
Alan Ott3731a332012-09-02 15:44:13 +0000656
657 dev_dbg(printdev(devrec), "Set PANID to %04hx\n", filt->pan_id);
658 }
659
Alexander Aring57205c12014-10-25 05:25:09 +0200660 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
Alan Ott3731a332012-09-02 15:44:13 +0000661 /* Pan Coordinator */
662 u8 val;
663 int ret;
664
Alexander Aring42c71482015-09-21 11:24:31 +0200665 if (filt->pan_coord)
666 val = 0x8;
667 else
668 val = 0x0;
669 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x8,
670 val);
Alan Ott3731a332012-09-02 15:44:13 +0000671 if (ret)
672 return ret;
Alan Ott3731a332012-09-02 15:44:13 +0000673
674 /* REG_SLOTTED is maintained as default (unslotted/CSMA-CA).
675 * REG_ORDER is maintained as default (no beacon/superframe).
676 */
677
678 dev_dbg(printdev(devrec), "Set Pan Coord to %s\n",
Stefan Schmidtce261bc2014-12-12 12:45:33 +0100679 filt->pan_coord ? "on" : "off");
Alan Ott3731a332012-09-02 15:44:13 +0000680 }
681
682 return 0;
683}
684
Alexander Aringc91a3012015-09-21 11:24:35 +0200685static void mrf24j40_handle_rx_read_buf_unlock(struct mrf24j40 *devrec)
Alan Ott3731a332012-09-02 15:44:13 +0000686{
Alexander Aringc91a3012015-09-21 11:24:35 +0200687 int ret;
688
689 /* Turn back on reception of packets off the air. */
690 devrec->rx_msg.complete = NULL;
691 devrec->rx_buf[0] = MRF24J40_WRITESHORT(REG_BBREG1);
692 devrec->rx_buf[1] = 0x00; /* CLR RXDECINV */
693 ret = spi_async(devrec->spi, &devrec->rx_msg);
694 if (ret)
695 dev_err(printdev(devrec), "failed to unlock rx buffer\n");
696}
697
698static void mrf24j40_handle_rx_read_buf_complete(void *context)
699{
700 struct mrf24j40 *devrec = context;
701 u8 len = devrec->rx_buf[2];
702 u8 rx_local_buf[RX_FIFO_SIZE];
Alan Ott3731a332012-09-02 15:44:13 +0000703 struct sk_buff *skb;
704
Alexander Aringc91a3012015-09-21 11:24:35 +0200705 memcpy(rx_local_buf, devrec->rx_fifo_buf, len);
706 mrf24j40_handle_rx_read_buf_unlock(devrec);
Alan Ott3731a332012-09-02 15:44:13 +0000707
Alexander Aringc91a3012015-09-21 11:24:35 +0200708 skb = dev_alloc_skb(IEEE802154_MTU);
Alan Ott3731a332012-09-02 15:44:13 +0000709 if (!skb) {
Alexander Aringc91a3012015-09-21 11:24:35 +0200710 dev_err(printdev(devrec), "failed to allocate skb\n");
711 return;
Alan Ott3731a332012-09-02 15:44:13 +0000712 }
713
Alexander Aringc91a3012015-09-21 11:24:35 +0200714 memcpy(skb_put(skb, len), rx_local_buf, len);
715 ieee802154_rx_irqsafe(devrec->hw, skb, 0);
716
717#ifdef DEBUG
718 print_hex_dump(KERN_DEBUG, "mrf24j40 rx: ", DUMP_PREFIX_OFFSET, 16, 1,
719 rx_local_buf, len, 0);
720 pr_debug("mrf24j40 rx: lqi: %02hhx rssi: %02hhx\n",
721 devrec->rx_lqi_buf[0], devrec->rx_lqi_buf[1]);
722#endif
723}
724
725static void mrf24j40_handle_rx_read_buf(void *context)
726{
727 struct mrf24j40 *devrec = context;
728 u16 cmd;
729 int ret;
730
731 /* if length is invalid read the full MTU */
732 if (!ieee802154_is_valid_psdu_len(devrec->rx_buf[2]))
733 devrec->rx_buf[2] = IEEE802154_MTU;
734
735 cmd = MRF24J40_READLONG(REG_RX_FIFO + 1);
736 devrec->rx_addr_buf[0] = cmd >> 8 & 0xff;
737 devrec->rx_addr_buf[1] = cmd & 0xff;
738 devrec->rx_fifo_buf_trx.len = devrec->rx_buf[2];
739 ret = spi_async(devrec->spi, &devrec->rx_buf_msg);
740 if (ret) {
741 dev_err(printdev(devrec), "failed to read rx buffer\n");
742 mrf24j40_handle_rx_read_buf_unlock(devrec);
Alan Ott3731a332012-09-02 15:44:13 +0000743 }
Alexander Aringc91a3012015-09-21 11:24:35 +0200744}
Alan Ott3731a332012-09-02 15:44:13 +0000745
Alexander Aringc91a3012015-09-21 11:24:35 +0200746static void mrf24j40_handle_rx_read_len(void *context)
747{
748 struct mrf24j40 *devrec = context;
749 u16 cmd;
750 int ret;
Alan Ott3731a332012-09-02 15:44:13 +0000751
Alexander Aringc91a3012015-09-21 11:24:35 +0200752 /* read the length of received frame */
753 devrec->rx_msg.complete = mrf24j40_handle_rx_read_buf;
754 devrec->rx_trx.len = 3;
755 cmd = MRF24J40_READLONG(REG_RX_FIFO);
756 devrec->rx_buf[0] = cmd >> 8 & 0xff;
757 devrec->rx_buf[1] = cmd & 0xff;
Alan Ott3731a332012-09-02 15:44:13 +0000758
Alexander Aringc91a3012015-09-21 11:24:35 +0200759 ret = spi_async(devrec->spi, &devrec->rx_msg);
760 if (ret) {
761 dev_err(printdev(devrec), "failed to read rx buffer length\n");
762 mrf24j40_handle_rx_read_buf_unlock(devrec);
763 }
764}
Alan Ott3731a332012-09-02 15:44:13 +0000765
Alexander Aringc91a3012015-09-21 11:24:35 +0200766static int mrf24j40_handle_rx(struct mrf24j40 *devrec)
767{
768 /* Turn off reception of packets off the air. This prevents the
769 * device from overwriting the buffer while we're reading it.
770 */
771 devrec->rx_msg.complete = mrf24j40_handle_rx_read_len;
772 devrec->rx_trx.len = 2;
773 devrec->rx_buf[0] = MRF24J40_WRITESHORT(REG_BBREG1);
774 devrec->rx_buf[1] = 0x04; /* SET RXDECINV */
775
776 return spi_async(devrec->spi, &devrec->rx_msg);
Alan Ott3731a332012-09-02 15:44:13 +0000777}
778
Alexander Aring2323cf32015-09-21 11:24:37 +0200779static int
780mrf24j40_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
781 u8 retries)
782{
783 struct mrf24j40 *devrec = hw->priv;
784 u8 val;
785
786 /* min_be */
787 val = min_be << 3;
788 /* csma backoffs */
789 val |= retries;
790
791 return regmap_update_bits(devrec->regmap_short, REG_TXMCR, 0x1f, val);
792}
793
Alexander Aringf1d78122015-09-21 11:24:38 +0200794static int mrf24j40_set_cca_mode(struct ieee802154_hw *hw,
795 const struct wpan_phy_cca *cca)
796{
797 struct mrf24j40 *devrec = hw->priv;
798 u8 val;
799
800 /* mapping 802.15.4 to driver spec */
801 switch (cca->mode) {
802 case NL802154_CCA_ENERGY:
803 val = 2;
804 break;
805 case NL802154_CCA_CARRIER:
806 val = 1;
807 break;
808 case NL802154_CCA_ENERGY_CARRIER:
809 switch (cca->opt) {
810 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
811 val = 3;
812 break;
813 default:
814 return -EINVAL;
815 }
816 break;
817 default:
818 return -EINVAL;
819 }
820
821 return regmap_update_bits(devrec->regmap_short, REG_BBREG2, 0xc0,
822 val << 6);
823}
824
Alexander Aringe33a0f92015-09-21 11:24:39 +0200825/* array for representing ed levels */
826static const s32 mrf24j40_ed_levels[] = {
827 -9000, -8900, -8800, -8700, -8600, -8500, -8400, -8300, -8200, -8100,
828 -8000, -7900, -7800, -7700, -7600, -7500, -7400, -7300, -7200, -7100,
829 -7000, -6900, -6800, -6700, -6600, -6500, -6400, -6300, -6200, -6100,
830 -6000, -5900, -5800, -5700, -5600, -5500, -5400, -5300, -5200, -5100,
831 -5000, -4900, -4800, -4700, -4600, -4500, -4400, -4300, -4200, -4100,
832 -4000, -3900, -3800, -3700, -3600, -3500
833};
834
835/* map ed levels to register value */
836static const s32 mrf24j40_ed_levels_map[][2] = {
837 { -9000, 0 }, { -8900, 1 }, { -8800, 2 }, { -8700, 5 }, { -8600, 9 },
838 { -8500, 13 }, { -8400, 18 }, { -8300, 23 }, { -8200, 27 },
839 { -8100, 32 }, { -8000, 37 }, { -7900, 43 }, { -7800, 48 },
840 { -7700, 53 }, { -7600, 58 }, { -7500, 63 }, { -7400, 68 },
841 { -7300, 73 }, { -7200, 78 }, { -7100, 83 }, { -7000, 89 },
842 { -6900, 95 }, { -6800, 100 }, { -6700, 107 }, { -6600, 111 },
843 { -6500, 117 }, { -6400, 121 }, { -6300, 125 }, { -6200, 129 },
844 { -6100, 133 }, { -6000, 138 }, { -5900, 143 }, { -5800, 148 },
845 { -5700, 153 }, { -5600, 159 }, { -5500, 165 }, { -5400, 170 },
846 { -5300, 176 }, { -5200, 183 }, { -5100, 188 }, { -5000, 193 },
847 { -4900, 198 }, { -4800, 203 }, { -4700, 207 }, { -4600, 212 },
848 { -4500, 216 }, { -4400, 221 }, { -4300, 225 }, { -4200, 228 },
849 { -4100, 233 }, { -4000, 239 }, { -3900, 245 }, { -3800, 250 },
850 { -3700, 253 }, { -3600, 254 }, { -3500, 255 },
851};
852
853static int mrf24j40_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
854{
855 struct mrf24j40 *devrec = hw->priv;
856 int i;
857
858 for (i = 0; i < ARRAY_SIZE(mrf24j40_ed_levels_map); i++) {
859 if (mrf24j40_ed_levels_map[i][0] == mbm)
860 return regmap_write(devrec->regmap_short, REG_CCAEDTH,
861 mrf24j40_ed_levels_map[i][1]);
862 }
863
864 return -EINVAL;
865}
866
Alexander Aring00250f72015-09-21 11:24:40 +0200867static const s32 mrf24j40ma_powers[] = {
868 0, -50, -120, -190, -280, -370, -490, -630, -1000, -1050, -1120, -1190,
869 -1280, -1370, -1490, -1630, -2000, -2050, -2120, -2190, -2280, -2370,
870 -2490, -2630, -3000, -3050, -3120, -3190, -3280, -3370, -3490, -3630,
871};
872
873static int mrf24j40_set_txpower(struct ieee802154_hw *hw, s32 mbm)
874{
875 struct mrf24j40 *devrec = hw->priv;
876 s32 small_scale;
877 u8 val;
878
879 if (0 >= mbm && mbm > -1000) {
880 val = 0;
881 small_scale = mbm;
882 } else if (-1000 >= mbm && mbm > -2000) {
883 val = 0x40;
884 small_scale = mbm + 1000;
885 } else if (-2000 >= mbm && mbm > -3000) {
886 val = 0x80;
887 small_scale = mbm + 2000;
888 } else if (-3000 >= mbm && mbm > -4000) {
889 val = 0xc0;
890 small_scale = mbm + 3000;
891 } else {
892 return -EINVAL;
893 }
894
895 switch (small_scale) {
896 case 0:
897 break;
898 case -50:
899 val |= 0x08;
900 break;
901 case -120:
902 val |= 0x10;
903 break;
904 case -190:
905 val |= 0x18;
906 break;
907 case -280:
908 val |= 0x20;
909 break;
910 case -370:
911 val |= 0x28;
912 break;
913 case -490:
914 val |= 0x30;
915 break;
916 case -630:
917 val |= 0x38;
918 break;
919 default:
920 return -EINVAL;
921 }
922
923 return regmap_update_bits(devrec->regmap_long, REG_RFCON3, 0xf8, val);
924}
925
Alexander Aring8ba40412015-09-21 11:24:41 +0200926static int mrf24j40_set_promiscuous_mode(struct ieee802154_hw *hw, bool on)
927{
928 struct mrf24j40 *devrec = hw->priv;
929 int ret;
930
931 if (on) {
932 /* set PROMI, ERRPKT and NOACKRSP */
933 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x23,
934 0x23);
935 } else {
936 /* clear PROMI, ERRPKT and NOACKRSP */
937 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x23,
938 0x00);
939 }
940
941 return ret;
942}
943
Alexander Aring16301862014-10-28 18:21:18 +0100944static const struct ieee802154_ops mrf24j40_ops = {
Alan Ott3731a332012-09-02 15:44:13 +0000945 .owner = THIS_MODULE,
Alexander Aring6844a0e2015-09-21 11:24:34 +0200946 .xmit_async = mrf24j40_tx,
Alan Ott3731a332012-09-02 15:44:13 +0000947 .ed = mrf24j40_ed,
948 .start = mrf24j40_start,
949 .stop = mrf24j40_stop,
950 .set_channel = mrf24j40_set_channel,
951 .set_hw_addr_filt = mrf24j40_filter,
Alexander Aring2323cf32015-09-21 11:24:37 +0200952 .set_csma_params = mrf24j40_csma_params,
Alexander Aringf1d78122015-09-21 11:24:38 +0200953 .set_cca_mode = mrf24j40_set_cca_mode,
Alexander Aringe33a0f92015-09-21 11:24:39 +0200954 .set_cca_ed_level = mrf24j40_set_cca_ed_level,
Alexander Aring00250f72015-09-21 11:24:40 +0200955 .set_txpower = mrf24j40_set_txpower,
Alexander Aring8ba40412015-09-21 11:24:41 +0200956 .set_promiscuous_mode = mrf24j40_set_promiscuous_mode,
Alan Ott3731a332012-09-02 15:44:13 +0000957};
958
Alexander Aring37441612015-09-21 11:24:36 +0200959static void mrf24j40_intstat_complete(void *context)
Alan Ott3731a332012-09-02 15:44:13 +0000960{
Alexander Aring37441612015-09-21 11:24:36 +0200961 struct mrf24j40 *devrec = context;
962 u8 intstat = devrec->irq_buf[1];
Alan Ott3731a332012-09-02 15:44:13 +0000963
Alexander Aring37441612015-09-21 11:24:36 +0200964 enable_irq(devrec->spi->irq);
Alan Ott3731a332012-09-02 15:44:13 +0000965
966 /* Check for TX complete */
967 if (intstat & 0x1)
Alexander Aring6844a0e2015-09-21 11:24:34 +0200968 ieee802154_xmit_complete(devrec->hw, devrec->tx_skb, false);
Alan Ott3731a332012-09-02 15:44:13 +0000969
970 /* Check for Rx */
971 if (intstat & 0x8)
972 mrf24j40_handle_rx(devrec);
Alexander Aring37441612015-09-21 11:24:36 +0200973}
Alan Ott3731a332012-09-02 15:44:13 +0000974
Alexander Aring37441612015-09-21 11:24:36 +0200975static irqreturn_t mrf24j40_isr(int irq, void *data)
976{
977 struct mrf24j40 *devrec = data;
978 int ret;
979
980 disable_irq_nosync(irq);
981
982 devrec->irq_buf[0] = MRF24J40_READSHORT(REG_INTSTAT);
983 /* Read the interrupt status */
984 ret = spi_async(devrec->spi, &devrec->irq_msg);
985 if (ret) {
986 enable_irq(irq);
987 return IRQ_NONE;
988 }
989
Alan Ott4a4e1da2013-10-05 23:52:23 -0400990 return IRQ_HANDLED;
Alan Ott3731a332012-09-02 15:44:13 +0000991}
992
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530993static int mrf24j40_hw_init(struct mrf24j40 *devrec)
994{
995 int ret;
Varka Bhadram3dac9a72014-06-16 09:12:31 +0530996
997 /* Initialize the device.
998 From datasheet section 3.2: Initialization. */
Alexander Aring42c71482015-09-21 11:24:31 +0200999 ret = regmap_write(devrec->regmap_short, REG_SOFTRST, 0x07);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301000 if (ret)
1001 goto err_ret;
1002
Alexander Aring42c71482015-09-21 11:24:31 +02001003 ret = regmap_write(devrec->regmap_short, REG_PACON2, 0x98);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301004 if (ret)
1005 goto err_ret;
1006
Alexander Aring42c71482015-09-21 11:24:31 +02001007 ret = regmap_write(devrec->regmap_short, REG_TXSTBL, 0x95);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301008 if (ret)
1009 goto err_ret;
1010
Alexander Aring42c71482015-09-21 11:24:31 +02001011 ret = regmap_write(devrec->regmap_long, REG_RFCON0, 0x03);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301012 if (ret)
1013 goto err_ret;
1014
Alexander Aring42c71482015-09-21 11:24:31 +02001015 ret = regmap_write(devrec->regmap_long, REG_RFCON1, 0x01);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301016 if (ret)
1017 goto err_ret;
1018
Alexander Aring42c71482015-09-21 11:24:31 +02001019 ret = regmap_write(devrec->regmap_long, REG_RFCON2, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301020 if (ret)
1021 goto err_ret;
1022
Alexander Aring42c71482015-09-21 11:24:31 +02001023 ret = regmap_write(devrec->regmap_long, REG_RFCON6, 0x90);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301024 if (ret)
1025 goto err_ret;
1026
Alexander Aring42c71482015-09-21 11:24:31 +02001027 ret = regmap_write(devrec->regmap_long, REG_RFCON7, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301028 if (ret)
1029 goto err_ret;
1030
Alexander Aring42c71482015-09-21 11:24:31 +02001031 ret = regmap_write(devrec->regmap_long, REG_RFCON8, 0x10);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301032 if (ret)
1033 goto err_ret;
1034
Alexander Aring42c71482015-09-21 11:24:31 +02001035 ret = regmap_write(devrec->regmap_long, REG_SLPCON1, 0x21);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301036 if (ret)
1037 goto err_ret;
1038
Alexander Aring42c71482015-09-21 11:24:31 +02001039 ret = regmap_write(devrec->regmap_short, REG_BBREG2, 0x80);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301040 if (ret)
1041 goto err_ret;
1042
Alexander Aring42c71482015-09-21 11:24:31 +02001043 ret = regmap_write(devrec->regmap_short, REG_CCAEDTH, 0x60);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301044 if (ret)
1045 goto err_ret;
1046
Alexander Aring42c71482015-09-21 11:24:31 +02001047 ret = regmap_write(devrec->regmap_short, REG_BBREG6, 0x40);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301048 if (ret)
1049 goto err_ret;
1050
Alexander Aring42c71482015-09-21 11:24:31 +02001051 ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x04);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301052 if (ret)
1053 goto err_ret;
1054
Alexander Aring42c71482015-09-21 11:24:31 +02001055 ret = regmap_write(devrec->regmap_short, REG_RFCTL, 0x0);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301056 if (ret)
1057 goto err_ret;
1058
1059 udelay(192);
1060
1061 /* Set RX Mode. RXMCR<1:0>: 0x0 normal, 0x1 promisc, 0x2 error */
Alexander Aring42c71482015-09-21 11:24:31 +02001062 ret = regmap_update_bits(devrec->regmap_short, REG_RXMCR, 0x03, 0x00);
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301063 if (ret)
1064 goto err_ret;
1065
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001066 if (spi_get_device_id(devrec->spi)->driver_data == MRF24J40MC) {
1067 /* Enable external amplifier.
1068 * From MRF24J40MC datasheet section 1.3: Operation.
1069 */
Alexander Aring42c71482015-09-21 11:24:31 +02001070 regmap_update_bits(devrec->regmap_long, REG_TESTMODE, 0x07,
1071 0x07);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001072
Alexander Aring42c71482015-09-21 11:24:31 +02001073 /* Set GPIO3 as output. */
1074 regmap_update_bits(devrec->regmap_short, REG_TRISGPIO, 0x08,
1075 0x08);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001076
Alexander Aring42c71482015-09-21 11:24:31 +02001077 /* Set GPIO3 HIGH to enable U5 voltage regulator */
1078 regmap_update_bits(devrec->regmap_short, REG_GPIO, 0x08, 0x08);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001079
1080 /* Reduce TX pwr to meet FCC requirements.
1081 * From MRF24J40MC datasheet section 3.1.1
1082 */
Alexander Aring42c71482015-09-21 11:24:31 +02001083 regmap_write(devrec->regmap_long, REG_RFCON3, 0x28);
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001084 }
1085
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301086 return 0;
1087
1088err_ret:
1089 return ret;
1090}
1091
Alexander Aring6844a0e2015-09-21 11:24:34 +02001092static void
1093mrf24j40_setup_tx_spi_messages(struct mrf24j40 *devrec)
1094{
1095 spi_message_init(&devrec->tx_msg);
1096 devrec->tx_msg.context = devrec;
1097 devrec->tx_msg.complete = write_tx_buf_complete;
1098 devrec->tx_hdr_trx.len = 2;
1099 devrec->tx_hdr_trx.tx_buf = devrec->tx_hdr_buf;
1100 spi_message_add_tail(&devrec->tx_hdr_trx, &devrec->tx_msg);
1101 devrec->tx_len_trx.len = 2;
1102 devrec->tx_len_trx.tx_buf = devrec->tx_len_buf;
1103 spi_message_add_tail(&devrec->tx_len_trx, &devrec->tx_msg);
1104 spi_message_add_tail(&devrec->tx_buf_trx, &devrec->tx_msg);
1105
1106 spi_message_init(&devrec->tx_post_msg);
1107 devrec->tx_post_msg.context = devrec;
1108 devrec->tx_post_trx.len = 2;
1109 devrec->tx_post_trx.tx_buf = devrec->tx_post_buf;
1110 spi_message_add_tail(&devrec->tx_post_trx, &devrec->tx_post_msg);
1111}
1112
Alexander Aringc91a3012015-09-21 11:24:35 +02001113static void
1114mrf24j40_setup_rx_spi_messages(struct mrf24j40 *devrec)
1115{
1116 spi_message_init(&devrec->rx_msg);
1117 devrec->rx_msg.context = devrec;
1118 devrec->rx_trx.len = 2;
1119 devrec->rx_trx.tx_buf = devrec->rx_buf;
1120 devrec->rx_trx.rx_buf = devrec->rx_buf;
1121 spi_message_add_tail(&devrec->rx_trx, &devrec->rx_msg);
1122
1123 spi_message_init(&devrec->rx_buf_msg);
1124 devrec->rx_buf_msg.context = devrec;
1125 devrec->rx_buf_msg.complete = mrf24j40_handle_rx_read_buf_complete;
1126 devrec->rx_addr_trx.len = 2;
1127 devrec->rx_addr_trx.tx_buf = devrec->rx_addr_buf;
1128 spi_message_add_tail(&devrec->rx_addr_trx, &devrec->rx_buf_msg);
1129 devrec->rx_fifo_buf_trx.rx_buf = devrec->rx_fifo_buf;
1130 spi_message_add_tail(&devrec->rx_fifo_buf_trx, &devrec->rx_buf_msg);
1131 devrec->rx_lqi_trx.len = 2;
1132 devrec->rx_lqi_trx.rx_buf = devrec->rx_lqi_buf;
1133 spi_message_add_tail(&devrec->rx_lqi_trx, &devrec->rx_buf_msg);
1134}
1135
Alexander Aring37441612015-09-21 11:24:36 +02001136static void
1137mrf24j40_setup_irq_spi_messages(struct mrf24j40 *devrec)
1138{
1139 spi_message_init(&devrec->irq_msg);
1140 devrec->irq_msg.context = devrec;
1141 devrec->irq_msg.complete = mrf24j40_intstat_complete;
1142 devrec->irq_trx.len = 2;
1143 devrec->irq_trx.tx_buf = devrec->irq_buf;
1144 devrec->irq_trx.rx_buf = devrec->irq_buf;
1145 spi_message_add_tail(&devrec->irq_trx, &devrec->irq_msg);
1146}
1147
Alexander Aring766928f2015-09-21 11:24:27 +02001148static void mrf24j40_phy_setup(struct mrf24j40 *devrec)
1149{
Alexander Aringd344c912015-09-21 11:24:28 +02001150 ieee802154_random_extended_addr(&devrec->hw->phy->perm_extended_addr);
Alexander Aring766928f2015-09-21 11:24:27 +02001151 devrec->hw->phy->current_channel = 11;
Alexander Aring2323cf32015-09-21 11:24:37 +02001152
1153 /* mrf24j40 supports max_minbe 0 - 3 */
1154 devrec->hw->phy->supported.max_minbe = 3;
1155 /* datasheet doesn't say anything about max_be, but we have min_be
1156 * So we assume the max_be default.
1157 */
1158 devrec->hw->phy->supported.min_maxbe = 5;
1159 devrec->hw->phy->supported.max_maxbe = 5;
Alexander Aringf1d78122015-09-21 11:24:38 +02001160
1161 devrec->hw->phy->cca.mode = NL802154_CCA_CARRIER;;
1162 devrec->hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
1163 BIT(NL802154_CCA_CARRIER) |
1164 BIT(NL802154_CCA_ENERGY_CARRIER);
1165 devrec->hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND);
Alexander Aringe33a0f92015-09-21 11:24:39 +02001166
1167 devrec->hw->phy->cca_ed_level = -6900;
1168 devrec->hw->phy->supported.cca_ed_levels = mrf24j40_ed_levels;
1169 devrec->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(mrf24j40_ed_levels);
Alexander Aring00250f72015-09-21 11:24:40 +02001170
1171 switch (spi_get_device_id(devrec->spi)->driver_data) {
1172 case MRF24J40:
1173 case MRF24J40MA:
1174 devrec->hw->phy->supported.tx_powers = mrf24j40ma_powers;
1175 devrec->hw->phy->supported.tx_powers_size = ARRAY_SIZE(mrf24j40ma_powers);
1176 devrec->hw->phy->flags |= WPAN_PHY_FLAG_TXPOWER;
1177 break;
1178 default:
1179 break;
1180 }
Alexander Aring766928f2015-09-21 11:24:27 +02001181}
1182
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001183static int mrf24j40_probe(struct spi_device *spi)
Alan Ott3731a332012-09-02 15:44:13 +00001184{
1185 int ret = -ENOMEM;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001186 struct ieee802154_hw *hw;
Alan Ott3731a332012-09-02 15:44:13 +00001187 struct mrf24j40 *devrec;
1188
Varka Bhadramca079ad2014-09-24 12:21:32 +02001189 dev_info(&spi->dev, "probe(). IRQ: %d\n", spi->irq);
Alan Ott3731a332012-09-02 15:44:13 +00001190
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001191 /* Register with the 802154 subsystem */
1192
1193 hw = ieee802154_alloc_hw(sizeof(*devrec), &mrf24j40_ops);
1194 if (!hw)
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301195 goto err_ret;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001196
1197 devrec = hw->priv;
1198 devrec->spi = spi;
1199 spi_set_drvdata(spi, devrec);
1200 devrec->hw = hw;
1201 devrec->hw->parent = &spi->dev;
1202 devrec->hw->phy->supported.channels[0] = CHANNEL_MASK;
Alexander Aring2323cf32015-09-21 11:24:37 +02001203 devrec->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
Alexander Aring8ba40412015-09-21 11:24:41 +02001204 IEEE802154_HW_CSMA_PARAMS |
1205 IEEE802154_HW_PROMISCUOUS;
Alexander Aringb2cfdf32015-09-21 11:24:23 +02001206
Alexander Aringe33a0f92015-09-21 11:24:39 +02001207 devrec->hw->phy->flags = WPAN_PHY_FLAG_CCA_MODE |
1208 WPAN_PHY_FLAG_CCA_ED_LEVEL;
Alexander Aringf1d78122015-09-21 11:24:38 +02001209
Alexander Aring6844a0e2015-09-21 11:24:34 +02001210 mrf24j40_setup_tx_spi_messages(devrec);
Alexander Aringc91a3012015-09-21 11:24:35 +02001211 mrf24j40_setup_rx_spi_messages(devrec);
Alexander Aring37441612015-09-21 11:24:36 +02001212 mrf24j40_setup_irq_spi_messages(devrec);
Alexander Aring6844a0e2015-09-21 11:24:34 +02001213
Alexander Aringb0156792015-09-21 11:24:30 +02001214 devrec->regmap_short = devm_regmap_init_spi(spi,
1215 &mrf24j40_short_regmap);
1216 if (IS_ERR(devrec->regmap_short)) {
1217 ret = PTR_ERR(devrec->regmap_short);
1218 dev_err(&spi->dev, "Failed to allocate short register map: %d\n",
1219 ret);
1220 goto err_register_device;
1221 }
1222
1223 devrec->regmap_long = devm_regmap_init(&spi->dev,
1224 &mrf24j40_long_regmap_bus,
1225 spi, &mrf24j40_long_regmap);
1226 if (IS_ERR(devrec->regmap_long)) {
1227 ret = PTR_ERR(devrec->regmap_long);
1228 dev_err(&spi->dev, "Failed to allocate long register map: %d\n",
1229 ret);
1230 goto err_register_device;
1231 }
1232
Alexander Aring78aedb62015-09-21 11:24:25 +02001233 if (spi->max_speed_hz > MAX_SPI_SPEED_HZ) {
1234 dev_warn(&spi->dev, "spi clock above possible maximum: %d",
1235 MAX_SPI_SPEED_HZ);
1236 return -EINVAL;
1237 }
Alan Ott3731a332012-09-02 15:44:13 +00001238
Varka Bhadram3dac9a72014-06-16 09:12:31 +05301239 ret = mrf24j40_hw_init(devrec);
Alan Ott3731a332012-09-02 15:44:13 +00001240 if (ret)
Alexander Aringa339e182015-09-21 11:24:24 +02001241 goto err_register_device;
Alan Ott3731a332012-09-02 15:44:13 +00001242
Alexander Aring766928f2015-09-21 11:24:27 +02001243 mrf24j40_phy_setup(devrec);
1244
Alexander Aring37441612015-09-21 11:24:36 +02001245 ret = devm_request_irq(&spi->dev, spi->irq, mrf24j40_isr,
1246 IRQF_TRIGGER_LOW, dev_name(&spi->dev),
1247 devrec);
Alan Ott3731a332012-09-02 15:44:13 +00001248 if (ret) {
1249 dev_err(printdev(devrec), "Unable to get IRQ");
Alexander Aringa339e182015-09-21 11:24:24 +02001250 goto err_register_device;
Alan Ott3731a332012-09-02 15:44:13 +00001251 }
1252
Alexander Aringa339e182015-09-21 11:24:24 +02001253 dev_dbg(printdev(devrec), "registered mrf24j40\n");
1254 ret = ieee802154_register_hw(devrec->hw);
1255 if (ret)
1256 goto err_register_device;
1257
Alan Ott3731a332012-09-02 15:44:13 +00001258 return 0;
1259
Alan Ott3731a332012-09-02 15:44:13 +00001260err_register_device:
Alexander Aring5a504392014-10-25 17:16:34 +02001261 ieee802154_free_hw(devrec->hw);
Varka Bhadram0aaf43f2014-06-11 10:04:44 +05301262err_ret:
Alan Ott3731a332012-09-02 15:44:13 +00001263 return ret;
1264}
1265
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001266static int mrf24j40_remove(struct spi_device *spi)
Alan Ott3731a332012-09-02 15:44:13 +00001267{
Jingoo Han4fa0a0e2013-04-05 20:34:18 +00001268 struct mrf24j40 *devrec = spi_get_drvdata(spi);
Alan Ott3731a332012-09-02 15:44:13 +00001269
1270 dev_dbg(printdev(devrec), "remove\n");
1271
Alexander Aring5a504392014-10-25 17:16:34 +02001272 ieee802154_unregister_hw(devrec->hw);
1273 ieee802154_free_hw(devrec->hw);
Alan Ott3731a332012-09-02 15:44:13 +00001274 /* TODO: Will ieee802154_free_device() wait until ->xmit() is
1275 * complete? */
1276
Alan Ott3731a332012-09-02 15:44:13 +00001277 return 0;
1278}
1279
Alexander Aring2e6fd642015-09-21 11:24:26 +02001280static const struct of_device_id mrf24j40_of_match[] = {
1281 { .compatible = "microchip,mrf24j40", .data = (void *)MRF24J40 },
1282 { .compatible = "microchip,mrf24j40ma", .data = (void *)MRF24J40MA },
1283 { .compatible = "microchip,mrf24j40mc", .data = (void *)MRF24J40MC },
1284 { },
1285};
1286MODULE_DEVICE_TABLE(of, mrf24j40_of_match);
1287
Alan Ott3731a332012-09-02 15:44:13 +00001288static const struct spi_device_id mrf24j40_ids[] = {
Simon Vincentdb9e0ee2014-10-06 10:39:45 +01001289 { "mrf24j40", MRF24J40 },
1290 { "mrf24j40ma", MRF24J40MA },
1291 { "mrf24j40mc", MRF24J40MC },
Alan Ott3731a332012-09-02 15:44:13 +00001292 { },
1293};
1294MODULE_DEVICE_TABLE(spi, mrf24j40_ids);
1295
1296static struct spi_driver mrf24j40_driver = {
1297 .driver = {
Alexander Aring2e6fd642015-09-21 11:24:26 +02001298 .of_match_table = of_match_ptr(mrf24j40_of_match),
Alan Ott3731a332012-09-02 15:44:13 +00001299 .name = "mrf24j40",
Alan Ott3731a332012-09-02 15:44:13 +00001300 .owner = THIS_MODULE,
1301 },
1302 .id_table = mrf24j40_ids,
1303 .probe = mrf24j40_probe,
Bill Pembertonbb1f4602012-12-03 09:24:12 -05001304 .remove = mrf24j40_remove,
Alan Ott3731a332012-09-02 15:44:13 +00001305};
1306
Wei Yongjun3d4a1312013-04-08 20:34:44 +00001307module_spi_driver(mrf24j40_driver);
Alan Ott3731a332012-09-02 15:44:13 +00001308
1309MODULE_LICENSE("GPL");
1310MODULE_AUTHOR("Alan Ott");
1311MODULE_DESCRIPTION("MRF24J40 SPI 802.15.4 Controller Driver");