rf69.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * abstraction of the spi interface of HopeRf rf69 radio module
  4. *
  5. * Copyright (C) 2016 Wolf-Entwicklungen
  6. * Marcus Wolf <linux@wolf-entwicklungen.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. /* enable prosa debug info */
  19. #undef DEBUG
  20. /* enable print of values on reg access */
  21. #undef DEBUG_VALUES
  22. /* enable print of values on fifo access */
  23. #undef DEBUG_FIFO_ACCESS
  24. #include <linux/types.h>
  25. #include <linux/spi/spi.h>
  26. #include "rf69.h"
  27. #include "rf69_registers.h"
  28. #define F_OSC 32000000 /* in Hz */
  29. #define FIFO_SIZE 66 /* in byte */
  30. /*-------------------------------------------------------------------------*/
  31. static u8 rf69_read_reg(struct spi_device *spi, u8 addr)
  32. {
  33. int retval;
  34. retval = spi_w8r8(spi, addr);
  35. #ifdef DEBUG_VALUES
  36. if (retval < 0)
  37. /*
  38. * should never happen, since we already checked,
  39. * that module is connected. Therefore no error
  40. * handling, just an optional error message...
  41. */
  42. dev_dbg(&spi->dev, "read 0x%x FAILED\n", addr);
  43. else
  44. dev_dbg(&spi->dev, "read 0x%x from reg 0x%x\n", retval, addr);
  45. #endif
  46. return retval;
  47. }
  48. static int rf69_write_reg(struct spi_device *spi, u8 addr, u8 value)
  49. {
  50. int retval;
  51. char buffer[2];
  52. buffer[0] = addr | WRITE_BIT;
  53. buffer[1] = value;
  54. retval = spi_write(spi, &buffer, 2);
  55. #ifdef DEBUG_VALUES
  56. if (retval < 0)
  57. /*
  58. * should never happen, since we already checked,
  59. * that module is connected. Therefore no error
  60. * handling, just an optional error message...
  61. */
  62. dev_dbg(&spi->dev, "write 0x%x to 0x%x FAILED\n", value, addr);
  63. else
  64. dev_dbg(&spi->dev, "wrote 0x%x to reg 0x%x\n", value, addr);
  65. #endif
  66. return retval;
  67. }
  68. /*-------------------------------------------------------------------------*/
  69. static int rf69_set_bit(struct spi_device *spi, u8 reg, u8 mask)
  70. {
  71. u8 tmp;
  72. tmp = rf69_read_reg(spi, reg);
  73. tmp = tmp | mask;
  74. return rf69_write_reg(spi, reg, tmp);
  75. }
  76. static int rf69_clear_bit(struct spi_device *spi, u8 reg, u8 mask)
  77. {
  78. u8 tmp;
  79. tmp = rf69_read_reg(spi, reg);
  80. tmp = tmp & ~mask;
  81. return rf69_write_reg(spi, reg, tmp);
  82. }
  83. static inline int rf69_read_mod_write(struct spi_device *spi, u8 reg,
  84. u8 mask, u8 value)
  85. {
  86. u8 tmp;
  87. tmp = rf69_read_reg(spi, reg);
  88. tmp = (tmp & ~mask) | value;
  89. return rf69_write_reg(spi, reg, tmp);
  90. }
  91. /*-------------------------------------------------------------------------*/
  92. int rf69_set_mode(struct spi_device *spi, enum mode mode)
  93. {
  94. static const u8 mode_map[] = {
  95. [transmit] = OPMODE_MODE_TRANSMIT,
  96. [receive] = OPMODE_MODE_RECEIVE,
  97. [synthesizer] = OPMODE_MODE_SYNTHESIZER,
  98. [standby] = OPMODE_MODE_STANDBY,
  99. [mode_sleep] = OPMODE_MODE_SLEEP,
  100. };
  101. if (unlikely(mode >= ARRAY_SIZE(mode_map))) {
  102. dev_dbg(&spi->dev, "set: illegal input param");
  103. return -EINVAL;
  104. }
  105. return rf69_read_mod_write(spi, REG_OPMODE, MASK_OPMODE_MODE,
  106. mode_map[mode]);
  107. /*
  108. * we are using packet mode, so this check is not really needed
  109. * but waiting for mode ready is necessary when going from sleep
  110. * because the FIFO may not be immediately available from previous mode
  111. * while (_mode == RF69_MODE_SLEEP && (READ_REG(REG_IRQFLAGS1) &
  112. RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady
  113. */
  114. }
  115. int rf69_set_data_mode(struct spi_device *spi, u8 data_mode)
  116. {
  117. return rf69_read_mod_write(spi, REG_DATAMODUL, MASK_DATAMODUL_MODE,
  118. data_mode);
  119. }
  120. int rf69_set_modulation(struct spi_device *spi, enum modulation modulation)
  121. {
  122. static const u8 modulation_map[] = {
  123. [OOK] = DATAMODUL_MODULATION_TYPE_OOK,
  124. [FSK] = DATAMODUL_MODULATION_TYPE_FSK,
  125. };
  126. if (unlikely(modulation >= ARRAY_SIZE(modulation_map))) {
  127. dev_dbg(&spi->dev, "set: illegal input param");
  128. return -EINVAL;
  129. }
  130. return rf69_read_mod_write(spi, REG_DATAMODUL,
  131. MASK_DATAMODUL_MODULATION_TYPE,
  132. modulation_map[modulation]);
  133. }
  134. static enum modulation rf69_get_modulation(struct spi_device *spi)
  135. {
  136. u8 modulation_reg;
  137. modulation_reg = rf69_read_reg(spi, REG_DATAMODUL);
  138. switch (modulation_reg & MASK_DATAMODUL_MODULATION_TYPE) {
  139. case DATAMODUL_MODULATION_TYPE_OOK:
  140. return OOK;
  141. case DATAMODUL_MODULATION_TYPE_FSK:
  142. return FSK;
  143. default:
  144. return UNDEF;
  145. }
  146. }
  147. int rf69_set_modulation_shaping(struct spi_device *spi,
  148. enum mod_shaping mod_shaping)
  149. {
  150. switch (rf69_get_modulation(spi)) {
  151. case FSK:
  152. switch (mod_shaping) {
  153. case SHAPING_OFF:
  154. return rf69_read_mod_write(spi, REG_DATAMODUL,
  155. MASK_DATAMODUL_MODULATION_SHAPE,
  156. DATAMODUL_MODULATION_SHAPE_NONE);
  157. case SHAPING_1_0:
  158. return rf69_read_mod_write(spi, REG_DATAMODUL,
  159. MASK_DATAMODUL_MODULATION_SHAPE,
  160. DATAMODUL_MODULATION_SHAPE_1_0);
  161. case SHAPING_0_5:
  162. return rf69_read_mod_write(spi, REG_DATAMODUL,
  163. MASK_DATAMODUL_MODULATION_SHAPE,
  164. DATAMODUL_MODULATION_SHAPE_0_5);
  165. case SHAPING_0_3:
  166. return rf69_read_mod_write(spi, REG_DATAMODUL,
  167. MASK_DATAMODUL_MODULATION_SHAPE,
  168. DATAMODUL_MODULATION_SHAPE_0_3);
  169. default:
  170. dev_dbg(&spi->dev, "set: illegal input param");
  171. return -EINVAL;
  172. }
  173. case OOK:
  174. switch (mod_shaping) {
  175. case SHAPING_OFF:
  176. return rf69_read_mod_write(spi, REG_DATAMODUL,
  177. MASK_DATAMODUL_MODULATION_SHAPE,
  178. DATAMODUL_MODULATION_SHAPE_NONE);
  179. case SHAPING_BR:
  180. return rf69_read_mod_write(spi, REG_DATAMODUL,
  181. MASK_DATAMODUL_MODULATION_SHAPE,
  182. DATAMODUL_MODULATION_SHAPE_BR);
  183. case SHAPING_2BR:
  184. return rf69_read_mod_write(spi, REG_DATAMODUL,
  185. MASK_DATAMODUL_MODULATION_SHAPE,
  186. DATAMODUL_MODULATION_SHAPE_2BR);
  187. default:
  188. dev_dbg(&spi->dev, "set: illegal input param");
  189. return -EINVAL;
  190. }
  191. default:
  192. dev_dbg(&spi->dev, "set: modulation undefined");
  193. return -EINVAL;
  194. }
  195. }
  196. int rf69_set_bit_rate(struct spi_device *spi, u16 bit_rate)
  197. {
  198. int retval;
  199. u32 bit_rate_min;
  200. u32 bit_rate_reg;
  201. u8 msb;
  202. u8 lsb;
  203. // check input value
  204. bit_rate_min = F_OSC / 8388608; // 8388608 = 2^23;
  205. if (bit_rate < bit_rate_min) {
  206. dev_dbg(&spi->dev, "setBitRate: illegal input param");
  207. return -EINVAL;
  208. }
  209. // calculate reg settings
  210. bit_rate_reg = (F_OSC / bit_rate);
  211. msb = (bit_rate_reg & 0xff00) >> 8;
  212. lsb = (bit_rate_reg & 0xff);
  213. // transmit to RF 69
  214. retval = rf69_write_reg(spi, REG_BITRATE_MSB, msb);
  215. if (retval)
  216. return retval;
  217. retval = rf69_write_reg(spi, REG_BITRATE_LSB, lsb);
  218. if (retval)
  219. return retval;
  220. return 0;
  221. }
  222. int rf69_set_deviation(struct spi_device *spi, u32 deviation)
  223. {
  224. int retval;
  225. u64 f_reg;
  226. u64 f_step;
  227. u8 msb;
  228. u8 lsb;
  229. u64 factor = 1000000; // to improve precision of calculation
  230. // TODO: Dependency to bitrate
  231. if (deviation < 600 || deviation > 500000) {
  232. dev_dbg(&spi->dev, "set_deviation: illegal input param");
  233. return -EINVAL;
  234. }
  235. // calculat f step
  236. f_step = F_OSC * factor;
  237. do_div(f_step, 524288); // 524288 = 2^19
  238. // calculate register settings
  239. f_reg = deviation * factor;
  240. do_div(f_reg, f_step);
  241. msb = (f_reg & 0xff00) >> 8;
  242. lsb = (f_reg & 0xff);
  243. // check msb
  244. if (msb & ~FDEVMASB_MASK) {
  245. dev_dbg(&spi->dev, "set_deviation: err in calc of msb");
  246. return -EINVAL;
  247. }
  248. // write to chip
  249. retval = rf69_write_reg(spi, REG_FDEV_MSB, msb);
  250. if (retval)
  251. return retval;
  252. retval = rf69_write_reg(spi, REG_FDEV_LSB, lsb);
  253. if (retval)
  254. return retval;
  255. return 0;
  256. }
  257. int rf69_set_frequency(struct spi_device *spi, u32 frequency)
  258. {
  259. int retval;
  260. u32 f_max;
  261. u64 f_reg;
  262. u64 f_step;
  263. u8 msb;
  264. u8 mid;
  265. u8 lsb;
  266. u64 factor = 1000000; // to improve precision of calculation
  267. // calculat f step
  268. f_step = F_OSC * factor;
  269. do_div(f_step, 524288); // 524288 = 2^19
  270. // check input value
  271. f_max = div_u64(f_step * 8388608, factor);
  272. if (frequency > f_max) {
  273. dev_dbg(&spi->dev, "setFrequency: illegal input param");
  274. return -EINVAL;
  275. }
  276. // calculate reg settings
  277. f_reg = frequency * factor;
  278. do_div(f_reg, f_step);
  279. msb = (f_reg & 0xff0000) >> 16;
  280. mid = (f_reg & 0xff00) >> 8;
  281. lsb = (f_reg & 0xff);
  282. // write to chip
  283. retval = rf69_write_reg(spi, REG_FRF_MSB, msb);
  284. if (retval)
  285. return retval;
  286. retval = rf69_write_reg(spi, REG_FRF_MID, mid);
  287. if (retval)
  288. return retval;
  289. retval = rf69_write_reg(spi, REG_FRF_LSB, lsb);
  290. if (retval)
  291. return retval;
  292. return 0;
  293. }
  294. int rf69_enable_amplifier(struct spi_device *spi, u8 amplifier_mask)
  295. {
  296. return rf69_set_bit(spi, REG_PALEVEL, amplifier_mask);
  297. }
  298. int rf69_disable_amplifier(struct spi_device *spi, u8 amplifier_mask)
  299. {
  300. return rf69_clear_bit(spi, REG_PALEVEL, amplifier_mask);
  301. }
  302. int rf69_set_output_power_level(struct spi_device *spi, u8 power_level)
  303. {
  304. // TODO: Dependency to PA0,1,2 setting
  305. power_level += 18;
  306. // check input value
  307. if (power_level > 0x1f) {
  308. dev_dbg(&spi->dev, "set: illegal input param");
  309. return -EINVAL;
  310. }
  311. // write value
  312. return rf69_read_mod_write(spi, REG_PALEVEL, MASK_PALEVEL_OUTPUT_POWER,
  313. power_level);
  314. }
  315. int rf69_set_pa_ramp(struct spi_device *spi, enum pa_ramp pa_ramp)
  316. {
  317. static const u8 pa_ramp_map[] = {
  318. [ramp3400] = PARAMP_3400,
  319. [ramp2000] = PARAMP_2000,
  320. [ramp1000] = PARAMP_1000,
  321. [ramp500] = PARAMP_500,
  322. [ramp250] = PARAMP_250,
  323. [ramp125] = PARAMP_125,
  324. [ramp100] = PARAMP_100,
  325. [ramp62] = PARAMP_62,
  326. [ramp50] = PARAMP_50,
  327. [ramp40] = PARAMP_40,
  328. [ramp31] = PARAMP_31,
  329. [ramp25] = PARAMP_25,
  330. [ramp20] = PARAMP_20,
  331. [ramp15] = PARAMP_15,
  332. [ramp10] = PARAMP_10,
  333. };
  334. if (unlikely(pa_ramp >= ARRAY_SIZE(pa_ramp_map))) {
  335. dev_dbg(&spi->dev, "set: illegal input param");
  336. return -EINVAL;
  337. }
  338. return rf69_write_reg(spi, REG_PARAMP, pa_ramp_map[pa_ramp]);
  339. }
  340. int rf69_set_antenna_impedance(struct spi_device *spi,
  341. enum antenna_impedance antenna_impedance)
  342. {
  343. switch (antenna_impedance) {
  344. case fifty_ohm:
  345. return rf69_clear_bit(spi, REG_LNA, MASK_LNA_ZIN);
  346. case two_hundred_ohm:
  347. return rf69_set_bit(spi, REG_LNA, MASK_LNA_ZIN);
  348. default:
  349. dev_dbg(&spi->dev, "set: illegal input param");
  350. return -EINVAL;
  351. }
  352. }
  353. int rf69_set_lna_gain(struct spi_device *spi, enum lna_gain lna_gain)
  354. {
  355. static const u8 lna_gain_map[] = {
  356. [automatic] = LNA_GAIN_AUTO,
  357. [max] = LNA_GAIN_MAX,
  358. [max_minus_6] = LNA_GAIN_MAX_MINUS_6,
  359. [max_minus_12] = LNA_GAIN_MAX_MINUS_12,
  360. [max_minus_24] = LNA_GAIN_MAX_MINUS_24,
  361. [max_minus_36] = LNA_GAIN_MAX_MINUS_36,
  362. [max_minus_48] = LNA_GAIN_MAX_MINUS_48,
  363. };
  364. if (unlikely(lna_gain >= ARRAY_SIZE(lna_gain_map))) {
  365. dev_dbg(&spi->dev, "set: illegal input param");
  366. return -EINVAL;
  367. }
  368. return rf69_read_mod_write(spi, REG_LNA, MASK_LNA_GAIN,
  369. lna_gain_map[lna_gain]);
  370. }
  371. static int rf69_set_bandwidth_intern(struct spi_device *spi, u8 reg,
  372. enum mantisse mantisse, u8 exponent)
  373. {
  374. u8 bandwidth;
  375. // check value for mantisse and exponent
  376. if (exponent > 7) {
  377. dev_dbg(&spi->dev, "set: illegal input param");
  378. return -EINVAL;
  379. }
  380. if ((mantisse != mantisse16) &&
  381. (mantisse != mantisse20) &&
  382. (mantisse != mantisse24)) {
  383. dev_dbg(&spi->dev, "set: illegal input param");
  384. return -EINVAL;
  385. }
  386. // read old value
  387. bandwidth = rf69_read_reg(spi, reg);
  388. // "delete" mantisse and exponent = just keep the DCC setting
  389. bandwidth = bandwidth & MASK_BW_DCC_FREQ;
  390. // add new mantisse
  391. switch (mantisse) {
  392. case mantisse16:
  393. bandwidth = bandwidth | BW_MANT_16;
  394. break;
  395. case mantisse20:
  396. bandwidth = bandwidth | BW_MANT_20;
  397. break;
  398. case mantisse24:
  399. bandwidth = bandwidth | BW_MANT_24;
  400. break;
  401. }
  402. // add new exponent
  403. bandwidth = bandwidth | exponent;
  404. // write back
  405. return rf69_write_reg(spi, reg, bandwidth);
  406. }
  407. int rf69_set_bandwidth(struct spi_device *spi, enum mantisse mantisse,
  408. u8 exponent)
  409. {
  410. return rf69_set_bandwidth_intern(spi, REG_RXBW, mantisse, exponent);
  411. }
  412. int rf69_set_bandwidth_during_afc(struct spi_device *spi,
  413. enum mantisse mantisse,
  414. u8 exponent)
  415. {
  416. return rf69_set_bandwidth_intern(spi, REG_AFCBW, mantisse, exponent);
  417. }
  418. int rf69_set_ook_threshold_dec(struct spi_device *spi,
  419. enum threshold_decrement threshold_decrement)
  420. {
  421. static const u8 td_map[] = {
  422. [dec_every8th] = OOKPEAK_THRESHDEC_EVERY_8TH,
  423. [dec_every4th] = OOKPEAK_THRESHDEC_EVERY_4TH,
  424. [dec_every2nd] = OOKPEAK_THRESHDEC_EVERY_2ND,
  425. [dec_once] = OOKPEAK_THRESHDEC_ONCE,
  426. [dec_twice] = OOKPEAK_THRESHDEC_TWICE,
  427. [dec_4times] = OOKPEAK_THRESHDEC_4_TIMES,
  428. [dec_8times] = OOKPEAK_THRESHDEC_8_TIMES,
  429. [dec_16times] = OOKPEAK_THRESHDEC_16_TIMES,
  430. };
  431. if (unlikely(threshold_decrement >= ARRAY_SIZE(td_map))) {
  432. dev_dbg(&spi->dev, "set: illegal input param");
  433. return -EINVAL;
  434. }
  435. return rf69_read_mod_write(spi, REG_OOKPEAK, MASK_OOKPEAK_THRESDEC,
  436. td_map[threshold_decrement]);
  437. }
  438. int rf69_set_dio_mapping(struct spi_device *spi, u8 dio_number, u8 value)
  439. {
  440. u8 mask;
  441. u8 shift;
  442. u8 dio_addr;
  443. u8 dio_value;
  444. switch (dio_number) {
  445. case 0:
  446. mask = MASK_DIO0;
  447. shift = SHIFT_DIO0;
  448. dio_addr = REG_DIOMAPPING1;
  449. break;
  450. case 1:
  451. mask = MASK_DIO1;
  452. shift = SHIFT_DIO1;
  453. dio_addr = REG_DIOMAPPING1;
  454. break;
  455. case 2:
  456. mask = MASK_DIO2;
  457. shift = SHIFT_DIO2;
  458. dio_addr = REG_DIOMAPPING1;
  459. break;
  460. case 3:
  461. mask = MASK_DIO3;
  462. shift = SHIFT_DIO3;
  463. dio_addr = REG_DIOMAPPING1;
  464. break;
  465. case 4:
  466. mask = MASK_DIO4;
  467. shift = SHIFT_DIO4;
  468. dio_addr = REG_DIOMAPPING2;
  469. break;
  470. case 5:
  471. mask = MASK_DIO5;
  472. shift = SHIFT_DIO5;
  473. dio_addr = REG_DIOMAPPING2;
  474. break;
  475. default:
  476. dev_dbg(&spi->dev, "set: illegal input param");
  477. return -EINVAL;
  478. }
  479. // read reg
  480. dio_value = rf69_read_reg(spi, dio_addr);
  481. // delete old value
  482. dio_value = dio_value & ~mask;
  483. // add new value
  484. dio_value = dio_value | value << shift;
  485. // write back
  486. return rf69_write_reg(spi, dio_addr, dio_value);
  487. }
  488. bool rf69_get_flag(struct spi_device *spi, enum flag flag)
  489. {
  490. switch (flag) {
  491. case mode_switch_completed:
  492. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_MODE_READY);
  493. case ready_to_receive:
  494. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RX_READY);
  495. case ready_to_send:
  496. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_TX_READY);
  497. case pll_locked:
  498. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_PLL_LOCK);
  499. case rssi_exceeded_threshold:
  500. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_RSSI);
  501. case timeout:
  502. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_TIMEOUT);
  503. case automode:
  504. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_AUTOMODE);
  505. case sync_address_match:
  506. return (rf69_read_reg(spi, REG_IRQFLAGS1) & MASK_IRQFLAGS1_SYNC_ADDRESS_MATCH);
  507. case fifo_full:
  508. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_FULL);
  509. /*
  510. * case fifo_not_empty:
  511. * return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY);
  512. */
  513. case fifo_empty:
  514. return !(rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_NOT_EMPTY);
  515. case fifo_level_below_threshold:
  516. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_LEVEL);
  517. case fifo_overrun:
  518. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_FIFO_OVERRUN);
  519. case packet_sent:
  520. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PACKET_SENT);
  521. case payload_ready:
  522. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_PAYLOAD_READY);
  523. case crc_ok:
  524. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_CRC_OK);
  525. case battery_low:
  526. return (rf69_read_reg(spi, REG_IRQFLAGS2) & MASK_IRQFLAGS2_LOW_BAT);
  527. default: return false;
  528. }
  529. }
  530. int rf69_set_rssi_threshold(struct spi_device *spi, u8 threshold)
  531. {
  532. /* no value check needed - u8 exactly matches register size */
  533. return rf69_write_reg(spi, REG_RSSITHRESH, threshold);
  534. }
  535. int rf69_set_preamble_length(struct spi_device *spi, u16 preamble_length)
  536. {
  537. int retval;
  538. u8 msb, lsb;
  539. /* no value check needed - u16 exactly matches register size */
  540. /* calculate reg settings */
  541. msb = (preamble_length & 0xff00) >> 8;
  542. lsb = (preamble_length & 0xff);
  543. /* transmit to chip */
  544. retval = rf69_write_reg(spi, REG_PREAMBLE_MSB, msb);
  545. if (retval)
  546. return retval;
  547. retval = rf69_write_reg(spi, REG_PREAMBLE_LSB, lsb);
  548. return retval;
  549. }
  550. int rf69_enable_sync(struct spi_device *spi)
  551. {
  552. return rf69_set_bit(spi, REG_SYNC_CONFIG, MASK_SYNC_CONFIG_SYNC_ON);
  553. }
  554. int rf69_disable_sync(struct spi_device *spi)
  555. {
  556. return rf69_clear_bit(spi, REG_SYNC_CONFIG, MASK_SYNC_CONFIG_SYNC_ON);
  557. }
  558. int rf69_set_fifo_fill_condition(struct spi_device *spi,
  559. enum fifo_fill_condition fifo_fill_condition)
  560. {
  561. switch (fifo_fill_condition) {
  562. case always:
  563. return rf69_set_bit(spi, REG_SYNC_CONFIG,
  564. MASK_SYNC_CONFIG_FIFO_FILL_CONDITION);
  565. case after_sync_interrupt:
  566. return rf69_clear_bit(spi, REG_SYNC_CONFIG,
  567. MASK_SYNC_CONFIG_FIFO_FILL_CONDITION);
  568. default:
  569. dev_dbg(&spi->dev, "set: illegal input param");
  570. return -EINVAL;
  571. }
  572. }
  573. int rf69_set_sync_size(struct spi_device *spi, u8 sync_size)
  574. {
  575. // check input value
  576. if (sync_size > 0x07) {
  577. dev_dbg(&spi->dev, "set: illegal input param");
  578. return -EINVAL;
  579. }
  580. // write value
  581. return rf69_read_mod_write(spi, REG_SYNC_CONFIG,
  582. MASK_SYNC_CONFIG_SYNC_SIZE,
  583. (sync_size << 3));
  584. }
  585. int rf69_set_sync_values(struct spi_device *spi, u8 sync_values[8])
  586. {
  587. int retval = 0;
  588. retval += rf69_write_reg(spi, REG_SYNCVALUE1, sync_values[0]);
  589. retval += rf69_write_reg(spi, REG_SYNCVALUE2, sync_values[1]);
  590. retval += rf69_write_reg(spi, REG_SYNCVALUE3, sync_values[2]);
  591. retval += rf69_write_reg(spi, REG_SYNCVALUE4, sync_values[3]);
  592. retval += rf69_write_reg(spi, REG_SYNCVALUE5, sync_values[4]);
  593. retval += rf69_write_reg(spi, REG_SYNCVALUE6, sync_values[5]);
  594. retval += rf69_write_reg(spi, REG_SYNCVALUE7, sync_values[6]);
  595. retval += rf69_write_reg(spi, REG_SYNCVALUE8, sync_values[7]);
  596. return retval;
  597. }
  598. int rf69_set_packet_format(struct spi_device *spi,
  599. enum packet_format packet_format)
  600. {
  601. switch (packet_format) {
  602. case packet_length_var:
  603. return rf69_set_bit(spi, REG_PACKETCONFIG1,
  604. MASK_PACKETCONFIG1_PAKET_FORMAT_VARIABLE);
  605. case packet_length_fix:
  606. return rf69_clear_bit(spi, REG_PACKETCONFIG1,
  607. MASK_PACKETCONFIG1_PAKET_FORMAT_VARIABLE);
  608. default:
  609. dev_dbg(&spi->dev, "set: illegal input param");
  610. return -EINVAL;
  611. }
  612. }
  613. int rf69_enable_crc(struct spi_device *spi)
  614. {
  615. return rf69_set_bit(spi, REG_PACKETCONFIG1, MASK_PACKETCONFIG1_CRC_ON);
  616. }
  617. int rf69_disable_crc(struct spi_device *spi)
  618. {
  619. return rf69_clear_bit(spi, REG_PACKETCONFIG1, MASK_PACKETCONFIG1_CRC_ON);
  620. }
  621. int rf69_set_address_filtering(struct spi_device *spi,
  622. enum address_filtering address_filtering)
  623. {
  624. static const u8 af_map[] = {
  625. [filtering_off] = PACKETCONFIG1_ADDRESSFILTERING_OFF,
  626. [node_address] = PACKETCONFIG1_ADDRESSFILTERING_NODE,
  627. [node_or_broadcast_address] =
  628. PACKETCONFIG1_ADDRESSFILTERING_NODEBROADCAST,
  629. };
  630. if (unlikely(address_filtering >= ARRAY_SIZE(af_map))) {
  631. dev_dbg(&spi->dev, "set: illegal input param");
  632. return -EINVAL;
  633. }
  634. return rf69_read_mod_write(spi, REG_PACKETCONFIG1,
  635. MASK_PACKETCONFIG1_ADDRESSFILTERING,
  636. af_map[address_filtering]);
  637. }
  638. int rf69_set_payload_length(struct spi_device *spi, u8 payload_length)
  639. {
  640. return rf69_write_reg(spi, REG_PAYLOAD_LENGTH, payload_length);
  641. }
  642. int rf69_set_node_address(struct spi_device *spi, u8 node_address)
  643. {
  644. return rf69_write_reg(spi, REG_NODEADRS, node_address);
  645. }
  646. int rf69_set_broadcast_address(struct spi_device *spi, u8 broadcast_address)
  647. {
  648. return rf69_write_reg(spi, REG_BROADCASTADRS, broadcast_address);
  649. }
  650. int rf69_set_tx_start_condition(struct spi_device *spi,
  651. enum tx_start_condition tx_start_condition)
  652. {
  653. switch (tx_start_condition) {
  654. case fifo_level:
  655. return rf69_clear_bit(spi, REG_FIFO_THRESH,
  656. MASK_FIFO_THRESH_TXSTART);
  657. case fifo_not_empty:
  658. return rf69_set_bit(spi, REG_FIFO_THRESH,
  659. MASK_FIFO_THRESH_TXSTART);
  660. default:
  661. dev_dbg(&spi->dev, "set: illegal input param");
  662. return -EINVAL;
  663. }
  664. }
  665. int rf69_set_fifo_threshold(struct spi_device *spi, u8 threshold)
  666. {
  667. int retval;
  668. /* check input value */
  669. if (threshold & 0x80) {
  670. dev_dbg(&spi->dev, "set: illegal input param");
  671. return -EINVAL;
  672. }
  673. /* write value */
  674. retval = rf69_read_mod_write(spi, REG_FIFO_THRESH,
  675. MASK_FIFO_THRESH_VALUE,
  676. threshold);
  677. if (retval)
  678. return retval;
  679. /*
  680. * access the fifo to activate new threshold
  681. * retval (mis-) used as buffer here
  682. */
  683. return rf69_read_fifo(spi, (u8 *)&retval, 1);
  684. }
  685. int rf69_set_dagc(struct spi_device *spi, enum dagc dagc)
  686. {
  687. static const u8 dagc_map[] = {
  688. [normal_mode] = DAGC_NORMAL,
  689. [improve] = DAGC_IMPROVED_LOWBETA0,
  690. [improve_for_low_modulation_index] = DAGC_IMPROVED_LOWBETA1,
  691. };
  692. if (unlikely(dagc >= ARRAY_SIZE(dagc_map))) {
  693. dev_dbg(&spi->dev, "set: illegal input param");
  694. return -EINVAL;
  695. }
  696. return rf69_write_reg(spi, REG_TESTDAGC, dagc_map[dagc]);
  697. }
  698. /*-------------------------------------------------------------------------*/
  699. int rf69_read_fifo(struct spi_device *spi, u8 *buffer, unsigned int size)
  700. {
  701. #ifdef DEBUG_FIFO_ACCESS
  702. int i;
  703. #endif
  704. struct spi_transfer transfer;
  705. u8 local_buffer[FIFO_SIZE + 1];
  706. int retval;
  707. if (size > FIFO_SIZE) {
  708. dev_dbg(&spi->dev,
  709. "read fifo: passed in buffer bigger then internal buffer\n");
  710. return -EMSGSIZE;
  711. }
  712. /* prepare a bidirectional transfer */
  713. local_buffer[0] = REG_FIFO;
  714. memset(&transfer, 0, sizeof(transfer));
  715. transfer.tx_buf = local_buffer;
  716. transfer.rx_buf = local_buffer;
  717. transfer.len = size + 1;
  718. retval = spi_sync_transfer(spi, &transfer, 1);
  719. #ifdef DEBUG_FIFO_ACCESS
  720. for (i = 0; i < size; i++)
  721. dev_dbg(&spi->dev, "%d - 0x%x\n", i, local_buffer[i + 1]);
  722. #endif
  723. memcpy(buffer, &local_buffer[1], size);
  724. return retval;
  725. }
  726. int rf69_write_fifo(struct spi_device *spi, u8 *buffer, unsigned int size)
  727. {
  728. #ifdef DEBUG_FIFO_ACCESS
  729. int i;
  730. #endif
  731. char spi_address = REG_FIFO | WRITE_BIT;
  732. u8 local_buffer[FIFO_SIZE + 1];
  733. if (size > FIFO_SIZE) {
  734. dev_dbg(&spi->dev,
  735. "read fifo: passed in buffer bigger then internal buffer\n");
  736. return -EMSGSIZE;
  737. }
  738. local_buffer[0] = spi_address;
  739. memcpy(&local_buffer[1], buffer, size);
  740. #ifdef DEBUG_FIFO_ACCESS
  741. for (i = 0; i < size; i++)
  742. dev_dbg(&spi->dev, "0x%x\n", buffer[i]);
  743. #endif
  744. return spi_write(spi, local_buffer, size + 1);
  745. }