at86rf230.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. /*
  2. * AT86RF230/RF231 driver
  3. *
  4. * Copyright (C) 2009-2012 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Written by:
  16. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. * Alexander Aring <aar@pengutronix.de>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/gpio.h>
  27. #include <linux/delay.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/spi/at86rf230.h>
  30. #include <linux/regmap.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/ieee802154.h>
  34. #include <net/mac802154.h>
  35. #include <net/cfg802154.h>
  36. struct at86rf230_local;
  37. /* at86rf2xx chip depend data.
  38. * All timings are in us.
  39. */
  40. struct at86rf2xx_chip_data {
  41. u16 t_sleep_cycle;
  42. u16 t_channel_switch;
  43. u16 t_reset_to_off;
  44. u16 t_off_to_aack;
  45. u16 t_off_to_tx_on;
  46. u16 t_frame;
  47. u16 t_p_ack;
  48. int rssi_base_val;
  49. int (*set_channel)(struct at86rf230_local *, u8, u8);
  50. int (*get_desense_steps)(struct at86rf230_local *, s32);
  51. };
  52. #define AT86RF2XX_MAX_BUF (127 + 3)
  53. /* tx retries to access the TX_ON state
  54. * if it's above then force change will be started.
  55. *
  56. * We assume the max_frame_retries (7) value of 802.15.4 here.
  57. */
  58. #define AT86RF2XX_MAX_TX_RETRIES 7
  59. /* We use the recommended 5 minutes timeout to recalibrate */
  60. #define AT86RF2XX_CAL_LOOP_TIMEOUT (5 * 60 * HZ)
  61. struct at86rf230_state_change {
  62. struct at86rf230_local *lp;
  63. int irq;
  64. struct hrtimer timer;
  65. struct spi_message msg;
  66. struct spi_transfer trx;
  67. u8 buf[AT86RF2XX_MAX_BUF];
  68. void (*complete)(void *context);
  69. u8 from_state;
  70. u8 to_state;
  71. bool irq_enable;
  72. };
  73. struct at86rf230_local {
  74. struct spi_device *spi;
  75. struct ieee802154_hw *hw;
  76. struct at86rf2xx_chip_data *data;
  77. struct regmap *regmap;
  78. int slp_tr;
  79. struct completion state_complete;
  80. struct at86rf230_state_change state;
  81. struct at86rf230_state_change irq;
  82. bool tx_aret;
  83. unsigned long cal_timeout;
  84. s8 max_frame_retries;
  85. bool is_tx;
  86. bool is_tx_from_off;
  87. u8 tx_retry;
  88. struct sk_buff *tx_skb;
  89. struct at86rf230_state_change tx;
  90. };
  91. #define RG_TRX_STATUS (0x01)
  92. #define SR_TRX_STATUS 0x01, 0x1f, 0
  93. #define SR_RESERVED_01_3 0x01, 0x20, 5
  94. #define SR_CCA_STATUS 0x01, 0x40, 6
  95. #define SR_CCA_DONE 0x01, 0x80, 7
  96. #define RG_TRX_STATE (0x02)
  97. #define SR_TRX_CMD 0x02, 0x1f, 0
  98. #define SR_TRAC_STATUS 0x02, 0xe0, 5
  99. #define RG_TRX_CTRL_0 (0x03)
  100. #define SR_CLKM_CTRL 0x03, 0x07, 0
  101. #define SR_CLKM_SHA_SEL 0x03, 0x08, 3
  102. #define SR_PAD_IO_CLKM 0x03, 0x30, 4
  103. #define SR_PAD_IO 0x03, 0xc0, 6
  104. #define RG_TRX_CTRL_1 (0x04)
  105. #define SR_IRQ_POLARITY 0x04, 0x01, 0
  106. #define SR_IRQ_MASK_MODE 0x04, 0x02, 1
  107. #define SR_SPI_CMD_MODE 0x04, 0x0c, 2
  108. #define SR_RX_BL_CTRL 0x04, 0x10, 4
  109. #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
  110. #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
  111. #define SR_PA_EXT_EN 0x04, 0x80, 7
  112. #define RG_PHY_TX_PWR (0x05)
  113. #define SR_TX_PWR 0x05, 0x0f, 0
  114. #define SR_PA_LT 0x05, 0x30, 4
  115. #define SR_PA_BUF_LT 0x05, 0xc0, 6
  116. #define RG_PHY_RSSI (0x06)
  117. #define SR_RSSI 0x06, 0x1f, 0
  118. #define SR_RND_VALUE 0x06, 0x60, 5
  119. #define SR_RX_CRC_VALID 0x06, 0x80, 7
  120. #define RG_PHY_ED_LEVEL (0x07)
  121. #define SR_ED_LEVEL 0x07, 0xff, 0
  122. #define RG_PHY_CC_CCA (0x08)
  123. #define SR_CHANNEL 0x08, 0x1f, 0
  124. #define SR_CCA_MODE 0x08, 0x60, 5
  125. #define SR_CCA_REQUEST 0x08, 0x80, 7
  126. #define RG_CCA_THRES (0x09)
  127. #define SR_CCA_ED_THRES 0x09, 0x0f, 0
  128. #define SR_RESERVED_09_1 0x09, 0xf0, 4
  129. #define RG_RX_CTRL (0x0a)
  130. #define SR_PDT_THRES 0x0a, 0x0f, 0
  131. #define SR_RESERVED_0a_1 0x0a, 0xf0, 4
  132. #define RG_SFD_VALUE (0x0b)
  133. #define SR_SFD_VALUE 0x0b, 0xff, 0
  134. #define RG_TRX_CTRL_2 (0x0c)
  135. #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
  136. #define SR_SUB_MODE 0x0c, 0x04, 2
  137. #define SR_BPSK_QPSK 0x0c, 0x08, 3
  138. #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
  139. #define SR_RESERVED_0c_5 0x0c, 0x60, 5
  140. #define SR_RX_SAFE_MODE 0x0c, 0x80, 7
  141. #define RG_ANT_DIV (0x0d)
  142. #define SR_ANT_CTRL 0x0d, 0x03, 0
  143. #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
  144. #define SR_ANT_DIV_EN 0x0d, 0x08, 3
  145. #define SR_RESERVED_0d_2 0x0d, 0x70, 4
  146. #define SR_ANT_SEL 0x0d, 0x80, 7
  147. #define RG_IRQ_MASK (0x0e)
  148. #define SR_IRQ_MASK 0x0e, 0xff, 0
  149. #define RG_IRQ_STATUS (0x0f)
  150. #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
  151. #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
  152. #define SR_IRQ_2_RX_START 0x0f, 0x04, 2
  153. #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
  154. #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
  155. #define SR_IRQ_5_AMI 0x0f, 0x20, 5
  156. #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
  157. #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
  158. #define RG_VREG_CTRL (0x10)
  159. #define SR_RESERVED_10_6 0x10, 0x03, 0
  160. #define SR_DVDD_OK 0x10, 0x04, 2
  161. #define SR_DVREG_EXT 0x10, 0x08, 3
  162. #define SR_RESERVED_10_3 0x10, 0x30, 4
  163. #define SR_AVDD_OK 0x10, 0x40, 6
  164. #define SR_AVREG_EXT 0x10, 0x80, 7
  165. #define RG_BATMON (0x11)
  166. #define SR_BATMON_VTH 0x11, 0x0f, 0
  167. #define SR_BATMON_HR 0x11, 0x10, 4
  168. #define SR_BATMON_OK 0x11, 0x20, 5
  169. #define SR_RESERVED_11_1 0x11, 0xc0, 6
  170. #define RG_XOSC_CTRL (0x12)
  171. #define SR_XTAL_TRIM 0x12, 0x0f, 0
  172. #define SR_XTAL_MODE 0x12, 0xf0, 4
  173. #define RG_RX_SYN (0x15)
  174. #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
  175. #define SR_RESERVED_15_2 0x15, 0x70, 4
  176. #define SR_RX_PDT_DIS 0x15, 0x80, 7
  177. #define RG_XAH_CTRL_1 (0x17)
  178. #define SR_RESERVED_17_8 0x17, 0x01, 0
  179. #define SR_AACK_PROM_MODE 0x17, 0x02, 1
  180. #define SR_AACK_ACK_TIME 0x17, 0x04, 2
  181. #define SR_RESERVED_17_5 0x17, 0x08, 3
  182. #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
  183. #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
  184. #define SR_CSMA_LBT_MODE 0x17, 0x40, 6
  185. #define SR_RESERVED_17_1 0x17, 0x80, 7
  186. #define RG_FTN_CTRL (0x18)
  187. #define SR_RESERVED_18_2 0x18, 0x7f, 0
  188. #define SR_FTN_START 0x18, 0x80, 7
  189. #define RG_PLL_CF (0x1a)
  190. #define SR_RESERVED_1a_2 0x1a, 0x7f, 0
  191. #define SR_PLL_CF_START 0x1a, 0x80, 7
  192. #define RG_PLL_DCU (0x1b)
  193. #define SR_RESERVED_1b_3 0x1b, 0x3f, 0
  194. #define SR_RESERVED_1b_2 0x1b, 0x40, 6
  195. #define SR_PLL_DCU_START 0x1b, 0x80, 7
  196. #define RG_PART_NUM (0x1c)
  197. #define SR_PART_NUM 0x1c, 0xff, 0
  198. #define RG_VERSION_NUM (0x1d)
  199. #define SR_VERSION_NUM 0x1d, 0xff, 0
  200. #define RG_MAN_ID_0 (0x1e)
  201. #define SR_MAN_ID_0 0x1e, 0xff, 0
  202. #define RG_MAN_ID_1 (0x1f)
  203. #define SR_MAN_ID_1 0x1f, 0xff, 0
  204. #define RG_SHORT_ADDR_0 (0x20)
  205. #define SR_SHORT_ADDR_0 0x20, 0xff, 0
  206. #define RG_SHORT_ADDR_1 (0x21)
  207. #define SR_SHORT_ADDR_1 0x21, 0xff, 0
  208. #define RG_PAN_ID_0 (0x22)
  209. #define SR_PAN_ID_0 0x22, 0xff, 0
  210. #define RG_PAN_ID_1 (0x23)
  211. #define SR_PAN_ID_1 0x23, 0xff, 0
  212. #define RG_IEEE_ADDR_0 (0x24)
  213. #define SR_IEEE_ADDR_0 0x24, 0xff, 0
  214. #define RG_IEEE_ADDR_1 (0x25)
  215. #define SR_IEEE_ADDR_1 0x25, 0xff, 0
  216. #define RG_IEEE_ADDR_2 (0x26)
  217. #define SR_IEEE_ADDR_2 0x26, 0xff, 0
  218. #define RG_IEEE_ADDR_3 (0x27)
  219. #define SR_IEEE_ADDR_3 0x27, 0xff, 0
  220. #define RG_IEEE_ADDR_4 (0x28)
  221. #define SR_IEEE_ADDR_4 0x28, 0xff, 0
  222. #define RG_IEEE_ADDR_5 (0x29)
  223. #define SR_IEEE_ADDR_5 0x29, 0xff, 0
  224. #define RG_IEEE_ADDR_6 (0x2a)
  225. #define SR_IEEE_ADDR_6 0x2a, 0xff, 0
  226. #define RG_IEEE_ADDR_7 (0x2b)
  227. #define SR_IEEE_ADDR_7 0x2b, 0xff, 0
  228. #define RG_XAH_CTRL_0 (0x2c)
  229. #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
  230. #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
  231. #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
  232. #define RG_CSMA_SEED_0 (0x2d)
  233. #define SR_CSMA_SEED_0 0x2d, 0xff, 0
  234. #define RG_CSMA_SEED_1 (0x2e)
  235. #define SR_CSMA_SEED_1 0x2e, 0x07, 0
  236. #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
  237. #define SR_AACK_DIS_ACK 0x2e, 0x10, 4
  238. #define SR_AACK_SET_PD 0x2e, 0x20, 5
  239. #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
  240. #define RG_CSMA_BE (0x2f)
  241. #define SR_MIN_BE 0x2f, 0x0f, 0
  242. #define SR_MAX_BE 0x2f, 0xf0, 4
  243. #define CMD_REG 0x80
  244. #define CMD_REG_MASK 0x3f
  245. #define CMD_WRITE 0x40
  246. #define CMD_FB 0x20
  247. #define IRQ_BAT_LOW (1 << 7)
  248. #define IRQ_TRX_UR (1 << 6)
  249. #define IRQ_AMI (1 << 5)
  250. #define IRQ_CCA_ED (1 << 4)
  251. #define IRQ_TRX_END (1 << 3)
  252. #define IRQ_RX_START (1 << 2)
  253. #define IRQ_PLL_UNL (1 << 1)
  254. #define IRQ_PLL_LOCK (1 << 0)
  255. #define IRQ_ACTIVE_HIGH 0
  256. #define IRQ_ACTIVE_LOW 1
  257. #define STATE_P_ON 0x00 /* BUSY */
  258. #define STATE_BUSY_RX 0x01
  259. #define STATE_BUSY_TX 0x02
  260. #define STATE_FORCE_TRX_OFF 0x03
  261. #define STATE_FORCE_TX_ON 0x04 /* IDLE */
  262. /* 0x05 */ /* INVALID_PARAMETER */
  263. #define STATE_RX_ON 0x06
  264. /* 0x07 */ /* SUCCESS */
  265. #define STATE_TRX_OFF 0x08
  266. #define STATE_TX_ON 0x09
  267. /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
  268. #define STATE_SLEEP 0x0F
  269. #define STATE_PREP_DEEP_SLEEP 0x10
  270. #define STATE_BUSY_RX_AACK 0x11
  271. #define STATE_BUSY_TX_ARET 0x12
  272. #define STATE_RX_AACK_ON 0x16
  273. #define STATE_TX_ARET_ON 0x19
  274. #define STATE_RX_ON_NOCLK 0x1C
  275. #define STATE_RX_AACK_ON_NOCLK 0x1D
  276. #define STATE_BUSY_RX_AACK_NOCLK 0x1E
  277. #define STATE_TRANSITION_IN_PROGRESS 0x1F
  278. #define TRX_STATE_MASK (0x1F)
  279. #define AT86RF2XX_NUMREGS 0x3F
  280. static void
  281. at86rf230_async_state_change(struct at86rf230_local *lp,
  282. struct at86rf230_state_change *ctx,
  283. const u8 state, void (*complete)(void *context),
  284. const bool irq_enable);
  285. static inline int
  286. __at86rf230_write(struct at86rf230_local *lp,
  287. unsigned int addr, unsigned int data)
  288. {
  289. return regmap_write(lp->regmap, addr, data);
  290. }
  291. static inline int
  292. __at86rf230_read(struct at86rf230_local *lp,
  293. unsigned int addr, unsigned int *data)
  294. {
  295. return regmap_read(lp->regmap, addr, data);
  296. }
  297. static inline int
  298. at86rf230_read_subreg(struct at86rf230_local *lp,
  299. unsigned int addr, unsigned int mask,
  300. unsigned int shift, unsigned int *data)
  301. {
  302. int rc;
  303. rc = __at86rf230_read(lp, addr, data);
  304. if (!rc)
  305. *data = (*data & mask) >> shift;
  306. return rc;
  307. }
  308. static inline int
  309. at86rf230_write_subreg(struct at86rf230_local *lp,
  310. unsigned int addr, unsigned int mask,
  311. unsigned int shift, unsigned int data)
  312. {
  313. return regmap_update_bits(lp->regmap, addr, mask, data << shift);
  314. }
  315. static inline void
  316. at86rf230_slp_tr_rising_edge(struct at86rf230_local *lp)
  317. {
  318. gpio_set_value(lp->slp_tr, 1);
  319. udelay(1);
  320. gpio_set_value(lp->slp_tr, 0);
  321. }
  322. static bool
  323. at86rf230_reg_writeable(struct device *dev, unsigned int reg)
  324. {
  325. switch (reg) {
  326. case RG_TRX_STATE:
  327. case RG_TRX_CTRL_0:
  328. case RG_TRX_CTRL_1:
  329. case RG_PHY_TX_PWR:
  330. case RG_PHY_ED_LEVEL:
  331. case RG_PHY_CC_CCA:
  332. case RG_CCA_THRES:
  333. case RG_RX_CTRL:
  334. case RG_SFD_VALUE:
  335. case RG_TRX_CTRL_2:
  336. case RG_ANT_DIV:
  337. case RG_IRQ_MASK:
  338. case RG_VREG_CTRL:
  339. case RG_BATMON:
  340. case RG_XOSC_CTRL:
  341. case RG_RX_SYN:
  342. case RG_XAH_CTRL_1:
  343. case RG_FTN_CTRL:
  344. case RG_PLL_CF:
  345. case RG_PLL_DCU:
  346. case RG_SHORT_ADDR_0:
  347. case RG_SHORT_ADDR_1:
  348. case RG_PAN_ID_0:
  349. case RG_PAN_ID_1:
  350. case RG_IEEE_ADDR_0:
  351. case RG_IEEE_ADDR_1:
  352. case RG_IEEE_ADDR_2:
  353. case RG_IEEE_ADDR_3:
  354. case RG_IEEE_ADDR_4:
  355. case RG_IEEE_ADDR_5:
  356. case RG_IEEE_ADDR_6:
  357. case RG_IEEE_ADDR_7:
  358. case RG_XAH_CTRL_0:
  359. case RG_CSMA_SEED_0:
  360. case RG_CSMA_SEED_1:
  361. case RG_CSMA_BE:
  362. return true;
  363. default:
  364. return false;
  365. }
  366. }
  367. static bool
  368. at86rf230_reg_readable(struct device *dev, unsigned int reg)
  369. {
  370. bool rc;
  371. /* all writeable are also readable */
  372. rc = at86rf230_reg_writeable(dev, reg);
  373. if (rc)
  374. return rc;
  375. /* readonly regs */
  376. switch (reg) {
  377. case RG_TRX_STATUS:
  378. case RG_PHY_RSSI:
  379. case RG_IRQ_STATUS:
  380. case RG_PART_NUM:
  381. case RG_VERSION_NUM:
  382. case RG_MAN_ID_1:
  383. case RG_MAN_ID_0:
  384. return true;
  385. default:
  386. return false;
  387. }
  388. }
  389. static bool
  390. at86rf230_reg_volatile(struct device *dev, unsigned int reg)
  391. {
  392. /* can be changed during runtime */
  393. switch (reg) {
  394. case RG_TRX_STATUS:
  395. case RG_TRX_STATE:
  396. case RG_PHY_RSSI:
  397. case RG_PHY_ED_LEVEL:
  398. case RG_IRQ_STATUS:
  399. case RG_VREG_CTRL:
  400. case RG_PLL_CF:
  401. case RG_PLL_DCU:
  402. return true;
  403. default:
  404. return false;
  405. }
  406. }
  407. static bool
  408. at86rf230_reg_precious(struct device *dev, unsigned int reg)
  409. {
  410. /* don't clear irq line on read */
  411. switch (reg) {
  412. case RG_IRQ_STATUS:
  413. return true;
  414. default:
  415. return false;
  416. }
  417. }
  418. static const struct regmap_config at86rf230_regmap_spi_config = {
  419. .reg_bits = 8,
  420. .val_bits = 8,
  421. .write_flag_mask = CMD_REG | CMD_WRITE,
  422. .read_flag_mask = CMD_REG,
  423. .cache_type = REGCACHE_RBTREE,
  424. .max_register = AT86RF2XX_NUMREGS,
  425. .writeable_reg = at86rf230_reg_writeable,
  426. .readable_reg = at86rf230_reg_readable,
  427. .volatile_reg = at86rf230_reg_volatile,
  428. .precious_reg = at86rf230_reg_precious,
  429. };
  430. static void
  431. at86rf230_async_error_recover(void *context)
  432. {
  433. struct at86rf230_state_change *ctx = context;
  434. struct at86rf230_local *lp = ctx->lp;
  435. lp->is_tx = 0;
  436. at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON, NULL, false);
  437. ieee802154_wake_queue(lp->hw);
  438. }
  439. static inline void
  440. at86rf230_async_error(struct at86rf230_local *lp,
  441. struct at86rf230_state_change *ctx, int rc)
  442. {
  443. dev_err(&lp->spi->dev, "spi_async error %d\n", rc);
  444. at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
  445. at86rf230_async_error_recover, false);
  446. }
  447. /* Generic function to get some register value in async mode */
  448. static void
  449. at86rf230_async_read_reg(struct at86rf230_local *lp, const u8 reg,
  450. struct at86rf230_state_change *ctx,
  451. void (*complete)(void *context),
  452. const bool irq_enable)
  453. {
  454. int rc;
  455. u8 *tx_buf = ctx->buf;
  456. tx_buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
  457. ctx->msg.complete = complete;
  458. ctx->irq_enable = irq_enable;
  459. rc = spi_async(lp->spi, &ctx->msg);
  460. if (rc) {
  461. if (irq_enable)
  462. enable_irq(ctx->irq);
  463. at86rf230_async_error(lp, ctx, rc);
  464. }
  465. }
  466. static inline u8 at86rf230_state_to_force(u8 state)
  467. {
  468. if (state == STATE_TX_ON)
  469. return STATE_FORCE_TX_ON;
  470. else
  471. return STATE_FORCE_TRX_OFF;
  472. }
  473. static void
  474. at86rf230_async_state_assert(void *context)
  475. {
  476. struct at86rf230_state_change *ctx = context;
  477. struct at86rf230_local *lp = ctx->lp;
  478. const u8 *buf = ctx->buf;
  479. const u8 trx_state = buf[1] & TRX_STATE_MASK;
  480. /* Assert state change */
  481. if (trx_state != ctx->to_state) {
  482. /* Special handling if transceiver state is in
  483. * STATE_BUSY_RX_AACK and a SHR was detected.
  484. */
  485. if (trx_state == STATE_BUSY_RX_AACK) {
  486. /* Undocumented race condition. If we send a state
  487. * change to STATE_RX_AACK_ON the transceiver could
  488. * change his state automatically to STATE_BUSY_RX_AACK
  489. * if a SHR was detected. This is not an error, but we
  490. * can't assert this.
  491. */
  492. if (ctx->to_state == STATE_RX_AACK_ON)
  493. goto done;
  494. /* If we change to STATE_TX_ON without forcing and
  495. * transceiver state is STATE_BUSY_RX_AACK, we wait
  496. * 'tFrame + tPAck' receiving time. In this time the
  497. * PDU should be received. If the transceiver is still
  498. * in STATE_BUSY_RX_AACK, we run a force state change
  499. * to STATE_TX_ON. This is a timeout handling, if the
  500. * transceiver stucks in STATE_BUSY_RX_AACK.
  501. *
  502. * Additional we do several retries to try to get into
  503. * TX_ON state without forcing. If the retries are
  504. * higher or equal than AT86RF2XX_MAX_TX_RETRIES we
  505. * will do a force change.
  506. */
  507. if (ctx->to_state == STATE_TX_ON ||
  508. ctx->to_state == STATE_TRX_OFF) {
  509. u8 state = ctx->to_state;
  510. if (lp->tx_retry >= AT86RF2XX_MAX_TX_RETRIES)
  511. state = at86rf230_state_to_force(state);
  512. lp->tx_retry++;
  513. at86rf230_async_state_change(lp, ctx, state,
  514. ctx->complete,
  515. ctx->irq_enable);
  516. return;
  517. }
  518. }
  519. dev_warn(&lp->spi->dev, "unexcept state change from 0x%02x to 0x%02x. Actual state: 0x%02x\n",
  520. ctx->from_state, ctx->to_state, trx_state);
  521. }
  522. done:
  523. if (ctx->complete)
  524. ctx->complete(context);
  525. }
  526. static enum hrtimer_restart at86rf230_async_state_timer(struct hrtimer *timer)
  527. {
  528. struct at86rf230_state_change *ctx =
  529. container_of(timer, struct at86rf230_state_change, timer);
  530. struct at86rf230_local *lp = ctx->lp;
  531. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  532. at86rf230_async_state_assert,
  533. ctx->irq_enable);
  534. return HRTIMER_NORESTART;
  535. }
  536. /* Do state change timing delay. */
  537. static void
  538. at86rf230_async_state_delay(void *context)
  539. {
  540. struct at86rf230_state_change *ctx = context;
  541. struct at86rf230_local *lp = ctx->lp;
  542. struct at86rf2xx_chip_data *c = lp->data;
  543. bool force = false;
  544. ktime_t tim;
  545. /* The force state changes are will show as normal states in the
  546. * state status subregister. We change the to_state to the
  547. * corresponding one and remember if it was a force change, this
  548. * differs if we do a state change from STATE_BUSY_RX_AACK.
  549. */
  550. switch (ctx->to_state) {
  551. case STATE_FORCE_TX_ON:
  552. ctx->to_state = STATE_TX_ON;
  553. force = true;
  554. break;
  555. case STATE_FORCE_TRX_OFF:
  556. ctx->to_state = STATE_TRX_OFF;
  557. force = true;
  558. break;
  559. default:
  560. break;
  561. }
  562. switch (ctx->from_state) {
  563. case STATE_TRX_OFF:
  564. switch (ctx->to_state) {
  565. case STATE_RX_AACK_ON:
  566. tim = ktime_set(0, c->t_off_to_aack * NSEC_PER_USEC);
  567. /* state change from TRX_OFF to RX_AACK_ON to do a
  568. * calibration, we need to reset the timeout for the
  569. * next one.
  570. */
  571. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  572. goto change;
  573. case STATE_TX_ARET_ON:
  574. case STATE_TX_ON:
  575. tim = ktime_set(0, c->t_off_to_tx_on * NSEC_PER_USEC);
  576. /* state change from TRX_OFF to TX_ON or ARET_ON to do
  577. * a calibration, we need to reset the timeout for the
  578. * next one.
  579. */
  580. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  581. goto change;
  582. default:
  583. break;
  584. }
  585. break;
  586. case STATE_BUSY_RX_AACK:
  587. switch (ctx->to_state) {
  588. case STATE_TRX_OFF:
  589. case STATE_TX_ON:
  590. /* Wait for worst case receiving time if we
  591. * didn't make a force change from BUSY_RX_AACK
  592. * to TX_ON or TRX_OFF.
  593. */
  594. if (!force) {
  595. tim = ktime_set(0, (c->t_frame + c->t_p_ack) *
  596. NSEC_PER_USEC);
  597. goto change;
  598. }
  599. break;
  600. default:
  601. break;
  602. }
  603. break;
  604. /* Default value, means RESET state */
  605. case STATE_P_ON:
  606. switch (ctx->to_state) {
  607. case STATE_TRX_OFF:
  608. tim = ktime_set(0, c->t_reset_to_off * NSEC_PER_USEC);
  609. goto change;
  610. default:
  611. break;
  612. }
  613. break;
  614. default:
  615. break;
  616. }
  617. /* Default delay is 1us in the most cases */
  618. tim = ktime_set(0, NSEC_PER_USEC);
  619. change:
  620. hrtimer_start(&ctx->timer, tim, HRTIMER_MODE_REL);
  621. }
  622. static void
  623. at86rf230_async_state_change_start(void *context)
  624. {
  625. struct at86rf230_state_change *ctx = context;
  626. struct at86rf230_local *lp = ctx->lp;
  627. u8 *buf = ctx->buf;
  628. const u8 trx_state = buf[1] & TRX_STATE_MASK;
  629. int rc;
  630. /* Check for "possible" STATE_TRANSITION_IN_PROGRESS */
  631. if (trx_state == STATE_TRANSITION_IN_PROGRESS) {
  632. udelay(1);
  633. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  634. at86rf230_async_state_change_start,
  635. ctx->irq_enable);
  636. return;
  637. }
  638. /* Check if we already are in the state which we change in */
  639. if (trx_state == ctx->to_state) {
  640. if (ctx->complete)
  641. ctx->complete(context);
  642. return;
  643. }
  644. /* Set current state to the context of state change */
  645. ctx->from_state = trx_state;
  646. /* Going into the next step for a state change which do a timing
  647. * relevant delay.
  648. */
  649. buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  650. buf[1] = ctx->to_state;
  651. ctx->msg.complete = at86rf230_async_state_delay;
  652. rc = spi_async(lp->spi, &ctx->msg);
  653. if (rc) {
  654. if (ctx->irq_enable)
  655. enable_irq(ctx->irq);
  656. at86rf230_async_error(lp, ctx, rc);
  657. }
  658. }
  659. static void
  660. at86rf230_async_state_change(struct at86rf230_local *lp,
  661. struct at86rf230_state_change *ctx,
  662. const u8 state, void (*complete)(void *context),
  663. const bool irq_enable)
  664. {
  665. /* Initialization for the state change context */
  666. ctx->to_state = state;
  667. ctx->complete = complete;
  668. ctx->irq_enable = irq_enable;
  669. at86rf230_async_read_reg(lp, RG_TRX_STATUS, ctx,
  670. at86rf230_async_state_change_start,
  671. irq_enable);
  672. }
  673. static void
  674. at86rf230_sync_state_change_complete(void *context)
  675. {
  676. struct at86rf230_state_change *ctx = context;
  677. struct at86rf230_local *lp = ctx->lp;
  678. complete(&lp->state_complete);
  679. }
  680. /* This function do a sync framework above the async state change.
  681. * Some callbacks of the IEEE 802.15.4 driver interface need to be
  682. * handled synchronously.
  683. */
  684. static int
  685. at86rf230_sync_state_change(struct at86rf230_local *lp, unsigned int state)
  686. {
  687. unsigned long rc;
  688. at86rf230_async_state_change(lp, &lp->state, state,
  689. at86rf230_sync_state_change_complete,
  690. false);
  691. rc = wait_for_completion_timeout(&lp->state_complete,
  692. msecs_to_jiffies(100));
  693. if (!rc) {
  694. at86rf230_async_error(lp, &lp->state, -ETIMEDOUT);
  695. return -ETIMEDOUT;
  696. }
  697. return 0;
  698. }
  699. static void
  700. at86rf230_tx_complete(void *context)
  701. {
  702. struct at86rf230_state_change *ctx = context;
  703. struct at86rf230_local *lp = ctx->lp;
  704. enable_irq(ctx->irq);
  705. ieee802154_xmit_complete(lp->hw, lp->tx_skb, !lp->tx_aret);
  706. }
  707. static void
  708. at86rf230_tx_on(void *context)
  709. {
  710. struct at86rf230_state_change *ctx = context;
  711. struct at86rf230_local *lp = ctx->lp;
  712. at86rf230_async_state_change(lp, ctx, STATE_RX_AACK_ON,
  713. at86rf230_tx_complete, true);
  714. }
  715. static void
  716. at86rf230_tx_trac_check(void *context)
  717. {
  718. struct at86rf230_state_change *ctx = context;
  719. struct at86rf230_local *lp = ctx->lp;
  720. const u8 *buf = ctx->buf;
  721. const u8 trac = (buf[1] & 0xe0) >> 5;
  722. /* If trac status is different than zero we need to do a state change
  723. * to STATE_FORCE_TRX_OFF then STATE_RX_AACK_ON to recover the
  724. * transceiver.
  725. */
  726. if (trac)
  727. at86rf230_async_state_change(lp, ctx, STATE_FORCE_TRX_OFF,
  728. at86rf230_tx_on, true);
  729. else
  730. at86rf230_tx_on(context);
  731. }
  732. static void
  733. at86rf230_tx_trac_status(void *context)
  734. {
  735. struct at86rf230_state_change *ctx = context;
  736. struct at86rf230_local *lp = ctx->lp;
  737. at86rf230_async_read_reg(lp, RG_TRX_STATE, ctx,
  738. at86rf230_tx_trac_check, true);
  739. }
  740. static void
  741. at86rf230_rx_read_frame_complete(void *context)
  742. {
  743. struct at86rf230_state_change *ctx = context;
  744. struct at86rf230_local *lp = ctx->lp;
  745. u8 rx_local_buf[AT86RF2XX_MAX_BUF];
  746. const u8 *buf = ctx->buf;
  747. struct sk_buff *skb;
  748. u8 len, lqi;
  749. len = buf[1];
  750. if (!ieee802154_is_valid_psdu_len(len)) {
  751. dev_vdbg(&lp->spi->dev, "corrupted frame received\n");
  752. len = IEEE802154_MTU;
  753. }
  754. lqi = buf[2 + len];
  755. memcpy(rx_local_buf, buf + 2, len);
  756. ctx->trx.len = 2;
  757. enable_irq(ctx->irq);
  758. skb = dev_alloc_skb(IEEE802154_MTU);
  759. if (!skb) {
  760. dev_vdbg(&lp->spi->dev, "failed to allocate sk_buff\n");
  761. return;
  762. }
  763. memcpy(skb_put(skb, len), rx_local_buf, len);
  764. ieee802154_rx_irqsafe(lp->hw, skb, lqi);
  765. }
  766. static void
  767. at86rf230_rx_read_frame(void *context)
  768. {
  769. struct at86rf230_state_change *ctx = context;
  770. struct at86rf230_local *lp = ctx->lp;
  771. u8 *buf = ctx->buf;
  772. int rc;
  773. buf[0] = CMD_FB;
  774. ctx->trx.len = AT86RF2XX_MAX_BUF;
  775. ctx->msg.complete = at86rf230_rx_read_frame_complete;
  776. rc = spi_async(lp->spi, &ctx->msg);
  777. if (rc) {
  778. ctx->trx.len = 2;
  779. enable_irq(ctx->irq);
  780. at86rf230_async_error(lp, ctx, rc);
  781. }
  782. }
  783. static void
  784. at86rf230_rx_trac_check(void *context)
  785. {
  786. /* Possible check on trac status here. This could be useful to make
  787. * some stats why receive is failed. Not used at the moment, but it's
  788. * maybe timing relevant. Datasheet doesn't say anything about this.
  789. * The programming guide say do it so.
  790. */
  791. at86rf230_rx_read_frame(context);
  792. }
  793. static void
  794. at86rf230_irq_trx_end(struct at86rf230_local *lp)
  795. {
  796. if (lp->is_tx) {
  797. lp->is_tx = 0;
  798. if (lp->tx_aret)
  799. at86rf230_async_state_change(lp, &lp->irq,
  800. STATE_FORCE_TX_ON,
  801. at86rf230_tx_trac_status,
  802. true);
  803. else
  804. at86rf230_async_state_change(lp, &lp->irq,
  805. STATE_RX_AACK_ON,
  806. at86rf230_tx_complete,
  807. true);
  808. } else {
  809. at86rf230_async_read_reg(lp, RG_TRX_STATE, &lp->irq,
  810. at86rf230_rx_trac_check, true);
  811. }
  812. }
  813. static void
  814. at86rf230_irq_status(void *context)
  815. {
  816. struct at86rf230_state_change *ctx = context;
  817. struct at86rf230_local *lp = ctx->lp;
  818. const u8 *buf = ctx->buf;
  819. const u8 irq = buf[1];
  820. if (irq & IRQ_TRX_END) {
  821. at86rf230_irq_trx_end(lp);
  822. } else {
  823. enable_irq(ctx->irq);
  824. dev_err(&lp->spi->dev, "not supported irq %02x received\n",
  825. irq);
  826. }
  827. }
  828. static irqreturn_t at86rf230_isr(int irq, void *data)
  829. {
  830. struct at86rf230_local *lp = data;
  831. struct at86rf230_state_change *ctx = &lp->irq;
  832. u8 *buf = ctx->buf;
  833. int rc;
  834. disable_irq_nosync(irq);
  835. buf[0] = (RG_IRQ_STATUS & CMD_REG_MASK) | CMD_REG;
  836. ctx->msg.complete = at86rf230_irq_status;
  837. rc = spi_async(lp->spi, &ctx->msg);
  838. if (rc) {
  839. enable_irq(irq);
  840. at86rf230_async_error(lp, ctx, rc);
  841. return IRQ_NONE;
  842. }
  843. return IRQ_HANDLED;
  844. }
  845. static void
  846. at86rf230_write_frame_complete(void *context)
  847. {
  848. struct at86rf230_state_change *ctx = context;
  849. struct at86rf230_local *lp = ctx->lp;
  850. u8 *buf = ctx->buf;
  851. int rc;
  852. ctx->trx.len = 2;
  853. if (gpio_is_valid(lp->slp_tr)) {
  854. at86rf230_slp_tr_rising_edge(lp);
  855. } else {
  856. buf[0] = (RG_TRX_STATE & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  857. buf[1] = STATE_BUSY_TX;
  858. ctx->msg.complete = NULL;
  859. rc = spi_async(lp->spi, &ctx->msg);
  860. if (rc)
  861. at86rf230_async_error(lp, ctx, rc);
  862. }
  863. }
  864. static void
  865. at86rf230_write_frame(void *context)
  866. {
  867. struct at86rf230_state_change *ctx = context;
  868. struct at86rf230_local *lp = ctx->lp;
  869. struct sk_buff *skb = lp->tx_skb;
  870. u8 *buf = ctx->buf;
  871. int rc;
  872. lp->is_tx = 1;
  873. buf[0] = CMD_FB | CMD_WRITE;
  874. buf[1] = skb->len + 2;
  875. memcpy(buf + 2, skb->data, skb->len);
  876. ctx->trx.len = skb->len + 2;
  877. ctx->msg.complete = at86rf230_write_frame_complete;
  878. rc = spi_async(lp->spi, &ctx->msg);
  879. if (rc) {
  880. ctx->trx.len = 2;
  881. at86rf230_async_error(lp, ctx, rc);
  882. }
  883. }
  884. static void
  885. at86rf230_xmit_tx_on(void *context)
  886. {
  887. struct at86rf230_state_change *ctx = context;
  888. struct at86rf230_local *lp = ctx->lp;
  889. at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
  890. at86rf230_write_frame, false);
  891. }
  892. static void
  893. at86rf230_xmit_start(void *context)
  894. {
  895. struct at86rf230_state_change *ctx = context;
  896. struct at86rf230_local *lp = ctx->lp;
  897. /* In ARET mode we need to go into STATE_TX_ARET_ON after we
  898. * are in STATE_TX_ON. The pfad differs here, so we change
  899. * the complete handler.
  900. */
  901. if (lp->tx_aret) {
  902. if (lp->is_tx_from_off) {
  903. lp->is_tx_from_off = false;
  904. at86rf230_async_state_change(lp, ctx, STATE_TX_ARET_ON,
  905. at86rf230_xmit_tx_on,
  906. false);
  907. } else {
  908. at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
  909. at86rf230_xmit_tx_on,
  910. false);
  911. }
  912. } else {
  913. at86rf230_async_state_change(lp, ctx, STATE_TX_ON,
  914. at86rf230_write_frame, false);
  915. }
  916. }
  917. static int
  918. at86rf230_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
  919. {
  920. struct at86rf230_local *lp = hw->priv;
  921. struct at86rf230_state_change *ctx = &lp->tx;
  922. lp->tx_skb = skb;
  923. lp->tx_retry = 0;
  924. /* After 5 minutes in PLL and the same frequency we run again the
  925. * calibration loops which is recommended by at86rf2xx datasheets.
  926. *
  927. * The calibration is initiate by a state change from TRX_OFF
  928. * to TX_ON, the lp->cal_timeout should be reinit by state_delay
  929. * function then to start in the next 5 minutes.
  930. */
  931. if (time_is_before_jiffies(lp->cal_timeout)) {
  932. lp->is_tx_from_off = true;
  933. at86rf230_async_state_change(lp, ctx, STATE_TRX_OFF,
  934. at86rf230_xmit_start, false);
  935. } else {
  936. at86rf230_xmit_start(ctx);
  937. }
  938. return 0;
  939. }
  940. static int
  941. at86rf230_ed(struct ieee802154_hw *hw, u8 *level)
  942. {
  943. BUG_ON(!level);
  944. *level = 0xbe;
  945. return 0;
  946. }
  947. static int
  948. at86rf230_start(struct ieee802154_hw *hw)
  949. {
  950. return at86rf230_sync_state_change(hw->priv, STATE_RX_AACK_ON);
  951. }
  952. static void
  953. at86rf230_stop(struct ieee802154_hw *hw)
  954. {
  955. at86rf230_sync_state_change(hw->priv, STATE_FORCE_TRX_OFF);
  956. }
  957. static int
  958. at86rf23x_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  959. {
  960. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  961. }
  962. static int
  963. at86rf212_set_channel(struct at86rf230_local *lp, u8 page, u8 channel)
  964. {
  965. int rc;
  966. if (channel == 0)
  967. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
  968. else
  969. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
  970. if (rc < 0)
  971. return rc;
  972. if (page == 0) {
  973. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
  974. lp->data->rssi_base_val = -100;
  975. } else {
  976. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
  977. lp->data->rssi_base_val = -98;
  978. }
  979. if (rc < 0)
  980. return rc;
  981. /* This sets the symbol_duration according frequency on the 212.
  982. * TODO move this handling while set channel and page in cfg802154.
  983. * We can do that, this timings are according 802.15.4 standard.
  984. * If we do that in cfg802154, this is a more generic calculation.
  985. *
  986. * This should also protected from ifs_timer. Means cancel timer and
  987. * init with a new value. For now, this is okay.
  988. */
  989. if (channel == 0) {
  990. if (page == 0) {
  991. /* SUB:0 and BPSK:0 -> BPSK-20 */
  992. lp->hw->phy->symbol_duration = 50;
  993. } else {
  994. /* SUB:1 and BPSK:0 -> BPSK-40 */
  995. lp->hw->phy->symbol_duration = 25;
  996. }
  997. } else {
  998. if (page == 0)
  999. /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
  1000. lp->hw->phy->symbol_duration = 40;
  1001. else
  1002. /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
  1003. lp->hw->phy->symbol_duration = 16;
  1004. }
  1005. lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
  1006. lp->hw->phy->symbol_duration;
  1007. lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
  1008. lp->hw->phy->symbol_duration;
  1009. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  1010. }
  1011. static int
  1012. at86rf230_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
  1013. {
  1014. struct at86rf230_local *lp = hw->priv;
  1015. int rc;
  1016. rc = lp->data->set_channel(lp, page, channel);
  1017. /* Wait for PLL */
  1018. usleep_range(lp->data->t_channel_switch,
  1019. lp->data->t_channel_switch + 10);
  1020. lp->cal_timeout = jiffies + AT86RF2XX_CAL_LOOP_TIMEOUT;
  1021. return rc;
  1022. }
  1023. static int
  1024. at86rf230_set_hw_addr_filt(struct ieee802154_hw *hw,
  1025. struct ieee802154_hw_addr_filt *filt,
  1026. unsigned long changed)
  1027. {
  1028. struct at86rf230_local *lp = hw->priv;
  1029. if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
  1030. u16 addr = le16_to_cpu(filt->short_addr);
  1031. dev_vdbg(&lp->spi->dev,
  1032. "at86rf230_set_hw_addr_filt called for saddr\n");
  1033. __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
  1034. __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
  1035. }
  1036. if (changed & IEEE802154_AFILT_PANID_CHANGED) {
  1037. u16 pan = le16_to_cpu(filt->pan_id);
  1038. dev_vdbg(&lp->spi->dev,
  1039. "at86rf230_set_hw_addr_filt called for pan id\n");
  1040. __at86rf230_write(lp, RG_PAN_ID_0, pan);
  1041. __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
  1042. }
  1043. if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
  1044. u8 i, addr[8];
  1045. memcpy(addr, &filt->ieee_addr, 8);
  1046. dev_vdbg(&lp->spi->dev,
  1047. "at86rf230_set_hw_addr_filt called for IEEE addr\n");
  1048. for (i = 0; i < 8; i++)
  1049. __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
  1050. }
  1051. if (changed & IEEE802154_AFILT_PANC_CHANGED) {
  1052. dev_vdbg(&lp->spi->dev,
  1053. "at86rf230_set_hw_addr_filt called for panc change\n");
  1054. if (filt->pan_coord)
  1055. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
  1056. else
  1057. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
  1058. }
  1059. return 0;
  1060. }
  1061. static int
  1062. at86rf230_set_txpower(struct ieee802154_hw *hw, s8 db)
  1063. {
  1064. struct at86rf230_local *lp = hw->priv;
  1065. /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
  1066. * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
  1067. * 0dB.
  1068. * thus, supported values for db range from -26 to 5, for 31dB of
  1069. * reduction to 0dB of reduction.
  1070. */
  1071. if (db > 5 || db < -26)
  1072. return -EINVAL;
  1073. db = -(db - 5);
  1074. return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
  1075. }
  1076. static int
  1077. at86rf230_set_lbt(struct ieee802154_hw *hw, bool on)
  1078. {
  1079. struct at86rf230_local *lp = hw->priv;
  1080. return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
  1081. }
  1082. static int
  1083. at86rf230_set_cca_mode(struct ieee802154_hw *hw,
  1084. const struct wpan_phy_cca *cca)
  1085. {
  1086. struct at86rf230_local *lp = hw->priv;
  1087. u8 val;
  1088. /* mapping 802.15.4 to driver spec */
  1089. switch (cca->mode) {
  1090. case NL802154_CCA_ENERGY:
  1091. val = 1;
  1092. break;
  1093. case NL802154_CCA_CARRIER:
  1094. val = 2;
  1095. break;
  1096. case NL802154_CCA_ENERGY_CARRIER:
  1097. switch (cca->opt) {
  1098. case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
  1099. val = 3;
  1100. break;
  1101. case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
  1102. val = 0;
  1103. break;
  1104. default:
  1105. return -EINVAL;
  1106. }
  1107. break;
  1108. default:
  1109. return -EINVAL;
  1110. }
  1111. return at86rf230_write_subreg(lp, SR_CCA_MODE, val);
  1112. }
  1113. static int
  1114. at86rf212_get_desens_steps(struct at86rf230_local *lp, s32 level)
  1115. {
  1116. return (level - lp->data->rssi_base_val) * 100 / 207;
  1117. }
  1118. static int
  1119. at86rf23x_get_desens_steps(struct at86rf230_local *lp, s32 level)
  1120. {
  1121. return (level - lp->data->rssi_base_val) / 2;
  1122. }
  1123. static int
  1124. at86rf230_set_cca_ed_level(struct ieee802154_hw *hw, s32 level)
  1125. {
  1126. struct at86rf230_local *lp = hw->priv;
  1127. if (level < lp->data->rssi_base_val || level > 30)
  1128. return -EINVAL;
  1129. return at86rf230_write_subreg(lp, SR_CCA_ED_THRES,
  1130. lp->data->get_desense_steps(lp, level));
  1131. }
  1132. static int
  1133. at86rf230_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be,
  1134. u8 retries)
  1135. {
  1136. struct at86rf230_local *lp = hw->priv;
  1137. int rc;
  1138. rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
  1139. if (rc)
  1140. return rc;
  1141. rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
  1142. if (rc)
  1143. return rc;
  1144. return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
  1145. }
  1146. static int
  1147. at86rf230_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
  1148. {
  1149. struct at86rf230_local *lp = hw->priv;
  1150. int rc = 0;
  1151. lp->tx_aret = retries >= 0;
  1152. lp->max_frame_retries = retries;
  1153. if (retries >= 0)
  1154. rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
  1155. return rc;
  1156. }
  1157. static int
  1158. at86rf230_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
  1159. {
  1160. struct at86rf230_local *lp = hw->priv;
  1161. int rc;
  1162. if (on) {
  1163. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 1);
  1164. if (rc < 0)
  1165. return rc;
  1166. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 1);
  1167. if (rc < 0)
  1168. return rc;
  1169. } else {
  1170. rc = at86rf230_write_subreg(lp, SR_AACK_PROM_MODE, 0);
  1171. if (rc < 0)
  1172. return rc;
  1173. rc = at86rf230_write_subreg(lp, SR_AACK_DIS_ACK, 0);
  1174. if (rc < 0)
  1175. return rc;
  1176. }
  1177. return 0;
  1178. }
  1179. static const struct ieee802154_ops at86rf230_ops = {
  1180. .owner = THIS_MODULE,
  1181. .xmit_async = at86rf230_xmit,
  1182. .ed = at86rf230_ed,
  1183. .set_channel = at86rf230_channel,
  1184. .start = at86rf230_start,
  1185. .stop = at86rf230_stop,
  1186. .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
  1187. .set_txpower = at86rf230_set_txpower,
  1188. .set_lbt = at86rf230_set_lbt,
  1189. .set_cca_mode = at86rf230_set_cca_mode,
  1190. .set_cca_ed_level = at86rf230_set_cca_ed_level,
  1191. .set_csma_params = at86rf230_set_csma_params,
  1192. .set_frame_retries = at86rf230_set_frame_retries,
  1193. .set_promiscuous_mode = at86rf230_set_promiscuous_mode,
  1194. };
  1195. static struct at86rf2xx_chip_data at86rf233_data = {
  1196. .t_sleep_cycle = 330,
  1197. .t_channel_switch = 11,
  1198. .t_reset_to_off = 26,
  1199. .t_off_to_aack = 80,
  1200. .t_off_to_tx_on = 80,
  1201. .t_frame = 4096,
  1202. .t_p_ack = 545,
  1203. .rssi_base_val = -91,
  1204. .set_channel = at86rf23x_set_channel,
  1205. .get_desense_steps = at86rf23x_get_desens_steps
  1206. };
  1207. static struct at86rf2xx_chip_data at86rf231_data = {
  1208. .t_sleep_cycle = 330,
  1209. .t_channel_switch = 24,
  1210. .t_reset_to_off = 37,
  1211. .t_off_to_aack = 110,
  1212. .t_off_to_tx_on = 110,
  1213. .t_frame = 4096,
  1214. .t_p_ack = 545,
  1215. .rssi_base_val = -91,
  1216. .set_channel = at86rf23x_set_channel,
  1217. .get_desense_steps = at86rf23x_get_desens_steps
  1218. };
  1219. static struct at86rf2xx_chip_data at86rf212_data = {
  1220. .t_sleep_cycle = 330,
  1221. .t_channel_switch = 11,
  1222. .t_reset_to_off = 26,
  1223. .t_off_to_aack = 200,
  1224. .t_off_to_tx_on = 200,
  1225. .t_frame = 4096,
  1226. .t_p_ack = 545,
  1227. .rssi_base_val = -100,
  1228. .set_channel = at86rf212_set_channel,
  1229. .get_desense_steps = at86rf212_get_desens_steps
  1230. };
  1231. static int at86rf230_hw_init(struct at86rf230_local *lp, u8 xtal_trim)
  1232. {
  1233. int rc, irq_type, irq_pol = IRQ_ACTIVE_HIGH;
  1234. unsigned int dvdd;
  1235. u8 csma_seed[2];
  1236. rc = at86rf230_sync_state_change(lp, STATE_FORCE_TRX_OFF);
  1237. if (rc)
  1238. return rc;
  1239. irq_type = irq_get_trigger_type(lp->spi->irq);
  1240. if (irq_type == IRQ_TYPE_EDGE_RISING ||
  1241. irq_type == IRQ_TYPE_EDGE_FALLING)
  1242. dev_warn(&lp->spi->dev,
  1243. "Using edge triggered irq's are not recommended!\n");
  1244. if (irq_type == IRQ_TYPE_EDGE_FALLING ||
  1245. irq_type == IRQ_TYPE_LEVEL_LOW)
  1246. irq_pol = IRQ_ACTIVE_LOW;
  1247. rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
  1248. if (rc)
  1249. return rc;
  1250. rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
  1251. if (rc)
  1252. return rc;
  1253. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
  1254. if (rc)
  1255. return rc;
  1256. /* reset values differs in at86rf231 and at86rf233 */
  1257. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK_MODE, 0);
  1258. if (rc)
  1259. return rc;
  1260. get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
  1261. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
  1262. if (rc)
  1263. return rc;
  1264. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
  1265. if (rc)
  1266. return rc;
  1267. /* CLKM changes are applied immediately */
  1268. rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
  1269. if (rc)
  1270. return rc;
  1271. /* Turn CLKM Off */
  1272. rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
  1273. if (rc)
  1274. return rc;
  1275. /* Wait the next SLEEP cycle */
  1276. usleep_range(lp->data->t_sleep_cycle,
  1277. lp->data->t_sleep_cycle + 100);
  1278. /* xtal_trim value is calculated by:
  1279. * CL = 0.5 * (CX + CTRIM + CPAR)
  1280. *
  1281. * whereas:
  1282. * CL = capacitor of used crystal
  1283. * CX = connected capacitors at xtal pins
  1284. * CPAR = in all at86rf2xx datasheets this is a constant value 3 pF,
  1285. * but this is different on each board setup. You need to fine
  1286. * tuning this value via CTRIM.
  1287. * CTRIM = variable capacitor setting. Resolution is 0.3 pF range is
  1288. * 0 pF upto 4.5 pF.
  1289. *
  1290. * Examples:
  1291. * atben transceiver:
  1292. *
  1293. * CL = 8 pF
  1294. * CX = 12 pF
  1295. * CPAR = 3 pF (We assume the magic constant from datasheet)
  1296. * CTRIM = 0.9 pF
  1297. *
  1298. * (12+0.9+3)/2 = 7.95 which is nearly at 8 pF
  1299. *
  1300. * xtal_trim = 0x3
  1301. *
  1302. * openlabs transceiver:
  1303. *
  1304. * CL = 16 pF
  1305. * CX = 22 pF
  1306. * CPAR = 3 pF (We assume the magic constant from datasheet)
  1307. * CTRIM = 4.5 pF
  1308. *
  1309. * (22+4.5+3)/2 = 14.75 which is the nearest value to 16 pF
  1310. *
  1311. * xtal_trim = 0xf
  1312. */
  1313. rc = at86rf230_write_subreg(lp, SR_XTAL_TRIM, xtal_trim);
  1314. if (rc)
  1315. return rc;
  1316. rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
  1317. if (rc)
  1318. return rc;
  1319. if (!dvdd) {
  1320. dev_err(&lp->spi->dev, "DVDD error\n");
  1321. return -EINVAL;
  1322. }
  1323. /* Force setting slotted operation bit to 0. Sometimes the atben
  1324. * sets this bit and I don't know why. We set this always force
  1325. * to zero while probing.
  1326. */
  1327. return at86rf230_write_subreg(lp, SR_SLOTTED_OPERATION, 0);
  1328. }
  1329. static int
  1330. at86rf230_get_pdata(struct spi_device *spi, int *rstn, int *slp_tr,
  1331. u8 *xtal_trim)
  1332. {
  1333. struct at86rf230_platform_data *pdata = spi->dev.platform_data;
  1334. int ret;
  1335. if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node) {
  1336. if (!pdata)
  1337. return -ENOENT;
  1338. *rstn = pdata->rstn;
  1339. *slp_tr = pdata->slp_tr;
  1340. *xtal_trim = pdata->xtal_trim;
  1341. return 0;
  1342. }
  1343. *rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
  1344. *slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
  1345. ret = of_property_read_u8(spi->dev.of_node, "xtal-trim", xtal_trim);
  1346. if (ret < 0 && ret != -EINVAL)
  1347. return ret;
  1348. return 0;
  1349. }
  1350. static int
  1351. at86rf230_detect_device(struct at86rf230_local *lp)
  1352. {
  1353. unsigned int part, version, val;
  1354. u16 man_id = 0;
  1355. const char *chip;
  1356. int rc;
  1357. rc = __at86rf230_read(lp, RG_MAN_ID_0, &val);
  1358. if (rc)
  1359. return rc;
  1360. man_id |= val;
  1361. rc = __at86rf230_read(lp, RG_MAN_ID_1, &val);
  1362. if (rc)
  1363. return rc;
  1364. man_id |= (val << 8);
  1365. rc = __at86rf230_read(lp, RG_PART_NUM, &part);
  1366. if (rc)
  1367. return rc;
  1368. rc = __at86rf230_read(lp, RG_VERSION_NUM, &version);
  1369. if (rc)
  1370. return rc;
  1371. if (man_id != 0x001f) {
  1372. dev_err(&lp->spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
  1373. man_id >> 8, man_id & 0xFF);
  1374. return -EINVAL;
  1375. }
  1376. lp->hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AACK |
  1377. IEEE802154_HW_TXPOWER | IEEE802154_HW_ARET |
  1378. IEEE802154_HW_AFILT | IEEE802154_HW_PROMISCUOUS;
  1379. lp->hw->phy->cca.mode = NL802154_CCA_ENERGY;
  1380. switch (part) {
  1381. case 2:
  1382. chip = "at86rf230";
  1383. rc = -ENOTSUPP;
  1384. break;
  1385. case 3:
  1386. chip = "at86rf231";
  1387. lp->data = &at86rf231_data;
  1388. lp->hw->phy->channels_supported[0] = 0x7FFF800;
  1389. lp->hw->phy->current_channel = 11;
  1390. lp->hw->phy->symbol_duration = 16;
  1391. break;
  1392. case 7:
  1393. chip = "at86rf212";
  1394. lp->data = &at86rf212_data;
  1395. lp->hw->flags |= IEEE802154_HW_LBT;
  1396. lp->hw->phy->channels_supported[0] = 0x00007FF;
  1397. lp->hw->phy->channels_supported[2] = 0x00007FF;
  1398. lp->hw->phy->current_channel = 5;
  1399. lp->hw->phy->symbol_duration = 25;
  1400. break;
  1401. case 11:
  1402. chip = "at86rf233";
  1403. lp->data = &at86rf233_data;
  1404. lp->hw->phy->channels_supported[0] = 0x7FFF800;
  1405. lp->hw->phy->current_channel = 13;
  1406. lp->hw->phy->symbol_duration = 16;
  1407. break;
  1408. default:
  1409. chip = "unknown";
  1410. rc = -ENOTSUPP;
  1411. break;
  1412. }
  1413. dev_info(&lp->spi->dev, "Detected %s chip version %d\n", chip, version);
  1414. return rc;
  1415. }
  1416. static void
  1417. at86rf230_setup_spi_messages(struct at86rf230_local *lp)
  1418. {
  1419. lp->state.lp = lp;
  1420. lp->state.irq = lp->spi->irq;
  1421. spi_message_init(&lp->state.msg);
  1422. lp->state.msg.context = &lp->state;
  1423. lp->state.trx.len = 2;
  1424. lp->state.trx.tx_buf = lp->state.buf;
  1425. lp->state.trx.rx_buf = lp->state.buf;
  1426. spi_message_add_tail(&lp->state.trx, &lp->state.msg);
  1427. hrtimer_init(&lp->state.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1428. lp->state.timer.function = at86rf230_async_state_timer;
  1429. lp->irq.lp = lp;
  1430. lp->irq.irq = lp->spi->irq;
  1431. spi_message_init(&lp->irq.msg);
  1432. lp->irq.msg.context = &lp->irq;
  1433. lp->irq.trx.len = 2;
  1434. lp->irq.trx.tx_buf = lp->irq.buf;
  1435. lp->irq.trx.rx_buf = lp->irq.buf;
  1436. spi_message_add_tail(&lp->irq.trx, &lp->irq.msg);
  1437. hrtimer_init(&lp->irq.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1438. lp->irq.timer.function = at86rf230_async_state_timer;
  1439. lp->tx.lp = lp;
  1440. lp->tx.irq = lp->spi->irq;
  1441. spi_message_init(&lp->tx.msg);
  1442. lp->tx.msg.context = &lp->tx;
  1443. lp->tx.trx.len = 2;
  1444. lp->tx.trx.tx_buf = lp->tx.buf;
  1445. lp->tx.trx.rx_buf = lp->tx.buf;
  1446. spi_message_add_tail(&lp->tx.trx, &lp->tx.msg);
  1447. hrtimer_init(&lp->tx.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1448. lp->tx.timer.function = at86rf230_async_state_timer;
  1449. }
  1450. static int at86rf230_probe(struct spi_device *spi)
  1451. {
  1452. struct ieee802154_hw *hw;
  1453. struct at86rf230_local *lp;
  1454. unsigned int status;
  1455. int rc, irq_type, rstn, slp_tr;
  1456. u8 xtal_trim = 0;
  1457. if (!spi->irq) {
  1458. dev_err(&spi->dev, "no IRQ specified\n");
  1459. return -EINVAL;
  1460. }
  1461. rc = at86rf230_get_pdata(spi, &rstn, &slp_tr, &xtal_trim);
  1462. if (rc < 0) {
  1463. dev_err(&spi->dev, "failed to parse platform_data: %d\n", rc);
  1464. return rc;
  1465. }
  1466. if (gpio_is_valid(rstn)) {
  1467. rc = devm_gpio_request_one(&spi->dev, rstn,
  1468. GPIOF_OUT_INIT_HIGH, "rstn");
  1469. if (rc)
  1470. return rc;
  1471. }
  1472. if (gpio_is_valid(slp_tr)) {
  1473. rc = devm_gpio_request_one(&spi->dev, slp_tr,
  1474. GPIOF_OUT_INIT_LOW, "slp_tr");
  1475. if (rc)
  1476. return rc;
  1477. }
  1478. /* Reset */
  1479. if (gpio_is_valid(rstn)) {
  1480. udelay(1);
  1481. gpio_set_value(rstn, 0);
  1482. udelay(1);
  1483. gpio_set_value(rstn, 1);
  1484. usleep_range(120, 240);
  1485. }
  1486. hw = ieee802154_alloc_hw(sizeof(*lp), &at86rf230_ops);
  1487. if (!hw)
  1488. return -ENOMEM;
  1489. lp = hw->priv;
  1490. lp->hw = hw;
  1491. lp->spi = spi;
  1492. lp->slp_tr = slp_tr;
  1493. hw->parent = &spi->dev;
  1494. hw->vif_data_size = sizeof(*lp);
  1495. ieee802154_random_extended_addr(&hw->phy->perm_extended_addr);
  1496. lp->regmap = devm_regmap_init_spi(spi, &at86rf230_regmap_spi_config);
  1497. if (IS_ERR(lp->regmap)) {
  1498. rc = PTR_ERR(lp->regmap);
  1499. dev_err(&spi->dev, "Failed to allocate register map: %d\n",
  1500. rc);
  1501. goto free_dev;
  1502. }
  1503. at86rf230_setup_spi_messages(lp);
  1504. rc = at86rf230_detect_device(lp);
  1505. if (rc < 0)
  1506. goto free_dev;
  1507. init_completion(&lp->state_complete);
  1508. spi_set_drvdata(spi, lp);
  1509. rc = at86rf230_hw_init(lp, xtal_trim);
  1510. if (rc)
  1511. goto free_dev;
  1512. /* Read irq status register to reset irq line */
  1513. rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
  1514. if (rc)
  1515. goto free_dev;
  1516. irq_type = irq_get_trigger_type(spi->irq);
  1517. if (!irq_type)
  1518. irq_type = IRQF_TRIGGER_RISING;
  1519. rc = devm_request_irq(&spi->dev, spi->irq, at86rf230_isr,
  1520. IRQF_SHARED | irq_type, dev_name(&spi->dev), lp);
  1521. if (rc)
  1522. goto free_dev;
  1523. rc = ieee802154_register_hw(lp->hw);
  1524. if (rc)
  1525. goto free_dev;
  1526. return rc;
  1527. free_dev:
  1528. ieee802154_free_hw(lp->hw);
  1529. return rc;
  1530. }
  1531. static int at86rf230_remove(struct spi_device *spi)
  1532. {
  1533. struct at86rf230_local *lp = spi_get_drvdata(spi);
  1534. /* mask all at86rf230 irq's */
  1535. at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
  1536. ieee802154_unregister_hw(lp->hw);
  1537. ieee802154_free_hw(lp->hw);
  1538. dev_dbg(&spi->dev, "unregistered at86rf230\n");
  1539. return 0;
  1540. }
  1541. static const struct of_device_id at86rf230_of_match[] = {
  1542. { .compatible = "atmel,at86rf230", },
  1543. { .compatible = "atmel,at86rf231", },
  1544. { .compatible = "atmel,at86rf233", },
  1545. { .compatible = "atmel,at86rf212", },
  1546. { },
  1547. };
  1548. MODULE_DEVICE_TABLE(of, at86rf230_of_match);
  1549. static const struct spi_device_id at86rf230_device_id[] = {
  1550. { .name = "at86rf230", },
  1551. { .name = "at86rf231", },
  1552. { .name = "at86rf233", },
  1553. { .name = "at86rf212", },
  1554. { },
  1555. };
  1556. MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
  1557. static struct spi_driver at86rf230_driver = {
  1558. .id_table = at86rf230_device_id,
  1559. .driver = {
  1560. .of_match_table = of_match_ptr(at86rf230_of_match),
  1561. .name = "at86rf230",
  1562. .owner = THIS_MODULE,
  1563. },
  1564. .probe = at86rf230_probe,
  1565. .remove = at86rf230_remove,
  1566. };
  1567. module_spi_driver(at86rf230_driver);
  1568. MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
  1569. MODULE_LICENSE("GPL v2");