at86rf230.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Written by:
  20. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/gpio.h>
  28. #include <linux/delay.h>
  29. #include <linux/mutex.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/at86rf230.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/of_gpio.h>
  36. #include <net/mac802154.h>
  37. #include <net/wpan-phy.h>
  38. struct at86rf230_local {
  39. struct spi_device *spi;
  40. u8 part;
  41. u8 vers;
  42. u8 buf[2];
  43. struct mutex bmux;
  44. struct work_struct irqwork;
  45. struct completion tx_complete;
  46. struct ieee802154_dev *dev;
  47. spinlock_t lock;
  48. bool irq_busy;
  49. bool is_tx;
  50. bool tx_aret;
  51. int rssi_base_val;
  52. };
  53. static bool is_rf212(struct at86rf230_local *local)
  54. {
  55. return local->part == 7;
  56. }
  57. #define RG_TRX_STATUS (0x01)
  58. #define SR_TRX_STATUS 0x01, 0x1f, 0
  59. #define SR_RESERVED_01_3 0x01, 0x20, 5
  60. #define SR_CCA_STATUS 0x01, 0x40, 6
  61. #define SR_CCA_DONE 0x01, 0x80, 7
  62. #define RG_TRX_STATE (0x02)
  63. #define SR_TRX_CMD 0x02, 0x1f, 0
  64. #define SR_TRAC_STATUS 0x02, 0xe0, 5
  65. #define RG_TRX_CTRL_0 (0x03)
  66. #define SR_CLKM_CTRL 0x03, 0x07, 0
  67. #define SR_CLKM_SHA_SEL 0x03, 0x08, 3
  68. #define SR_PAD_IO_CLKM 0x03, 0x30, 4
  69. #define SR_PAD_IO 0x03, 0xc0, 6
  70. #define RG_TRX_CTRL_1 (0x04)
  71. #define SR_IRQ_POLARITY 0x04, 0x01, 0
  72. #define SR_IRQ_MASK_MODE 0x04, 0x02, 1
  73. #define SR_SPI_CMD_MODE 0x04, 0x0c, 2
  74. #define SR_RX_BL_CTRL 0x04, 0x10, 4
  75. #define SR_TX_AUTO_CRC_ON 0x04, 0x20, 5
  76. #define SR_IRQ_2_EXT_EN 0x04, 0x40, 6
  77. #define SR_PA_EXT_EN 0x04, 0x80, 7
  78. #define RG_PHY_TX_PWR (0x05)
  79. #define SR_TX_PWR 0x05, 0x0f, 0
  80. #define SR_PA_LT 0x05, 0x30, 4
  81. #define SR_PA_BUF_LT 0x05, 0xc0, 6
  82. #define RG_PHY_RSSI (0x06)
  83. #define SR_RSSI 0x06, 0x1f, 0
  84. #define SR_RND_VALUE 0x06, 0x60, 5
  85. #define SR_RX_CRC_VALID 0x06, 0x80, 7
  86. #define RG_PHY_ED_LEVEL (0x07)
  87. #define SR_ED_LEVEL 0x07, 0xff, 0
  88. #define RG_PHY_CC_CCA (0x08)
  89. #define SR_CHANNEL 0x08, 0x1f, 0
  90. #define SR_CCA_MODE 0x08, 0x60, 5
  91. #define SR_CCA_REQUEST 0x08, 0x80, 7
  92. #define RG_CCA_THRES (0x09)
  93. #define SR_CCA_ED_THRES 0x09, 0x0f, 0
  94. #define SR_RESERVED_09_1 0x09, 0xf0, 4
  95. #define RG_RX_CTRL (0x0a)
  96. #define SR_PDT_THRES 0x0a, 0x0f, 0
  97. #define SR_RESERVED_0a_1 0x0a, 0xf0, 4
  98. #define RG_SFD_VALUE (0x0b)
  99. #define SR_SFD_VALUE 0x0b, 0xff, 0
  100. #define RG_TRX_CTRL_2 (0x0c)
  101. #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0
  102. #define SR_SUB_MODE 0x0c, 0x04, 2
  103. #define SR_BPSK_QPSK 0x0c, 0x08, 3
  104. #define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4
  105. #define SR_RESERVED_0c_5 0x0c, 0x60, 5
  106. #define SR_RX_SAFE_MODE 0x0c, 0x80, 7
  107. #define RG_ANT_DIV (0x0d)
  108. #define SR_ANT_CTRL 0x0d, 0x03, 0
  109. #define SR_ANT_EXT_SW_EN 0x0d, 0x04, 2
  110. #define SR_ANT_DIV_EN 0x0d, 0x08, 3
  111. #define SR_RESERVED_0d_2 0x0d, 0x70, 4
  112. #define SR_ANT_SEL 0x0d, 0x80, 7
  113. #define RG_IRQ_MASK (0x0e)
  114. #define SR_IRQ_MASK 0x0e, 0xff, 0
  115. #define RG_IRQ_STATUS (0x0f)
  116. #define SR_IRQ_0_PLL_LOCK 0x0f, 0x01, 0
  117. #define SR_IRQ_1_PLL_UNLOCK 0x0f, 0x02, 1
  118. #define SR_IRQ_2_RX_START 0x0f, 0x04, 2
  119. #define SR_IRQ_3_TRX_END 0x0f, 0x08, 3
  120. #define SR_IRQ_4_CCA_ED_DONE 0x0f, 0x10, 4
  121. #define SR_IRQ_5_AMI 0x0f, 0x20, 5
  122. #define SR_IRQ_6_TRX_UR 0x0f, 0x40, 6
  123. #define SR_IRQ_7_BAT_LOW 0x0f, 0x80, 7
  124. #define RG_VREG_CTRL (0x10)
  125. #define SR_RESERVED_10_6 0x10, 0x03, 0
  126. #define SR_DVDD_OK 0x10, 0x04, 2
  127. #define SR_DVREG_EXT 0x10, 0x08, 3
  128. #define SR_RESERVED_10_3 0x10, 0x30, 4
  129. #define SR_AVDD_OK 0x10, 0x40, 6
  130. #define SR_AVREG_EXT 0x10, 0x80, 7
  131. #define RG_BATMON (0x11)
  132. #define SR_BATMON_VTH 0x11, 0x0f, 0
  133. #define SR_BATMON_HR 0x11, 0x10, 4
  134. #define SR_BATMON_OK 0x11, 0x20, 5
  135. #define SR_RESERVED_11_1 0x11, 0xc0, 6
  136. #define RG_XOSC_CTRL (0x12)
  137. #define SR_XTAL_TRIM 0x12, 0x0f, 0
  138. #define SR_XTAL_MODE 0x12, 0xf0, 4
  139. #define RG_RX_SYN (0x15)
  140. #define SR_RX_PDT_LEVEL 0x15, 0x0f, 0
  141. #define SR_RESERVED_15_2 0x15, 0x70, 4
  142. #define SR_RX_PDT_DIS 0x15, 0x80, 7
  143. #define RG_XAH_CTRL_1 (0x17)
  144. #define SR_RESERVED_17_8 0x17, 0x01, 0
  145. #define SR_AACK_PROM_MODE 0x17, 0x02, 1
  146. #define SR_AACK_ACK_TIME 0x17, 0x04, 2
  147. #define SR_RESERVED_17_5 0x17, 0x08, 3
  148. #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4
  149. #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5
  150. #define SR_CSMA_LBT_MODE 0x17, 0x40, 6
  151. #define SR_RESERVED_17_1 0x17, 0x80, 7
  152. #define RG_FTN_CTRL (0x18)
  153. #define SR_RESERVED_18_2 0x18, 0x7f, 0
  154. #define SR_FTN_START 0x18, 0x80, 7
  155. #define RG_PLL_CF (0x1a)
  156. #define SR_RESERVED_1a_2 0x1a, 0x7f, 0
  157. #define SR_PLL_CF_START 0x1a, 0x80, 7
  158. #define RG_PLL_DCU (0x1b)
  159. #define SR_RESERVED_1b_3 0x1b, 0x3f, 0
  160. #define SR_RESERVED_1b_2 0x1b, 0x40, 6
  161. #define SR_PLL_DCU_START 0x1b, 0x80, 7
  162. #define RG_PART_NUM (0x1c)
  163. #define SR_PART_NUM 0x1c, 0xff, 0
  164. #define RG_VERSION_NUM (0x1d)
  165. #define SR_VERSION_NUM 0x1d, 0xff, 0
  166. #define RG_MAN_ID_0 (0x1e)
  167. #define SR_MAN_ID_0 0x1e, 0xff, 0
  168. #define RG_MAN_ID_1 (0x1f)
  169. #define SR_MAN_ID_1 0x1f, 0xff, 0
  170. #define RG_SHORT_ADDR_0 (0x20)
  171. #define SR_SHORT_ADDR_0 0x20, 0xff, 0
  172. #define RG_SHORT_ADDR_1 (0x21)
  173. #define SR_SHORT_ADDR_1 0x21, 0xff, 0
  174. #define RG_PAN_ID_0 (0x22)
  175. #define SR_PAN_ID_0 0x22, 0xff, 0
  176. #define RG_PAN_ID_1 (0x23)
  177. #define SR_PAN_ID_1 0x23, 0xff, 0
  178. #define RG_IEEE_ADDR_0 (0x24)
  179. #define SR_IEEE_ADDR_0 0x24, 0xff, 0
  180. #define RG_IEEE_ADDR_1 (0x25)
  181. #define SR_IEEE_ADDR_1 0x25, 0xff, 0
  182. #define RG_IEEE_ADDR_2 (0x26)
  183. #define SR_IEEE_ADDR_2 0x26, 0xff, 0
  184. #define RG_IEEE_ADDR_3 (0x27)
  185. #define SR_IEEE_ADDR_3 0x27, 0xff, 0
  186. #define RG_IEEE_ADDR_4 (0x28)
  187. #define SR_IEEE_ADDR_4 0x28, 0xff, 0
  188. #define RG_IEEE_ADDR_5 (0x29)
  189. #define SR_IEEE_ADDR_5 0x29, 0xff, 0
  190. #define RG_IEEE_ADDR_6 (0x2a)
  191. #define SR_IEEE_ADDR_6 0x2a, 0xff, 0
  192. #define RG_IEEE_ADDR_7 (0x2b)
  193. #define SR_IEEE_ADDR_7 0x2b, 0xff, 0
  194. #define RG_XAH_CTRL_0 (0x2c)
  195. #define SR_SLOTTED_OPERATION 0x2c, 0x01, 0
  196. #define SR_MAX_CSMA_RETRIES 0x2c, 0x0e, 1
  197. #define SR_MAX_FRAME_RETRIES 0x2c, 0xf0, 4
  198. #define RG_CSMA_SEED_0 (0x2d)
  199. #define SR_CSMA_SEED_0 0x2d, 0xff, 0
  200. #define RG_CSMA_SEED_1 (0x2e)
  201. #define SR_CSMA_SEED_1 0x2e, 0x07, 0
  202. #define SR_AACK_I_AM_COORD 0x2e, 0x08, 3
  203. #define SR_AACK_DIS_ACK 0x2e, 0x10, 4
  204. #define SR_AACK_SET_PD 0x2e, 0x20, 5
  205. #define SR_AACK_FVN_MODE 0x2e, 0xc0, 6
  206. #define RG_CSMA_BE (0x2f)
  207. #define SR_MIN_BE 0x2f, 0x0f, 0
  208. #define SR_MAX_BE 0x2f, 0xf0, 4
  209. #define CMD_REG 0x80
  210. #define CMD_REG_MASK 0x3f
  211. #define CMD_WRITE 0x40
  212. #define CMD_FB 0x20
  213. #define IRQ_BAT_LOW (1 << 7)
  214. #define IRQ_TRX_UR (1 << 6)
  215. #define IRQ_AMI (1 << 5)
  216. #define IRQ_CCA_ED (1 << 4)
  217. #define IRQ_TRX_END (1 << 3)
  218. #define IRQ_RX_START (1 << 2)
  219. #define IRQ_PLL_UNL (1 << 1)
  220. #define IRQ_PLL_LOCK (1 << 0)
  221. #define IRQ_ACTIVE_HIGH 0
  222. #define IRQ_ACTIVE_LOW 1
  223. #define STATE_P_ON 0x00 /* BUSY */
  224. #define STATE_BUSY_RX 0x01
  225. #define STATE_BUSY_TX 0x02
  226. #define STATE_FORCE_TRX_OFF 0x03
  227. #define STATE_FORCE_TX_ON 0x04 /* IDLE */
  228. /* 0x05 */ /* INVALID_PARAMETER */
  229. #define STATE_RX_ON 0x06
  230. /* 0x07 */ /* SUCCESS */
  231. #define STATE_TRX_OFF 0x08
  232. #define STATE_TX_ON 0x09
  233. /* 0x0a - 0x0e */ /* 0x0a - UNSUPPORTED_ATTRIBUTE */
  234. #define STATE_SLEEP 0x0F
  235. #define STATE_PREP_DEEP_SLEEP 0x10
  236. #define STATE_BUSY_RX_AACK 0x11
  237. #define STATE_BUSY_TX_ARET 0x12
  238. #define STATE_RX_AACK_ON 0x16
  239. #define STATE_TX_ARET_ON 0x19
  240. #define STATE_RX_ON_NOCLK 0x1C
  241. #define STATE_RX_AACK_ON_NOCLK 0x1D
  242. #define STATE_BUSY_RX_AACK_NOCLK 0x1E
  243. #define STATE_TRANSITION_IN_PROGRESS 0x1F
  244. static int
  245. __at86rf230_detect_device(struct spi_device *spi, u16 *man_id, u8 *part,
  246. u8 *version)
  247. {
  248. u8 data[4];
  249. u8 *buf = kmalloc(2, GFP_KERNEL);
  250. int status;
  251. struct spi_message msg;
  252. struct spi_transfer xfer = {
  253. .len = 2,
  254. .tx_buf = buf,
  255. .rx_buf = buf,
  256. };
  257. u8 reg;
  258. if (!buf)
  259. return -ENOMEM;
  260. for (reg = RG_PART_NUM; reg <= RG_MAN_ID_1; reg++) {
  261. buf[0] = (reg & CMD_REG_MASK) | CMD_REG;
  262. buf[1] = 0xff;
  263. dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
  264. spi_message_init(&msg);
  265. spi_message_add_tail(&xfer, &msg);
  266. status = spi_sync(spi, &msg);
  267. dev_vdbg(&spi->dev, "status = %d\n", status);
  268. if (msg.status)
  269. status = msg.status;
  270. dev_vdbg(&spi->dev, "status = %d\n", status);
  271. dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]);
  272. dev_vdbg(&spi->dev, "buf[1] = %02x\n", buf[1]);
  273. if (status == 0)
  274. data[reg - RG_PART_NUM] = buf[1];
  275. else
  276. break;
  277. }
  278. if (status == 0) {
  279. *part = data[0];
  280. *version = data[1];
  281. *man_id = (data[3] << 8) | data[2];
  282. }
  283. kfree(buf);
  284. return status;
  285. }
  286. static int
  287. __at86rf230_write(struct at86rf230_local *lp, u8 addr, u8 data)
  288. {
  289. u8 *buf = lp->buf;
  290. int status;
  291. struct spi_message msg;
  292. struct spi_transfer xfer = {
  293. .len = 2,
  294. .tx_buf = buf,
  295. };
  296. buf[0] = (addr & CMD_REG_MASK) | CMD_REG | CMD_WRITE;
  297. buf[1] = data;
  298. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  299. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  300. spi_message_init(&msg);
  301. spi_message_add_tail(&xfer, &msg);
  302. status = spi_sync(lp->spi, &msg);
  303. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  304. if (msg.status)
  305. status = msg.status;
  306. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  307. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  308. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  309. return status;
  310. }
  311. static int
  312. __at86rf230_read_subreg(struct at86rf230_local *lp,
  313. u8 addr, u8 mask, int shift, u8 *data)
  314. {
  315. u8 *buf = lp->buf;
  316. int status;
  317. struct spi_message msg;
  318. struct spi_transfer xfer = {
  319. .len = 2,
  320. .tx_buf = buf,
  321. .rx_buf = buf,
  322. };
  323. buf[0] = (addr & CMD_REG_MASK) | CMD_REG;
  324. buf[1] = 0xff;
  325. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  326. spi_message_init(&msg);
  327. spi_message_add_tail(&xfer, &msg);
  328. status = spi_sync(lp->spi, &msg);
  329. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  330. if (msg.status)
  331. status = msg.status;
  332. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  333. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  334. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  335. if (status == 0)
  336. *data = (buf[1] & mask) >> shift;
  337. return status;
  338. }
  339. static int
  340. at86rf230_read_subreg(struct at86rf230_local *lp,
  341. u8 addr, u8 mask, int shift, u8 *data)
  342. {
  343. int status;
  344. mutex_lock(&lp->bmux);
  345. status = __at86rf230_read_subreg(lp, addr, mask, shift, data);
  346. mutex_unlock(&lp->bmux);
  347. return status;
  348. }
  349. static int
  350. at86rf230_write_subreg(struct at86rf230_local *lp,
  351. u8 addr, u8 mask, int shift, u8 data)
  352. {
  353. int status;
  354. u8 val;
  355. mutex_lock(&lp->bmux);
  356. status = __at86rf230_read_subreg(lp, addr, 0xff, 0, &val);
  357. if (status)
  358. goto out;
  359. val &= ~mask;
  360. val |= (data << shift) & mask;
  361. status = __at86rf230_write(lp, addr, val);
  362. out:
  363. mutex_unlock(&lp->bmux);
  364. return status;
  365. }
  366. static int
  367. at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len)
  368. {
  369. u8 *buf = lp->buf;
  370. int status;
  371. struct spi_message msg;
  372. struct spi_transfer xfer_head = {
  373. .len = 2,
  374. .tx_buf = buf,
  375. };
  376. struct spi_transfer xfer_buf = {
  377. .len = len,
  378. .tx_buf = data,
  379. };
  380. mutex_lock(&lp->bmux);
  381. buf[0] = CMD_WRITE | CMD_FB;
  382. buf[1] = len + 2; /* 2 bytes for CRC that isn't written */
  383. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  384. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  385. spi_message_init(&msg);
  386. spi_message_add_tail(&xfer_head, &msg);
  387. spi_message_add_tail(&xfer_buf, &msg);
  388. status = spi_sync(lp->spi, &msg);
  389. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  390. if (msg.status)
  391. status = msg.status;
  392. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  393. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  394. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  395. mutex_unlock(&lp->bmux);
  396. return status;
  397. }
  398. static int
  399. at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi)
  400. {
  401. u8 *buf = lp->buf;
  402. int status;
  403. struct spi_message msg;
  404. struct spi_transfer xfer_head = {
  405. .len = 2,
  406. .tx_buf = buf,
  407. .rx_buf = buf,
  408. };
  409. struct spi_transfer xfer_head1 = {
  410. .len = 2,
  411. .tx_buf = buf,
  412. .rx_buf = buf,
  413. };
  414. struct spi_transfer xfer_buf = {
  415. .len = 0,
  416. .rx_buf = data,
  417. };
  418. mutex_lock(&lp->bmux);
  419. buf[0] = CMD_FB;
  420. buf[1] = 0x00;
  421. spi_message_init(&msg);
  422. spi_message_add_tail(&xfer_head, &msg);
  423. status = spi_sync(lp->spi, &msg);
  424. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  425. xfer_buf.len = *(buf + 1) + 1;
  426. *len = buf[1];
  427. buf[0] = CMD_FB;
  428. buf[1] = 0x00;
  429. spi_message_init(&msg);
  430. spi_message_add_tail(&xfer_head1, &msg);
  431. spi_message_add_tail(&xfer_buf, &msg);
  432. status = spi_sync(lp->spi, &msg);
  433. if (msg.status)
  434. status = msg.status;
  435. dev_vdbg(&lp->spi->dev, "status = %d\n", status);
  436. dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]);
  437. dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]);
  438. if (status) {
  439. if (lqi && (*len > lp->buf[1]))
  440. *lqi = data[lp->buf[1]];
  441. }
  442. mutex_unlock(&lp->bmux);
  443. return status;
  444. }
  445. static int
  446. at86rf230_ed(struct ieee802154_dev *dev, u8 *level)
  447. {
  448. might_sleep();
  449. BUG_ON(!level);
  450. *level = 0xbe;
  451. return 0;
  452. }
  453. static int
  454. at86rf230_state(struct ieee802154_dev *dev, int state)
  455. {
  456. struct at86rf230_local *lp = dev->priv;
  457. int rc;
  458. u8 val;
  459. u8 desired_status;
  460. might_sleep();
  461. if (state == STATE_FORCE_TX_ON)
  462. desired_status = STATE_TX_ON;
  463. else if (state == STATE_FORCE_TRX_OFF)
  464. desired_status = STATE_TRX_OFF;
  465. else
  466. desired_status = state;
  467. do {
  468. rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
  469. if (rc)
  470. goto err;
  471. } while (val == STATE_TRANSITION_IN_PROGRESS);
  472. if (val == desired_status)
  473. return 0;
  474. /* state is equal to phy states */
  475. rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state);
  476. if (rc)
  477. goto err;
  478. do {
  479. rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val);
  480. if (rc)
  481. goto err;
  482. } while (val == STATE_TRANSITION_IN_PROGRESS);
  483. if (val == desired_status ||
  484. (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) ||
  485. (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK))
  486. return 0;
  487. pr_err("unexpected state change: %d, asked for %d\n", val, state);
  488. return -EBUSY;
  489. err:
  490. pr_err("error: %d\n", rc);
  491. return rc;
  492. }
  493. static int
  494. at86rf230_start(struct ieee802154_dev *dev)
  495. {
  496. struct at86rf230_local *lp = dev->priv;
  497. u8 rc;
  498. rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1);
  499. if (rc)
  500. return rc;
  501. rc = at86rf230_state(dev, STATE_TX_ON);
  502. if (rc)
  503. return rc;
  504. return at86rf230_state(dev, STATE_RX_AACK_ON);
  505. }
  506. static void
  507. at86rf230_stop(struct ieee802154_dev *dev)
  508. {
  509. at86rf230_state(dev, STATE_FORCE_TRX_OFF);
  510. }
  511. static int
  512. at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel)
  513. {
  514. lp->rssi_base_val = -91;
  515. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  516. }
  517. static int
  518. at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel)
  519. {
  520. int rc;
  521. if (channel == 0)
  522. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0);
  523. else
  524. rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1);
  525. if (rc < 0)
  526. return rc;
  527. if (page == 0) {
  528. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0);
  529. lp->rssi_base_val = -100;
  530. } else {
  531. rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1);
  532. lp->rssi_base_val = -98;
  533. }
  534. if (rc < 0)
  535. return rc;
  536. return at86rf230_write_subreg(lp, SR_CHANNEL, channel);
  537. }
  538. static int
  539. at86rf230_channel(struct ieee802154_dev *dev, int page, int channel)
  540. {
  541. struct at86rf230_local *lp = dev->priv;
  542. int rc;
  543. might_sleep();
  544. if (page < 0 || page > 31 ||
  545. !(lp->dev->phy->channels_supported[page] & BIT(channel))) {
  546. WARN_ON(1);
  547. return -EINVAL;
  548. }
  549. if (is_rf212(lp))
  550. rc = at86rf212_set_channel(lp, page, channel);
  551. else
  552. rc = at86rf230_set_channel(lp, page, channel);
  553. if (rc < 0)
  554. return rc;
  555. msleep(1); /* Wait for PLL */
  556. dev->phy->current_channel = channel;
  557. dev->phy->current_page = page;
  558. return 0;
  559. }
  560. static int
  561. at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb)
  562. {
  563. struct at86rf230_local *lp = dev->priv;
  564. int rc;
  565. unsigned long flags;
  566. spin_lock_irqsave(&lp->lock, flags);
  567. if (lp->irq_busy) {
  568. spin_unlock_irqrestore(&lp->lock, flags);
  569. return -EBUSY;
  570. }
  571. spin_unlock_irqrestore(&lp->lock, flags);
  572. might_sleep();
  573. rc = at86rf230_state(dev, STATE_FORCE_TX_ON);
  574. if (rc)
  575. goto err;
  576. spin_lock_irqsave(&lp->lock, flags);
  577. lp->is_tx = 1;
  578. reinit_completion(&lp->tx_complete);
  579. spin_unlock_irqrestore(&lp->lock, flags);
  580. rc = at86rf230_write_fbuf(lp, skb->data, skb->len);
  581. if (rc)
  582. goto err_rx;
  583. if (lp->tx_aret) {
  584. rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ARET_ON);
  585. if (rc)
  586. goto err_rx;
  587. }
  588. rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX);
  589. if (rc)
  590. goto err_rx;
  591. rc = wait_for_completion_interruptible(&lp->tx_complete);
  592. if (rc < 0)
  593. goto err_rx;
  594. return at86rf230_start(dev);
  595. err_rx:
  596. at86rf230_start(dev);
  597. err:
  598. pr_err("error: %d\n", rc);
  599. spin_lock_irqsave(&lp->lock, flags);
  600. lp->is_tx = 0;
  601. spin_unlock_irqrestore(&lp->lock, flags);
  602. return rc;
  603. }
  604. static int at86rf230_rx(struct at86rf230_local *lp)
  605. {
  606. u8 len = 128, lqi = 0;
  607. struct sk_buff *skb;
  608. skb = alloc_skb(len, GFP_KERNEL);
  609. if (!skb)
  610. return -ENOMEM;
  611. if (at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi))
  612. goto err;
  613. if (len < 2)
  614. goto err;
  615. skb_trim(skb, len - 2); /* We do not put CRC into the frame */
  616. ieee802154_rx_irqsafe(lp->dev, skb, lqi);
  617. dev_dbg(&lp->spi->dev, "READ_FBUF: %d %x\n", len, lqi);
  618. return 0;
  619. err:
  620. pr_debug("received frame is too small\n");
  621. kfree_skb(skb);
  622. return -EINVAL;
  623. }
  624. static int
  625. at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
  626. struct ieee802154_hw_addr_filt *filt,
  627. unsigned long changed)
  628. {
  629. struct at86rf230_local *lp = dev->priv;
  630. if (changed & IEEE802515_AFILT_SADDR_CHANGED) {
  631. u16 addr = le16_to_cpu(filt->short_addr);
  632. dev_vdbg(&lp->spi->dev,
  633. "at86rf230_set_hw_addr_filt called for saddr\n");
  634. __at86rf230_write(lp, RG_SHORT_ADDR_0, addr);
  635. __at86rf230_write(lp, RG_SHORT_ADDR_1, addr >> 8);
  636. }
  637. if (changed & IEEE802515_AFILT_PANID_CHANGED) {
  638. u16 pan = le16_to_cpu(filt->pan_id);
  639. dev_vdbg(&lp->spi->dev,
  640. "at86rf230_set_hw_addr_filt called for pan id\n");
  641. __at86rf230_write(lp, RG_PAN_ID_0, pan);
  642. __at86rf230_write(lp, RG_PAN_ID_1, pan >> 8);
  643. }
  644. if (changed & IEEE802515_AFILT_IEEEADDR_CHANGED) {
  645. u8 i, addr[8];
  646. memcpy(addr, &filt->ieee_addr, 8);
  647. dev_vdbg(&lp->spi->dev,
  648. "at86rf230_set_hw_addr_filt called for IEEE addr\n");
  649. for (i = 0; i < 8; i++)
  650. __at86rf230_write(lp, RG_IEEE_ADDR_0 + i, addr[i]);
  651. }
  652. if (changed & IEEE802515_AFILT_PANC_CHANGED) {
  653. dev_vdbg(&lp->spi->dev,
  654. "at86rf230_set_hw_addr_filt called for panc change\n");
  655. if (filt->pan_coord)
  656. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1);
  657. else
  658. at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0);
  659. }
  660. return 0;
  661. }
  662. static int
  663. at86rf212_set_txpower(struct ieee802154_dev *dev, int db)
  664. {
  665. struct at86rf230_local *lp = dev->priv;
  666. /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
  667. * bits decrease power in 1dB steps. 0x60 represents extra PA gain of
  668. * 0dB.
  669. * thus, supported values for db range from -26 to 5, for 31dB of
  670. * reduction to 0dB of reduction.
  671. */
  672. if (db > 5 || db < -26)
  673. return -EINVAL;
  674. db = -(db - 5);
  675. return __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
  676. }
  677. static int
  678. at86rf212_set_lbt(struct ieee802154_dev *dev, bool on)
  679. {
  680. struct at86rf230_local *lp = dev->priv;
  681. return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on);
  682. }
  683. static int
  684. at86rf212_set_cca_mode(struct ieee802154_dev *dev, u8 mode)
  685. {
  686. struct at86rf230_local *lp = dev->priv;
  687. return at86rf230_write_subreg(lp, SR_CCA_MODE, mode);
  688. }
  689. static int
  690. at86rf212_set_cca_ed_level(struct ieee802154_dev *dev, s32 level)
  691. {
  692. struct at86rf230_local *lp = dev->priv;
  693. int desens_steps;
  694. if (level < lp->rssi_base_val || level > 30)
  695. return -EINVAL;
  696. desens_steps = (level - lp->rssi_base_val) * 100 / 207;
  697. return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, desens_steps);
  698. }
  699. static int
  700. at86rf212_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be,
  701. u8 retries)
  702. {
  703. struct at86rf230_local *lp = dev->priv;
  704. int rc;
  705. if (min_be > max_be || max_be > 8 || retries > 5)
  706. return -EINVAL;
  707. rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be);
  708. if (rc)
  709. return rc;
  710. rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be);
  711. if (rc)
  712. return rc;
  713. return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, retries);
  714. }
  715. static int
  716. at86rf212_set_frame_retries(struct ieee802154_dev *dev, s8 retries)
  717. {
  718. struct at86rf230_local *lp = dev->priv;
  719. int rc = 0;
  720. if (retries < -1 || retries > 15)
  721. return -EINVAL;
  722. lp->tx_aret = retries >= 0;
  723. if (retries >= 0)
  724. rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries);
  725. return rc;
  726. }
  727. static struct ieee802154_ops at86rf230_ops = {
  728. .owner = THIS_MODULE,
  729. .xmit = at86rf230_xmit,
  730. .ed = at86rf230_ed,
  731. .set_channel = at86rf230_channel,
  732. .start = at86rf230_start,
  733. .stop = at86rf230_stop,
  734. .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
  735. };
  736. static struct ieee802154_ops at86rf212_ops = {
  737. .owner = THIS_MODULE,
  738. .xmit = at86rf230_xmit,
  739. .ed = at86rf230_ed,
  740. .set_channel = at86rf230_channel,
  741. .start = at86rf230_start,
  742. .stop = at86rf230_stop,
  743. .set_hw_addr_filt = at86rf230_set_hw_addr_filt,
  744. .set_txpower = at86rf212_set_txpower,
  745. .set_lbt = at86rf212_set_lbt,
  746. .set_cca_mode = at86rf212_set_cca_mode,
  747. .set_cca_ed_level = at86rf212_set_cca_ed_level,
  748. .set_csma_params = at86rf212_set_csma_params,
  749. .set_frame_retries = at86rf212_set_frame_retries,
  750. };
  751. static void at86rf230_irqwork(struct work_struct *work)
  752. {
  753. struct at86rf230_local *lp =
  754. container_of(work, struct at86rf230_local, irqwork);
  755. u8 status = 0, val;
  756. int rc;
  757. unsigned long flags;
  758. rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &val);
  759. status |= val;
  760. status &= ~IRQ_PLL_LOCK; /* ignore */
  761. status &= ~IRQ_RX_START; /* ignore */
  762. status &= ~IRQ_AMI; /* ignore */
  763. status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/
  764. if (status & IRQ_TRX_END) {
  765. status &= ~IRQ_TRX_END;
  766. spin_lock_irqsave(&lp->lock, flags);
  767. if (lp->is_tx) {
  768. lp->is_tx = 0;
  769. spin_unlock_irqrestore(&lp->lock, flags);
  770. complete(&lp->tx_complete);
  771. } else {
  772. spin_unlock_irqrestore(&lp->lock, flags);
  773. at86rf230_rx(lp);
  774. }
  775. }
  776. spin_lock_irqsave(&lp->lock, flags);
  777. lp->irq_busy = 0;
  778. spin_unlock_irqrestore(&lp->lock, flags);
  779. }
  780. static void at86rf230_irqwork_level(struct work_struct *work)
  781. {
  782. struct at86rf230_local *lp =
  783. container_of(work, struct at86rf230_local, irqwork);
  784. at86rf230_irqwork(work);
  785. enable_irq(lp->spi->irq);
  786. }
  787. static irqreturn_t at86rf230_isr(int irq, void *data)
  788. {
  789. struct at86rf230_local *lp = data;
  790. unsigned long flags;
  791. spin_lock_irqsave(&lp->lock, flags);
  792. lp->irq_busy = 1;
  793. spin_unlock_irqrestore(&lp->lock, flags);
  794. schedule_work(&lp->irqwork);
  795. return IRQ_HANDLED;
  796. }
  797. static irqreturn_t at86rf230_isr_level(int irq, void *data)
  798. {
  799. disable_irq_nosync(irq);
  800. return at86rf230_isr(irq, data);
  801. }
  802. static int at86rf230_hw_init(struct at86rf230_local *lp)
  803. {
  804. int rc, irq_pol, irq_type;
  805. u8 dvdd;
  806. u8 csma_seed[2];
  807. rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF);
  808. if (rc)
  809. return rc;
  810. irq_type = irq_get_trigger_type(lp->spi->irq);
  811. /* configure irq polarity, defaults to high active */
  812. if (irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW))
  813. irq_pol = IRQ_ACTIVE_LOW;
  814. else
  815. irq_pol = IRQ_ACTIVE_HIGH;
  816. rc = at86rf230_write_subreg(lp, SR_IRQ_POLARITY, irq_pol);
  817. if (rc)
  818. return rc;
  819. rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, IRQ_TRX_END);
  820. if (rc)
  821. return rc;
  822. get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed));
  823. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]);
  824. if (rc)
  825. return rc;
  826. rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]);
  827. if (rc)
  828. return rc;
  829. /* CLKM changes are applied immediately */
  830. rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00);
  831. if (rc)
  832. return rc;
  833. /* Turn CLKM Off */
  834. rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00);
  835. if (rc)
  836. return rc;
  837. /* Wait the next SLEEP cycle */
  838. msleep(100);
  839. rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &dvdd);
  840. if (rc)
  841. return rc;
  842. if (!dvdd) {
  843. dev_err(&lp->spi->dev, "DVDD error\n");
  844. return -EINVAL;
  845. }
  846. return 0;
  847. }
  848. static struct at86rf230_platform_data *
  849. at86rf230_get_pdata(struct spi_device *spi)
  850. {
  851. struct at86rf230_platform_data *pdata;
  852. if (!IS_ENABLED(CONFIG_OF) || !spi->dev.of_node)
  853. return spi->dev.platform_data;
  854. pdata = devm_kzalloc(&spi->dev, sizeof(*pdata), GFP_KERNEL);
  855. if (!pdata)
  856. goto done;
  857. pdata->rstn = of_get_named_gpio(spi->dev.of_node, "reset-gpio", 0);
  858. pdata->slp_tr = of_get_named_gpio(spi->dev.of_node, "sleep-gpio", 0);
  859. spi->dev.platform_data = pdata;
  860. done:
  861. return pdata;
  862. }
  863. static int at86rf230_probe(struct spi_device *spi)
  864. {
  865. struct at86rf230_platform_data *pdata;
  866. struct ieee802154_dev *dev;
  867. struct at86rf230_local *lp;
  868. u16 man_id = 0;
  869. u8 part = 0, version = 0, status;
  870. irq_handler_t irq_handler;
  871. work_func_t irq_worker;
  872. int rc, irq_type;
  873. const char *chip;
  874. struct ieee802154_ops *ops = NULL;
  875. if (!spi->irq) {
  876. dev_err(&spi->dev, "no IRQ specified\n");
  877. return -EINVAL;
  878. }
  879. pdata = at86rf230_get_pdata(spi);
  880. if (!pdata) {
  881. dev_err(&spi->dev, "no platform_data\n");
  882. return -EINVAL;
  883. }
  884. if (gpio_is_valid(pdata->rstn)) {
  885. rc = devm_gpio_request_one(&spi->dev, pdata->rstn,
  886. GPIOF_OUT_INIT_HIGH, "rstn");
  887. if (rc)
  888. return rc;
  889. }
  890. if (gpio_is_valid(pdata->slp_tr)) {
  891. rc = devm_gpio_request_one(&spi->dev, pdata->slp_tr,
  892. GPIOF_OUT_INIT_LOW, "slp_tr");
  893. if (rc)
  894. return rc;
  895. }
  896. /* Reset */
  897. if (gpio_is_valid(pdata->rstn)) {
  898. udelay(1);
  899. gpio_set_value(pdata->rstn, 0);
  900. udelay(1);
  901. gpio_set_value(pdata->rstn, 1);
  902. usleep_range(120, 240);
  903. }
  904. rc = __at86rf230_detect_device(spi, &man_id, &part, &version);
  905. if (rc < 0)
  906. return rc;
  907. if (man_id != 0x001f) {
  908. dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n",
  909. man_id >> 8, man_id & 0xFF);
  910. return -EINVAL;
  911. }
  912. switch (part) {
  913. case 2:
  914. chip = "at86rf230";
  915. /* FIXME: should be easy to support; */
  916. break;
  917. case 3:
  918. chip = "at86rf231";
  919. ops = &at86rf230_ops;
  920. break;
  921. case 7:
  922. chip = "at86rf212";
  923. if (version == 1)
  924. ops = &at86rf212_ops;
  925. break;
  926. case 11:
  927. chip = "at86rf233";
  928. ops = &at86rf230_ops;
  929. break;
  930. default:
  931. chip = "UNKNOWN";
  932. break;
  933. }
  934. dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version);
  935. if (!ops)
  936. return -ENOTSUPP;
  937. dev = ieee802154_alloc_device(sizeof(*lp), ops);
  938. if (!dev)
  939. return -ENOMEM;
  940. lp = dev->priv;
  941. lp->dev = dev;
  942. lp->part = part;
  943. lp->vers = version;
  944. lp->spi = spi;
  945. dev->parent = &spi->dev;
  946. dev->extra_tx_headroom = 0;
  947. dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK;
  948. irq_type = irq_get_trigger_type(spi->irq);
  949. if (!irq_type)
  950. irq_type = IRQF_TRIGGER_RISING;
  951. if (irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
  952. irq_worker = at86rf230_irqwork;
  953. irq_handler = at86rf230_isr;
  954. } else {
  955. irq_worker = at86rf230_irqwork_level;
  956. irq_handler = at86rf230_isr_level;
  957. }
  958. mutex_init(&lp->bmux);
  959. INIT_WORK(&lp->irqwork, irq_worker);
  960. spin_lock_init(&lp->lock);
  961. init_completion(&lp->tx_complete);
  962. spi_set_drvdata(spi, lp);
  963. if (is_rf212(lp)) {
  964. dev->phy->channels_supported[0] = 0x00007FF;
  965. dev->phy->channels_supported[2] = 0x00007FF;
  966. } else {
  967. dev->phy->channels_supported[0] = 0x7FFF800;
  968. }
  969. rc = at86rf230_hw_init(lp);
  970. if (rc)
  971. goto err_hw_init;
  972. /* Read irq status register to reset irq line */
  973. rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status);
  974. if (rc)
  975. goto err_hw_init;
  976. rc = devm_request_irq(&spi->dev, spi->irq, irq_handler,
  977. IRQF_SHARED | irq_type,
  978. dev_name(&spi->dev), lp);
  979. if (rc)
  980. goto err_hw_init;
  981. rc = ieee802154_register_device(lp->dev);
  982. if (rc)
  983. goto err_hw_init;
  984. return rc;
  985. err_hw_init:
  986. flush_work(&lp->irqwork);
  987. mutex_destroy(&lp->bmux);
  988. ieee802154_free_device(lp->dev);
  989. return rc;
  990. }
  991. static int at86rf230_remove(struct spi_device *spi)
  992. {
  993. struct at86rf230_local *lp = spi_get_drvdata(spi);
  994. /* mask all at86rf230 irq's */
  995. at86rf230_write_subreg(lp, SR_IRQ_MASK, 0);
  996. ieee802154_unregister_device(lp->dev);
  997. flush_work(&lp->irqwork);
  998. mutex_destroy(&lp->bmux);
  999. ieee802154_free_device(lp->dev);
  1000. dev_dbg(&spi->dev, "unregistered at86rf230\n");
  1001. return 0;
  1002. }
  1003. static const struct of_device_id at86rf230_of_match[] = {
  1004. { .compatible = "atmel,at86rf230", },
  1005. { .compatible = "atmel,at86rf231", },
  1006. { .compatible = "atmel,at86rf233", },
  1007. { .compatible = "atmel,at86rf212", },
  1008. { },
  1009. };
  1010. MODULE_DEVICE_TABLE(of, at86rf230_of_match);
  1011. static const struct spi_device_id at86rf230_device_id[] = {
  1012. { .name = "at86rf230", },
  1013. { .name = "at86rf231", },
  1014. { .name = "at86rf233", },
  1015. { .name = "at86rf212", },
  1016. { },
  1017. };
  1018. MODULE_DEVICE_TABLE(spi, at86rf230_device_id);
  1019. static struct spi_driver at86rf230_driver = {
  1020. .id_table = at86rf230_device_id,
  1021. .driver = {
  1022. .of_match_table = of_match_ptr(at86rf230_of_match),
  1023. .name = "at86rf230",
  1024. .owner = THIS_MODULE,
  1025. },
  1026. .probe = at86rf230_probe,
  1027. .remove = at86rf230_remove,
  1028. };
  1029. module_spi_driver(at86rf230_driver);
  1030. MODULE_DESCRIPTION("AT86RF230 Transceiver Driver");
  1031. MODULE_LICENSE("GPL v2");