1 // SPDX-License-Identifier: GPL-2.0+
3 * abstraction of the spi interface of HopeRf rf69 radio module
5 * Copyright (C) 2016 Wolf-Entwicklungen
6 * Marcus Wolf <linux@wolf-entwicklungen.de>
9 /* enable prosa debug info */
11 /* enable print of values on reg access */
13 /* enable print of values on fifo access */
14 #undef DEBUG_FIFO_ACCESS
16 #include <linux/types.h>
17 #include <linux/spi/spi.h>
20 #include "rf69_registers.h"
22 #define F_OSC 32000000 /* in Hz */
23 #define FIFO_SIZE 66 /* in byte */
25 /*-------------------------------------------------------------------------*/
27 static u8
rf69_read_reg(struct spi_device
*spi
, u8 addr
)
31 retval
= spi_w8r8(spi
, addr
);
36 * should never happen, since we already checked,
37 * that module is connected. Therefore no error
38 * handling, just an optional error message...
40 dev_dbg(&spi
->dev
, "read 0x%x FAILED\n", addr
);
42 dev_dbg(&spi
->dev
, "read 0x%x from reg 0x%x\n", retval
, addr
);
48 static int rf69_write_reg(struct spi_device
*spi
, u8 addr
, u8 value
)
53 buffer
[0] = addr
| WRITE_BIT
;
56 retval
= spi_write(spi
, &buffer
, 2);
61 * should never happen, since we already checked,
62 * that module is connected. Therefore no error
63 * handling, just an optional error message...
65 dev_dbg(&spi
->dev
, "write 0x%x to 0x%x FAILED\n", value
, addr
);
67 dev_dbg(&spi
->dev
, "wrote 0x%x to reg 0x%x\n", value
, addr
);
73 /*-------------------------------------------------------------------------*/
75 static int rf69_set_bit(struct spi_device
*spi
, u8 reg
, u8 mask
)
79 tmp
= rf69_read_reg(spi
, reg
);
81 return rf69_write_reg(spi
, reg
, tmp
);
84 static int rf69_clear_bit(struct spi_device
*spi
, u8 reg
, u8 mask
)
88 tmp
= rf69_read_reg(spi
, reg
);
90 return rf69_write_reg(spi
, reg
, tmp
);
93 static inline int rf69_read_mod_write(struct spi_device
*spi
, u8 reg
,
98 tmp
= rf69_read_reg(spi
, reg
);
99 tmp
= (tmp
& ~mask
) | value
;
100 return rf69_write_reg(spi
, reg
, tmp
);
103 /*-------------------------------------------------------------------------*/
105 int rf69_set_mode(struct spi_device
*spi
, enum mode mode
)
107 static const u8 mode_map
[] = {
108 [transmit
] = OPMODE_MODE_TRANSMIT
,
109 [receive
] = OPMODE_MODE_RECEIVE
,
110 [synthesizer
] = OPMODE_MODE_SYNTHESIZER
,
111 [standby
] = OPMODE_MODE_STANDBY
,
112 [mode_sleep
] = OPMODE_MODE_SLEEP
,
115 if (unlikely(mode
>= ARRAY_SIZE(mode_map
))) {
116 dev_dbg(&spi
->dev
, "set: illegal input param");
120 return rf69_read_mod_write(spi
, REG_OPMODE
, MASK_OPMODE_MODE
,
124 * we are using packet mode, so this check is not really needed
125 * but waiting for mode ready is necessary when going from sleep
126 * because the FIFO may not be immediately available from previous mode
127 * while (_mode == RF69_MODE_SLEEP && (READ_REG(REG_IRQFLAGS1) &
128 RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
132 int rf69_set_data_mode(struct spi_device
*spi
, u8 data_mode
)
134 return rf69_read_mod_write(spi
, REG_DATAMODUL
, MASK_DATAMODUL_MODE
,
138 int rf69_set_modulation(struct spi_device
*spi
, enum modulation modulation
)
140 static const u8 modulation_map
[] = {
141 [OOK
] = DATAMODUL_MODULATION_TYPE_OOK
,
142 [FSK
] = DATAMODUL_MODULATION_TYPE_FSK
,
145 if (unlikely(modulation
>= ARRAY_SIZE(modulation_map
))) {
146 dev_dbg(&spi
->dev
, "set: illegal input param");
150 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
151 MASK_DATAMODUL_MODULATION_TYPE
,
152 modulation_map
[modulation
]);
155 static enum modulation
rf69_get_modulation(struct spi_device
*spi
)
159 modulation_reg
= rf69_read_reg(spi
, REG_DATAMODUL
);
161 switch (modulation_reg
& MASK_DATAMODUL_MODULATION_TYPE
) {
162 case DATAMODUL_MODULATION_TYPE_OOK
:
164 case DATAMODUL_MODULATION_TYPE_FSK
:
171 int rf69_set_modulation_shaping(struct spi_device
*spi
,
172 enum mod_shaping mod_shaping
)
174 switch (rf69_get_modulation(spi
)) {
176 switch (mod_shaping
) {
178 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
179 MASK_DATAMODUL_MODULATION_SHAPE
,
180 DATAMODUL_MODULATION_SHAPE_NONE
);
182 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
183 MASK_DATAMODUL_MODULATION_SHAPE
,
184 DATAMODUL_MODULATION_SHAPE_1_0
);
186 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
187 MASK_DATAMODUL_MODULATION_SHAPE
,
188 DATAMODUL_MODULATION_SHAPE_0_5
);
190 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
191 MASK_DATAMODUL_MODULATION_SHAPE
,
192 DATAMODUL_MODULATION_SHAPE_0_3
);
194 dev_dbg(&spi
->dev
, "set: illegal input param");
198 switch (mod_shaping
) {
200 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
201 MASK_DATAMODUL_MODULATION_SHAPE
,
202 DATAMODUL_MODULATION_SHAPE_NONE
);
204 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
205 MASK_DATAMODUL_MODULATION_SHAPE
,
206 DATAMODUL_MODULATION_SHAPE_BR
);
208 return rf69_read_mod_write(spi
, REG_DATAMODUL
,
209 MASK_DATAMODUL_MODULATION_SHAPE
,
210 DATAMODUL_MODULATION_SHAPE_2BR
);
212 dev_dbg(&spi
->dev
, "set: illegal input param");
216 dev_dbg(&spi
->dev
, "set: modulation undefined");
221 int rf69_set_bit_rate(struct spi_device
*spi
, u16 bit_rate
)
230 bit_rate_min
= F_OSC
/ 8388608; // 8388608 = 2^23;
231 if (bit_rate
< bit_rate_min
) {
232 dev_dbg(&spi
->dev
, "setBitRate: illegal input param");
236 // calculate reg settings
237 bit_rate_reg
= (F_OSC
/ bit_rate
);
239 msb
= (bit_rate_reg
& 0xff00) >> 8;
240 lsb
= (bit_rate_reg
& 0xff);
243 retval
= rf69_write_reg(spi
, REG_BITRATE_MSB
, msb
);
246 retval
= rf69_write_reg(spi
, REG_BITRATE_LSB
, lsb
);
253 int rf69_set_deviation(struct spi_device
*spi
, u32 deviation
)
260 u64 factor
= 1000000; // to improve precision of calculation
262 // TODO: Dependency to bitrate
263 if (deviation
< 600 || deviation
> 500000) {
264 dev_dbg(&spi
->dev
, "set_deviation: illegal input param");
269 f_step
= F_OSC
* factor
;
270 do_div(f_step
, 524288); // 524288 = 2^19
272 // calculate register settings
273 f_reg
= deviation
* factor
;
274 do_div(f_reg
, f_step
);
276 msb
= (f_reg
& 0xff00) >> 8;
277 lsb
= (f_reg
& 0xff);
280 if (msb
& ~FDEVMASB_MASK
) {
281 dev_dbg(&spi
->dev
, "set_deviation: err in calc of msb");
286 retval
= rf69_write_reg(spi
, REG_FDEV_MSB
, msb
);
289 retval
= rf69_write_reg(spi
, REG_FDEV_LSB
, lsb
);
296 int rf69_set_frequency(struct spi_device
*spi
, u32 frequency
)
305 u64 factor
= 1000000; // to improve precision of calculation
308 f_step
= F_OSC
* factor
;
309 do_div(f_step
, 524288); // 524288 = 2^19
312 f_max
= div_u64(f_step
* 8388608, factor
);
313 if (frequency
> f_max
) {
314 dev_dbg(&spi
->dev
, "setFrequency: illegal input param");
318 // calculate reg settings
319 f_reg
= frequency
* factor
;
320 do_div(f_reg
, f_step
);
322 msb
= (f_reg
& 0xff0000) >> 16;
323 mid
= (f_reg
& 0xff00) >> 8;
324 lsb
= (f_reg
& 0xff);
327 retval
= rf69_write_reg(spi
, REG_FRF_MSB
, msb
);
330 retval
= rf69_write_reg(spi
, REG_FRF_MID
, mid
);
333 retval
= rf69_write_reg(spi
, REG_FRF_LSB
, lsb
);
340 int rf69_enable_amplifier(struct spi_device
*spi
, u8 amplifier_mask
)
342 return rf69_set_bit(spi
, REG_PALEVEL
, amplifier_mask
);
345 int rf69_disable_amplifier(struct spi_device
*spi
, u8 amplifier_mask
)
347 return rf69_clear_bit(spi
, REG_PALEVEL
, amplifier_mask
);
350 int rf69_set_output_power_level(struct spi_device
*spi
, u8 power_level
)
352 u8 pa_level
, ocp
, test_pa1
, test_pa2
;
353 bool pa0
, pa1
, pa2
, high_power
;
356 // check register pa_level
357 pa_level
= rf69_read_reg(spi
, REG_PALEVEL
);
358 pa0
= pa_level
& MASK_PALEVEL_PA0
;
359 pa1
= pa_level
& MASK_PALEVEL_PA1
;
360 pa2
= pa_level
& MASK_PALEVEL_PA2
;
362 // check high power mode
363 ocp
= rf69_read_reg(spi
, REG_OCP
);
364 test_pa1
= rf69_read_reg(spi
, REG_TESTPA1
);
365 test_pa2
= rf69_read_reg(spi
, REG_TESTPA2
);
366 high_power
= (ocp
== 0x0f) && (test_pa1
== 0x5d) && (test_pa2
== 0x7c);
368 if (pa0
&& !pa1
&& !pa2
) {
371 } else if (!pa0
&& pa1
&& !pa2
) {
373 min_power_level
= 16;
374 } else if (!pa0
&& pa1
&& pa2
) {
379 min_power_level
= 16;
385 if (power_level
> 0x1f)
388 if (power_level
< min_power_level
)
392 return rf69_read_mod_write(spi
, REG_PALEVEL
, MASK_PALEVEL_OUTPUT_POWER
,
395 dev_dbg(&spi
->dev
, "set: illegal input param");
399 int rf69_set_pa_ramp(struct spi_device
*spi
, enum pa_ramp pa_ramp
)
401 static const u8 pa_ramp_map
[] = {
402 [ramp3400
] = PARAMP_3400
,
403 [ramp2000
] = PARAMP_2000
,
404 [ramp1000
] = PARAMP_1000
,
405 [ramp500
] = PARAMP_500
,
406 [ramp250
] = PARAMP_250
,
407 [ramp125
] = PARAMP_125
,
408 [ramp100
] = PARAMP_100
,
409 [ramp62
] = PARAMP_62
,
410 [ramp50
] = PARAMP_50
,
411 [ramp40
] = PARAMP_40
,
412 [ramp31
] = PARAMP_31
,
413 [ramp25
] = PARAMP_25
,
414 [ramp20
] = PARAMP_20
,
415 [ramp15
] = PARAMP_15
,
416 [ramp10
] = PARAMP_10
,
419 if (unlikely(pa_ramp
>= ARRAY_SIZE(pa_ramp_map
))) {
420 dev_dbg(&spi
->dev
, "set: illegal input param");
424 return rf69_write_reg(spi
, REG_PARAMP
, pa_ramp_map
[pa_ramp
]);
427 int rf69_set_antenna_impedance(struct spi_device
*spi
,
428 enum antenna_impedance antenna_impedance
)
430 switch (antenna_impedance
) {
432 return rf69_clear_bit(spi
, REG_LNA
, MASK_LNA_ZIN
);
433 case two_hundred_ohm
:
434 return rf69_set_bit(spi
, REG_LNA
, MASK_LNA_ZIN
);
436 dev_dbg(&spi
->dev
, "set: illegal input param");
441 int rf69_set_lna_gain(struct spi_device
*spi
, enum lna_gain lna_gain
)
443 static const u8 lna_gain_map
[] = {
444 [automatic
] = LNA_GAIN_AUTO
,
445 [max
] = LNA_GAIN_MAX
,
446 [max_minus_6
] = LNA_GAIN_MAX_MINUS_6
,
447 [max_minus_12
] = LNA_GAIN_MAX_MINUS_12
,
448 [max_minus_24
] = LNA_GAIN_MAX_MINUS_24
,
449 [max_minus_36
] = LNA_GAIN_MAX_MINUS_36
,
450 [max_minus_48
] = LNA_GAIN_MAX_MINUS_48
,
453 if (unlikely(lna_gain
>= ARRAY_SIZE(lna_gain_map
))) {
454 dev_dbg(&spi
->dev
, "set: illegal input param");
458 return rf69_read_mod_write(spi
, REG_LNA
, MASK_LNA_GAIN
,
459 lna_gain_map
[lna_gain
]);
462 static int rf69_set_bandwidth_intern(struct spi_device
*spi
, u8 reg
,
463 enum mantisse mantisse
, u8 exponent
)
467 // check value for mantisse and exponent
469 dev_dbg(&spi
->dev
, "set: illegal input param");
473 if ((mantisse
!= mantisse16
) &&
474 (mantisse
!= mantisse20
) &&
475 (mantisse
!= mantisse24
)) {
476 dev_dbg(&spi
->dev
, "set: illegal input param");
481 bandwidth
= rf69_read_reg(spi
, reg
);
483 // "delete" mantisse and exponent = just keep the DCC setting
484 bandwidth
= bandwidth
& MASK_BW_DCC_FREQ
;
489 bandwidth
= bandwidth
| BW_MANT_16
;
492 bandwidth
= bandwidth
| BW_MANT_20
;
495 bandwidth
= bandwidth
| BW_MANT_24
;
500 bandwidth
= bandwidth
| exponent
;
503 return rf69_write_reg(spi
, reg
, bandwidth
);
506 int rf69_set_bandwidth(struct spi_device
*spi
, enum mantisse mantisse
,
509 return rf69_set_bandwidth_intern(spi
, REG_RXBW
, mantisse
, exponent
);
512 int rf69_set_bandwidth_during_afc(struct spi_device
*spi
,
513 enum mantisse mantisse
,
516 return rf69_set_bandwidth_intern(spi
, REG_AFCBW
, mantisse
, exponent
);
519 int rf69_set_ook_threshold_dec(struct spi_device
*spi
,
520 enum threshold_decrement threshold_decrement
)
522 static const u8 td_map
[] = {
523 [dec_every8th
] = OOKPEAK_THRESHDEC_EVERY_8TH
,
524 [dec_every4th
] = OOKPEAK_THRESHDEC_EVERY_4TH
,
525 [dec_every2nd
] = OOKPEAK_THRESHDEC_EVERY_2ND
,
526 [dec_once
] = OOKPEAK_THRESHDEC_ONCE
,
527 [dec_twice
] = OOKPEAK_THRESHDEC_TWICE
,
528 [dec_4times
] = OOKPEAK_THRESHDEC_4_TIMES
,
529 [dec_8times
] = OOKPEAK_THRESHDEC_8_TIMES
,
530 [dec_16times
] = OOKPEAK_THRESHDEC_16_TIMES
,
533 if (unlikely(threshold_decrement
>= ARRAY_SIZE(td_map
))) {
534 dev_dbg(&spi
->dev
, "set: illegal input param");
538 return rf69_read_mod_write(spi
, REG_OOKPEAK
, MASK_OOKPEAK_THRESDEC
,
539 td_map
[threshold_decrement
]);
542 int rf69_set_dio_mapping(struct spi_device
*spi
, u8 dio_number
, u8 value
)
549 switch (dio_number
) {
553 dio_addr
= REG_DIOMAPPING1
;
558 dio_addr
= REG_DIOMAPPING1
;
563 dio_addr
= REG_DIOMAPPING1
;
568 dio_addr
= REG_DIOMAPPING1
;
573 dio_addr
= REG_DIOMAPPING2
;
578 dio_addr
= REG_DIOMAPPING2
;
581 dev_dbg(&spi
->dev
, "set: illegal input param");
586 dio_value
= rf69_read_reg(spi
, dio_addr
);
588 dio_value
= dio_value
& ~mask
;
590 dio_value
= dio_value
| value
<< shift
;
592 return rf69_write_reg(spi
, dio_addr
, dio_value
);
595 bool rf69_get_flag(struct spi_device
*spi
, enum flag flag
)
598 case mode_switch_completed
:
599 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_MODE_READY
);
600 case ready_to_receive
:
601 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_RX_READY
);
603 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_TX_READY
);
605 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_PLL_LOCK
);
606 case rssi_exceeded_threshold
:
607 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_RSSI
);
609 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_TIMEOUT
);
611 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_AUTOMODE
);
612 case sync_address_match
:
613 return (rf69_read_reg(spi
, REG_IRQFLAGS1
) & MASK_IRQFLAGS1_SYNC_ADDRESS_MATCH
);
615 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_FIFO_FULL
);
617 * case fifo_not_empty:
618 * return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY);
621 return !(rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY
);
622 case fifo_level_below_threshold
:
623 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_FIFO_LEVEL
);
625 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_FIFO_OVERRUN
);
627 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_PACKET_SENT
);
629 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_PAYLOAD_READY
);
631 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_CRC_OK
);
633 return (rf69_read_reg(spi
, REG_IRQFLAGS2
) & MASK_IRQFLAGS2_LOW_BAT
);
634 default: return false;
638 int rf69_set_rssi_threshold(struct spi_device
*spi
, u8 threshold
)
640 /* no value check needed - u8 exactly matches register size */
642 return rf69_write_reg(spi
, REG_RSSITHRESH
, threshold
);
645 int rf69_set_preamble_length(struct spi_device
*spi
, u16 preamble_length
)
650 /* no value check needed - u16 exactly matches register size */
652 /* calculate reg settings */
653 msb
= (preamble_length
& 0xff00) >> 8;
654 lsb
= (preamble_length
& 0xff);
656 /* transmit to chip */
657 retval
= rf69_write_reg(spi
, REG_PREAMBLE_MSB
, msb
);
660 return rf69_write_reg(spi
, REG_PREAMBLE_LSB
, lsb
);
663 int rf69_enable_sync(struct spi_device
*spi
)
665 return rf69_set_bit(spi
, REG_SYNC_CONFIG
, MASK_SYNC_CONFIG_SYNC_ON
);
668 int rf69_disable_sync(struct spi_device
*spi
)
670 return rf69_clear_bit(spi
, REG_SYNC_CONFIG
, MASK_SYNC_CONFIG_SYNC_ON
);
673 int rf69_set_fifo_fill_condition(struct spi_device
*spi
,
674 enum fifo_fill_condition fifo_fill_condition
)
676 switch (fifo_fill_condition
) {
678 return rf69_set_bit(spi
, REG_SYNC_CONFIG
,
679 MASK_SYNC_CONFIG_FIFO_FILL_CONDITION
);
680 case after_sync_interrupt
:
681 return rf69_clear_bit(spi
, REG_SYNC_CONFIG
,
682 MASK_SYNC_CONFIG_FIFO_FILL_CONDITION
);
684 dev_dbg(&spi
->dev
, "set: illegal input param");
689 int rf69_set_sync_size(struct spi_device
*spi
, u8 sync_size
)
692 if (sync_size
> 0x07) {
693 dev_dbg(&spi
->dev
, "set: illegal input param");
698 return rf69_read_mod_write(spi
, REG_SYNC_CONFIG
,
699 MASK_SYNC_CONFIG_SYNC_SIZE
,
703 int rf69_set_sync_values(struct spi_device
*spi
, u8 sync_values
[8])
707 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE1
, sync_values
[0]);
708 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE2
, sync_values
[1]);
709 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE3
, sync_values
[2]);
710 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE4
, sync_values
[3]);
711 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE5
, sync_values
[4]);
712 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE6
, sync_values
[5]);
713 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE7
, sync_values
[6]);
714 retval
+= rf69_write_reg(spi
, REG_SYNCVALUE8
, sync_values
[7]);
719 int rf69_set_packet_format(struct spi_device
*spi
,
720 enum packet_format packet_format
)
722 switch (packet_format
) {
723 case packet_length_var
:
724 return rf69_set_bit(spi
, REG_PACKETCONFIG1
,
725 MASK_PACKETCONFIG1_PACKET_FORMAT_VARIABLE
);
726 case packet_length_fix
:
727 return rf69_clear_bit(spi
, REG_PACKETCONFIG1
,
728 MASK_PACKETCONFIG1_PACKET_FORMAT_VARIABLE
);
730 dev_dbg(&spi
->dev
, "set: illegal input param");
735 int rf69_enable_crc(struct spi_device
*spi
)
737 return rf69_set_bit(spi
, REG_PACKETCONFIG1
, MASK_PACKETCONFIG1_CRC_ON
);
740 int rf69_disable_crc(struct spi_device
*spi
)
742 return rf69_clear_bit(spi
, REG_PACKETCONFIG1
, MASK_PACKETCONFIG1_CRC_ON
);
745 int rf69_set_address_filtering(struct spi_device
*spi
,
746 enum address_filtering address_filtering
)
748 static const u8 af_map
[] = {
749 [filtering_off
] = PACKETCONFIG1_ADDRESSFILTERING_OFF
,
750 [node_address
] = PACKETCONFIG1_ADDRESSFILTERING_NODE
,
751 [node_or_broadcast_address
] =
752 PACKETCONFIG1_ADDRESSFILTERING_NODEBROADCAST
,
755 if (unlikely(address_filtering
>= ARRAY_SIZE(af_map
))) {
756 dev_dbg(&spi
->dev
, "set: illegal input param");
760 return rf69_read_mod_write(spi
, REG_PACKETCONFIG1
,
761 MASK_PACKETCONFIG1_ADDRESSFILTERING
,
762 af_map
[address_filtering
]);
765 int rf69_set_payload_length(struct spi_device
*spi
, u8 payload_length
)
767 return rf69_write_reg(spi
, REG_PAYLOAD_LENGTH
, payload_length
);
770 int rf69_set_node_address(struct spi_device
*spi
, u8 node_address
)
772 return rf69_write_reg(spi
, REG_NODEADRS
, node_address
);
775 int rf69_set_broadcast_address(struct spi_device
*spi
, u8 broadcast_address
)
777 return rf69_write_reg(spi
, REG_BROADCASTADRS
, broadcast_address
);
780 int rf69_set_tx_start_condition(struct spi_device
*spi
,
781 enum tx_start_condition tx_start_condition
)
783 switch (tx_start_condition
) {
785 return rf69_clear_bit(spi
, REG_FIFO_THRESH
,
786 MASK_FIFO_THRESH_TXSTART
);
788 return rf69_set_bit(spi
, REG_FIFO_THRESH
,
789 MASK_FIFO_THRESH_TXSTART
);
791 dev_dbg(&spi
->dev
, "set: illegal input param");
796 int rf69_set_fifo_threshold(struct spi_device
*spi
, u8 threshold
)
800 /* check input value */
801 if (threshold
& 0x80) {
802 dev_dbg(&spi
->dev
, "set: illegal input param");
807 retval
= rf69_read_mod_write(spi
, REG_FIFO_THRESH
,
808 MASK_FIFO_THRESH_VALUE
,
814 * access the fifo to activate new threshold
815 * retval (mis-) used as buffer here
817 return rf69_read_fifo(spi
, (u8
*)&retval
, 1);
820 int rf69_set_dagc(struct spi_device
*spi
, enum dagc dagc
)
822 static const u8 dagc_map
[] = {
823 [normal_mode
] = DAGC_NORMAL
,
824 [improve
] = DAGC_IMPROVED_LOWBETA0
,
825 [improve_for_low_modulation_index
] = DAGC_IMPROVED_LOWBETA1
,
828 if (unlikely(dagc
>= ARRAY_SIZE(dagc_map
))) {
829 dev_dbg(&spi
->dev
, "set: illegal input param");
833 return rf69_write_reg(spi
, REG_TESTDAGC
, dagc_map
[dagc
]);
836 /*-------------------------------------------------------------------------*/
838 int rf69_read_fifo(struct spi_device
*spi
, u8
*buffer
, unsigned int size
)
840 #ifdef DEBUG_FIFO_ACCESS
843 struct spi_transfer transfer
;
844 u8 local_buffer
[FIFO_SIZE
+ 1];
847 if (size
> FIFO_SIZE
) {
849 "read fifo: passed in buffer bigger then internal buffer\n");
853 /* prepare a bidirectional transfer */
854 local_buffer
[0] = REG_FIFO
;
855 memset(&transfer
, 0, sizeof(transfer
));
856 transfer
.tx_buf
= local_buffer
;
857 transfer
.rx_buf
= local_buffer
;
858 transfer
.len
= size
+ 1;
860 retval
= spi_sync_transfer(spi
, &transfer
, 1);
862 #ifdef DEBUG_FIFO_ACCESS
863 for (i
= 0; i
< size
; i
++)
864 dev_dbg(&spi
->dev
, "%d - 0x%x\n", i
, local_buffer
[i
+ 1]);
867 memcpy(buffer
, &local_buffer
[1], size
);
872 int rf69_write_fifo(struct spi_device
*spi
, u8
*buffer
, unsigned int size
)
874 #ifdef DEBUG_FIFO_ACCESS
877 u8 local_buffer
[FIFO_SIZE
+ 1];
879 if (size
> FIFO_SIZE
) {
881 "read fifo: passed in buffer bigger then internal buffer\n");
885 local_buffer
[0] = REG_FIFO
| WRITE_BIT
;
886 memcpy(&local_buffer
[1], buffer
, size
);
888 #ifdef DEBUG_FIFO_ACCESS
889 for (i
= 0; i
< size
; i
++)
890 dev_dbg(&spi
->dev
, "0x%x\n", buffer
[i
]);
893 return spi_write(spi
, local_buffer
, size
+ 1);