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