at86rf230.c 44 KB

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