at86rf230.c 44 KB

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