Root/
Source at commit d8f39e8418e18f9fe0d595aba440e6ea63135164 created 12 years 6 months ago. By Stefan Schmidt, ieee802154/at86rf230.c: Prepare driver for auto ACK support | |
---|---|
1 | /* |
2 | * AT86RF230/RF231 driver |
3 | * |
4 | * Copyright (C) 2009 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 <dmitry.baryshkov@siemens.com> |
21 | */ |
22 | #include <linux/kernel.h> |
23 | #include <linux/module.h> |
24 | #include <linux/interrupt.h> |
25 | #include <linux/gpio.h> |
26 | #include <linux/delay.h> |
27 | #include <linux/mutex.h> |
28 | #include <linux/workqueue.h> |
29 | #include <linux/spi/spi.h> |
30 | #include <linux/spi/at86rf230.h> |
31 | |
32 | #ifdef CONFIG_OF |
33 | #include <linux/of.h> |
34 | #include <linux/of_gpio.h> |
35 | #endif |
36 | |
37 | #include <net/mac802154.h> |
38 | #include <net/wpan-phy.h> |
39 | |
40 | #include "at86rf230.h" |
41 | |
42 | //#define ENABLE_AACK |
43 | |
44 | struct at86rf230_local { |
45 | struct spi_device *spi; |
46 | int rstn, slp_tr, dig2; |
47 | void (*reset)(void *reset_data); |
48 | void *reset_data; |
49 | |
50 | u8 part; |
51 | u8 vers; |
52 | |
53 | u8 buf[2]; |
54 | struct mutex bmux; |
55 | |
56 | struct work_struct irqwork; |
57 | struct completion tx_complete; |
58 | |
59 | struct ieee802154_dev *dev; |
60 | |
61 | volatile unsigned is_tx:1; |
62 | }; |
63 | |
64 | |
65 | static int |
66 | __at86rf230_write(struct at86rf230_local *lp, u8 addr, u8 data) |
67 | { |
68 | u8 *buf = lp->buf; |
69 | int status; |
70 | struct spi_message msg; |
71 | struct spi_transfer xfer = { |
72 | .len = 2, |
73 | .tx_buf = buf, |
74 | }; |
75 | |
76 | buf[0] = (addr & CMD_REG_MASK) | CMD_REG | CMD_WRITE; |
77 | buf[1] = data; |
78 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
79 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
80 | spi_message_init(&msg); |
81 | spi_message_add_tail(&xfer, &msg); |
82 | |
83 | status = spi_sync(lp->spi, &msg); |
84 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
85 | if (msg.status) |
86 | status = msg.status; |
87 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
88 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
89 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
90 | |
91 | return status; |
92 | } |
93 | |
94 | static int |
95 | __at86rf230_read_subreg(struct at86rf230_local *lp, |
96 | u8 addr, u8 mask, int shift, u8 *data) |
97 | { |
98 | u8 *buf = lp->buf; |
99 | int status; |
100 | struct spi_message msg; |
101 | struct spi_transfer xfer = { |
102 | .len = 2, |
103 | .tx_buf = buf, |
104 | .rx_buf = buf, |
105 | }; |
106 | |
107 | buf[0] = (addr & CMD_REG_MASK) | CMD_REG; |
108 | buf[1] = 0xff; |
109 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
110 | spi_message_init(&msg); |
111 | spi_message_add_tail(&xfer, &msg); |
112 | |
113 | status = spi_sync(lp->spi, &msg); |
114 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
115 | if (msg.status) |
116 | status = msg.status; |
117 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
118 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
119 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
120 | |
121 | if (status == 0) |
122 | *data = buf[1]; |
123 | |
124 | return status; |
125 | } |
126 | |
127 | static int |
128 | at86rf230_read_subreg(struct at86rf230_local *lp, |
129 | u8 addr, u8 mask, int shift, u8 *data) |
130 | { |
131 | int status; |
132 | |
133 | mutex_lock(&lp->bmux); |
134 | status = __at86rf230_read_subreg(lp, addr, mask, shift, data); |
135 | mutex_unlock(&lp->bmux); |
136 | |
137 | return status; |
138 | } |
139 | |
140 | static int |
141 | at86rf230_write_subreg(struct at86rf230_local *lp, |
142 | u8 addr, u8 mask, int shift, u8 data) |
143 | { |
144 | int status; |
145 | u8 val; |
146 | |
147 | mutex_lock(&lp->bmux); |
148 | status = __at86rf230_read_subreg(lp, addr, 0xff, 0, &val); |
149 | if (status) |
150 | goto out; |
151 | |
152 | val &= ~mask; |
153 | val |= (data << shift) & mask; |
154 | |
155 | status = __at86rf230_write(lp, addr, val); |
156 | out: |
157 | mutex_unlock(&lp->bmux); |
158 | |
159 | return status; |
160 | } |
161 | |
162 | static int |
163 | at86rf230_write_fbuf(struct at86rf230_local *lp, u8 *data, u8 len) |
164 | { |
165 | u8 *buf = lp->buf; |
166 | int status; |
167 | struct spi_message msg; |
168 | struct spi_transfer xfer_head = { |
169 | .len = 2, |
170 | .tx_buf = buf, |
171 | |
172 | }; |
173 | struct spi_transfer xfer_buf = { |
174 | .len = len, |
175 | .tx_buf = data, |
176 | }; |
177 | |
178 | mutex_lock(&lp->bmux); |
179 | buf[0] = CMD_WRITE | CMD_FB; |
180 | buf[1] = len + 2; /* 2 bytes for CRC that isn't written */ |
181 | |
182 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
183 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
184 | |
185 | spi_message_init(&msg); |
186 | spi_message_add_tail(&xfer_head, &msg); |
187 | spi_message_add_tail(&xfer_buf, &msg); |
188 | |
189 | status = spi_sync(lp->spi, &msg); |
190 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
191 | if (msg.status) |
192 | status = msg.status; |
193 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
194 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
195 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
196 | |
197 | mutex_unlock(&lp->bmux); |
198 | return status; |
199 | } |
200 | |
201 | static int |
202 | at86rf230_read_fbuf(struct at86rf230_local *lp, u8 *data, u8 *len, u8 *lqi) |
203 | { |
204 | u8 *buf = lp->buf; |
205 | int status; |
206 | struct spi_message msg; |
207 | struct spi_transfer xfer_head = { |
208 | .len = 2, |
209 | .tx_buf = buf, |
210 | .rx_buf = buf, |
211 | }; |
212 | struct spi_transfer xfer_head1 = { |
213 | .len = 2, |
214 | .tx_buf = buf, |
215 | .rx_buf = buf, |
216 | }; |
217 | struct spi_transfer xfer_buf = { |
218 | .len = 0, |
219 | .rx_buf = data, |
220 | }; |
221 | |
222 | mutex_lock(&lp->bmux); |
223 | |
224 | buf[0] = CMD_FB; |
225 | buf[1] = 0x00; |
226 | |
227 | spi_message_init(&msg); |
228 | spi_message_add_tail(&xfer_head, &msg); |
229 | |
230 | status = spi_sync(lp->spi, &msg); |
231 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
232 | |
233 | if (buf[1] & 0x80) { |
234 | dev_err(&lp->spi->dev, "invalid PHR 0x%02x\n", buf[1]); |
235 | status = -EIO; |
236 | goto fail; |
237 | } |
238 | if (buf[1] >= *len) { |
239 | dev_err(&lp->spi->dev, "PHR 0x%02x >= buffer %d bytes\n", |
240 | buf[1], *len); |
241 | status = -EMSGSIZE; |
242 | goto fail; |
243 | } |
244 | xfer_buf.len = *(buf + 1) + 1; |
245 | *len = buf[1]; |
246 | |
247 | buf[0] = CMD_FB; |
248 | buf[1] = 0x00; |
249 | |
250 | spi_message_init(&msg); |
251 | spi_message_add_tail(&xfer_head1, &msg); |
252 | spi_message_add_tail(&xfer_buf, &msg); |
253 | |
254 | status = spi_sync(lp->spi, &msg); |
255 | |
256 | if (msg.status) |
257 | status = msg.status; |
258 | dev_vdbg(&lp->spi->dev, "status = %d\n", status); |
259 | dev_vdbg(&lp->spi->dev, "buf[0] = %02x\n", buf[0]); |
260 | dev_vdbg(&lp->spi->dev, "buf[1] = %02x\n", buf[1]); |
261 | |
262 | if (!status) { |
263 | if (lqi && *len > lp->buf[1]) |
264 | *lqi = data[lp->buf[1]]; |
265 | } |
266 | |
267 | fail: |
268 | mutex_unlock(&lp->bmux); |
269 | |
270 | return status; |
271 | } |
272 | |
273 | static int |
274 | at86rf230_ed(struct ieee802154_dev *dev, u8 *level) |
275 | { |
276 | pr_debug("%s\n", __func__); |
277 | might_sleep(); |
278 | BUG_ON(!level); |
279 | *level = 0xbe; |
280 | return 0; |
281 | } |
282 | |
283 | static int |
284 | at86rf230_state(struct ieee802154_dev *dev, int state) |
285 | { |
286 | struct at86rf230_local *lp = dev->priv; |
287 | int rc; |
288 | u8 val; |
289 | u8 desired_status; |
290 | |
291 | pr_debug("%s %d\n", __func__/*, priv->cur_state*/, state); |
292 | might_sleep(); |
293 | |
294 | if (state == STATE_FORCE_TX_ON) |
295 | desired_status = STATE_TX_ON; |
296 | else if (state == STATE_FORCE_TRX_OFF) |
297 | desired_status = STATE_TRX_OFF; |
298 | else |
299 | desired_status = state; |
300 | |
301 | do { |
302 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val); |
303 | if (rc) |
304 | goto err; |
305 | pr_debug("%s val1 = %x\n", __func__, val); |
306 | } while (val == STATE_TRANSITION_IN_PROGRESS); |
307 | |
308 | if (val == desired_status) |
309 | return 0; |
310 | |
311 | /* state is equal to phy states */ |
312 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, state); |
313 | if (rc) |
314 | goto err; |
315 | |
316 | do { |
317 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &val); |
318 | if (rc) |
319 | goto err; |
320 | pr_debug("%s val2 = %x\n", __func__, val); |
321 | } while (val == STATE_TRANSITION_IN_PROGRESS); |
322 | |
323 | |
324 | if (val == desired_status) |
325 | return 0; |
326 | #ifdef ENABLE_AACK |
327 | if (state == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK) |
328 | #else |
329 | if (state == STATE_RX_ON && val == STATE_BUSY_RX) |
330 | #endif |
331 | return 0; |
332 | |
333 | pr_err("%s unexpected state change: %d, asked for %d\n", __func__, |
334 | val, state); |
335 | return -EBUSY; |
336 | |
337 | err: |
338 | pr_err("%s error: %d\n", __func__, rc); |
339 | return rc; |
340 | } |
341 | |
342 | static int |
343 | at86rf230_start(struct ieee802154_dev *dev) |
344 | { |
345 | struct at86rf230_local *lp = dev->priv; |
346 | u8 rc; |
347 | |
348 | rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1); |
349 | if (rc) |
350 | return rc; |
351 | #ifdef ENABLE_AACK |
352 | return at86rf230_state(dev, STATE_RX_AACK_ON); |
353 | #else |
354 | return at86rf230_state(dev, STATE_RX_ON); |
355 | #endif |
356 | } |
357 | |
358 | static void |
359 | at86rf230_stop(struct ieee802154_dev *dev) |
360 | { |
361 | at86rf230_state(dev, STATE_FORCE_TRX_OFF); |
362 | } |
363 | |
364 | static int |
365 | at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) |
366 | { |
367 | struct at86rf230_local *lp = dev->priv; |
368 | int rc; |
369 | |
370 | pr_debug("%s %d\n", __func__, channel); |
371 | might_sleep(); |
372 | |
373 | BUG_ON(page != 0); |
374 | BUG_ON(channel < 11); |
375 | BUG_ON(channel > 26); |
376 | |
377 | rc = at86rf230_write_subreg(lp, SR_CHANNEL, channel); |
378 | msleep(1); /* Wait for PLL */ |
379 | dev->phy->current_channel = channel; |
380 | |
381 | return 0; |
382 | } |
383 | |
384 | /* FIXME: |
385 | * This function currently is a mess. It uses flush_work to guard |
386 | * against concurrent irqwork, etc. One has to use mutexes intead. */ |
387 | static int |
388 | at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb) |
389 | { |
390 | struct at86rf230_local *lp = dev->priv; |
391 | int rc; |
392 | |
393 | pr_debug("%s\n", __func__); |
394 | |
395 | might_sleep(); |
396 | |
397 | BUG_ON(lp->is_tx); |
398 | INIT_COMPLETION(lp->tx_complete); |
399 | |
400 | rc = at86rf230_state(dev, STATE_FORCE_TX_ON); |
401 | if (rc) |
402 | goto err; |
403 | |
404 | synchronize_irq(lp->spi->irq); |
405 | flush_work(&lp->irqwork); |
406 | |
407 | lp->is_tx = 1; |
408 | |
409 | rc = at86rf230_write_fbuf(lp, skb->data, skb->len); |
410 | if (rc) |
411 | goto err_rx; |
412 | |
413 | if (gpio_is_valid(lp->slp_tr)) { |
414 | gpio_set_value(lp->slp_tr, 1); |
415 | udelay(80); /* > 62.5 */ |
416 | gpio_set_value(lp->slp_tr, 0); |
417 | } else { |
418 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX); |
419 | if (rc) |
420 | goto err_rx; |
421 | } |
422 | |
423 | /* FIXME: the logic is really strange here. Datasheet doesn't |
424 | * provide us enough info about behaviour in such cases. |
425 | * Basically either we were interrupted here, or we have lost |
426 | * the interrupt. Most probably this should be changed to |
427 | * wait_for_completion_timeout() and handle it's results |
428 | */ |
429 | rc = wait_for_completion_interruptible(&lp->tx_complete); |
430 | if (rc < 0) |
431 | goto err_state; |
432 | |
433 | lp->is_tx = 0; |
434 | |
435 | rc = at86rf230_start(dev); |
436 | return rc; |
437 | |
438 | err_state: |
439 | /* try to recover from possibly problematic state */ |
440 | at86rf230_state(dev, STATE_FORCE_TX_ON); |
441 | synchronize_irq(lp->spi->irq); |
442 | flush_work(&lp->irqwork); |
443 | lp->is_tx = 0; |
444 | err_rx: |
445 | at86rf230_start(dev); |
446 | err: |
447 | if (rc) |
448 | pr_err("%s error: %d\n", __func__, rc); |
449 | |
450 | return rc; |
451 | } |
452 | |
453 | static int at86rf230_rx(struct at86rf230_local *lp) |
454 | { |
455 | u8 len = 128, lqi = 0; |
456 | int rc, rc2; |
457 | struct sk_buff *skb; |
458 | |
459 | skb = alloc_skb(len, GFP_KERNEL); |
460 | if (!skb) |
461 | return -ENOMEM; |
462 | |
463 | /* FIXME: process return status */ |
464 | rc = at86rf230_write_subreg(lp, SR_RX_PDT_DIS, 1); |
465 | rc2 = at86rf230_read_fbuf(lp, skb_put(skb, len), &len, &lqi); |
466 | rc = at86rf230_write_subreg(lp, SR_RX_SAFE_MODE, 1); |
467 | rc = at86rf230_write_subreg(lp, SR_RX_PDT_DIS, 0); |
468 | if (rc2 < 0) |
469 | goto err_fbuf; |
470 | |
471 | if (len < 2) |
472 | goto err; |
473 | |
474 | skb_trim(skb, len-2); /* We do not put CRC into the frame */ |
475 | |
476 | |
477 | ieee802154_rx_irqsafe(lp->dev, skb, lqi); |
478 | |
479 | dev_dbg(&lp->spi->dev, "READ_FBUF: %d %d %x\n", rc, len, lqi); |
480 | |
481 | return 0; |
482 | err: |
483 | pr_debug("%s: received frame is too small\n", __func__); |
484 | |
485 | err_fbuf: |
486 | kfree_skb(skb); |
487 | return -EINVAL; |
488 | } |
489 | |
490 | #ifdef ENABLE_AACK |
491 | static int |
492 | at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev, |
493 | struct ieee802154_hw_addr_filt *filt, |
494 | unsigned long changed) |
495 | { |
496 | struct at86rf230_local *lp = dev->priv; |
497 | |
498 | might_sleep(); |
499 | |
500 | at86rf230_stop(dev); |
501 | |
502 | msleep(10); |
503 | |
504 | if (changed & IEEE802515_SADDR_CHANGED) { |
505 | dev_info(&lp->spi->dev, "at86rf230_set_hw_addr_filt called for saddr\n"); |
506 | __at86rf230_write(lp, RG_SHORT_ADDR_0, filt->short_addr & 0xff); /* LSB */ |
507 | __at86rf230_write(lp, RG_SHORT_ADDR_1, (filt->short_addr >> 8) & 0xff); /* MSB */ |
508 | } |
509 | |
510 | if (changed & IEEE802515_PANID_CHANGED) { |
511 | dev_info(&lp->spi->dev, "at86rf230_set_hw_addr_filt called for pan id\n"); |
512 | __at86rf230_write(lp, RG_PAN_ID_0, filt->pan_id & 0xff); /* LSB */ |
513 | __at86rf230_write(lp, RG_PAN_ID_1, (filt->pan_id >> 8) & 0xff); /* MSB */ |
514 | } |
515 | |
516 | if (changed & IEEE802515_IEEEADDR_CHANGED) { |
517 | dev_info(&lp->spi->dev, "at86rf230_set_hw_addr_filt called ieee addr\n"); |
518 | // Make sure order MSB to LSB is correct |
519 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_0, filt->ieee_addr[7]); |
520 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_1, filt->ieee_addr[6]); |
521 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_2, filt->ieee_addr[5]); |
522 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_3, filt->ieee_addr[4]); |
523 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_4, filt->ieee_addr[3]); |
524 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_5, filt->ieee_addr[2]); |
525 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_6, filt->ieee_addr[1]); |
526 | at86rf230_write_subreg(lp, SR_IEEE_ADDR_7, filt->ieee_addr[0]); |
527 | } |
528 | |
529 | if (changed & IEEE802515_PANC_CHANGED) { |
530 | dev_info(&lp->spi->dev, "at86rf230_set_hw_addr_filt called panc change\n"); |
531 | if (filt->pan_coord) |
532 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 1); |
533 | else |
534 | at86rf230_write_subreg(lp, SR_AACK_I_AM_COORD, 0); |
535 | } |
536 | |
537 | at86rf230_start(dev); |
538 | |
539 | return 0; |
540 | } |
541 | #endif |
542 | static struct ieee802154_ops at86rf230_ops = { |
543 | .owner = THIS_MODULE, |
544 | .xmit = at86rf230_xmit, |
545 | .ed = at86rf230_ed, |
546 | .set_channel = at86rf230_channel, |
547 | .start = at86rf230_start, |
548 | .stop = at86rf230_stop, |
549 | #ifdef ENABLE_AACK |
550 | .set_hw_addr_filt = at86rf230_set_hw_addr_filt, |
551 | #endif |
552 | }; |
553 | |
554 | static void at86rf230_irqwork(struct work_struct *work) |
555 | { |
556 | struct at86rf230_local *lp = |
557 | container_of(work, struct at86rf230_local, irqwork); |
558 | u8 status = 0, val; |
559 | int rc; |
560 | |
561 | dev_dbg(&lp->spi->dev, "IRQ Worker\n"); |
562 | |
563 | do { |
564 | rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &val); |
565 | status |= val; |
566 | dev_dbg(&lp->spi->dev, "IRQ Status: %02x\n", status); |
567 | |
568 | status &= ~IRQ_PLL_LOCK; /* ignore */ |
569 | status &= ~IRQ_RX_START; /* ignore */ |
570 | status &= ~IRQ_AMI; /* ignore */ |
571 | status &= ~IRQ_TRX_UR; /* FIXME: possibly handle ???*/ |
572 | |
573 | if (status & IRQ_TRX_END) { |
574 | status &= ~IRQ_TRX_END; |
575 | if (lp->is_tx) |
576 | complete(&lp->tx_complete); |
577 | else |
578 | at86rf230_rx(lp); |
579 | } |
580 | |
581 | } while (status != 0); |
582 | |
583 | enable_irq(lp->spi->irq); |
584 | } |
585 | |
586 | static irqreturn_t at86rf230_isr(int irq, void *data) |
587 | { |
588 | struct at86rf230_local *lp = data; |
589 | |
590 | dev_dbg(&lp->spi->dev, "IRQ!\n"); |
591 | |
592 | disable_irq_nosync(irq); |
593 | schedule_work(&lp->irqwork); |
594 | |
595 | return IRQ_HANDLED; |
596 | } |
597 | |
598 | static int at86rf230_hw_init(struct at86rf230_local *lp) |
599 | { |
600 | u8 status; |
601 | int rc; |
602 | |
603 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); |
604 | if (rc) |
605 | return rc; |
606 | |
607 | dev_info(&lp->spi->dev, "Status: %02x\n", status); |
608 | if (status == STATE_P_ON) { |
609 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TRX_OFF); |
610 | if (rc) |
611 | return rc; |
612 | msleep(1); |
613 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); |
614 | if (rc) |
615 | return rc; |
616 | dev_info(&lp->spi->dev, "Status: %02x\n", status); |
617 | } |
618 | |
619 | rc = at86rf230_write_subreg(lp, SR_IRQ_MASK, |
620 | /*IRQ_TRX_UR | IRQ_CCA_ED | IRQ_TRX_END | IRQ_PLL_UNL | IRQ_PLL_LOCK*/ 0xff); |
621 | if (rc) |
622 | return rc; |
623 | |
624 | /* CLKM changes are applied immediately */ |
625 | rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00); |
626 | if (rc) |
627 | return rc; |
628 | |
629 | /* Turn CLKM Off */ |
630 | rc = at86rf230_write_subreg(lp, SR_CLKM_CTRL, 0x00); |
631 | if (rc) |
632 | return rc; |
633 | |
634 | msleep(100); |
635 | |
636 | rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ON); |
637 | if (rc) |
638 | return rc; |
639 | msleep(1); |
640 | |
641 | rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); |
642 | if (rc) |
643 | return rc; |
644 | dev_info(&lp->spi->dev, "Status: %02x\n", status); |
645 | |
646 | rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &status); |
647 | if (rc) |
648 | return rc; |
649 | if (!status) { |
650 | dev_err(&lp->spi->dev, "DVDD error\n"); |
651 | return -EINVAL; |
652 | } |
653 | |
654 | rc = at86rf230_read_subreg(lp, SR_AVDD_OK, &status); |
655 | if (rc) |
656 | return rc; |
657 | if (!status) { |
658 | dev_err(&lp->spi->dev, "AVDD error\n"); |
659 | return -EINVAL; |
660 | } |
661 | |
662 | return 0; |
663 | } |
664 | |
665 | static int at86rf230_suspend(struct spi_device *spi, pm_message_t message) |
666 | { |
667 | return 0; |
668 | } |
669 | |
670 | static int at86rf230_resume(struct spi_device *spi) |
671 | { |
672 | return 0; |
673 | } |
674 | |
675 | #ifdef CONFIG_OF |
676 | static int at86rf230_fill_data(struct spi_device *spi) |
677 | { |
678 | struct device_node *np = spi->dev.of_node; |
679 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
680 | struct at86rf230_platform_data *pdata = spi->dev.platform_data; |
681 | enum of_gpio_flags gpio_flags; |
682 | |
683 | if (pdata) { |
684 | lp->rstn = pdata->rstn; |
685 | lp->slp_tr = pdata->slp_tr; |
686 | lp->dig2 = pdata->dig2; |
687 | lp->reset = pdata->reset; |
688 | lp->reset_data = pdata->reset_data; |
689 | |
690 | return 0; |
691 | } |
692 | |
693 | if (!np) { |
694 | dev_err(&spi->dev, "no platform_data and no node data\n"); |
695 | return -EINVAL; |
696 | } |
697 | |
698 | lp->rstn = of_get_gpio_flags(np, 0, &gpio_flags); |
699 | if (!gpio_is_valid(lp->rstn)) { |
700 | dev_err(&spi->dev, "no RSTN GPIO!\n"); |
701 | return -EINVAL; |
702 | } |
703 | |
704 | lp->slp_tr = of_get_gpio_flags(np, 1, &gpio_flags); |
705 | lp->dig2 = of_get_gpio_flags(np, 2, &gpio_flags); |
706 | |
707 | lp->reset = NULL; |
708 | |
709 | return 0; |
710 | } |
711 | #else |
712 | static int at86rf230_fill_data(struct spi_device *spi) |
713 | { |
714 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
715 | struct at86rf230_platform_data *pdata = spi->dev.platform_data; |
716 | |
717 | if (!pdata) { |
718 | dev_err(&spi->dev, "no platform_data\n"); |
719 | return -EINVAL; |
720 | } |
721 | |
722 | lp->rstn = pdata->rstn; |
723 | lp->slp_tr = pdata->slp_tr; |
724 | lp->dig2 = pdata->dig2; |
725 | lp->reset = pdata->reset; |
726 | lp->reset_data = pdata->reset_data; |
727 | |
728 | return 0; |
729 | } |
730 | #endif |
731 | |
732 | static int __devinit at86rf230_probe(struct spi_device *spi) |
733 | { |
734 | struct ieee802154_dev *dev; |
735 | struct at86rf230_local *lp; |
736 | u8 man_id_0, man_id_1; |
737 | int rc; |
738 | const char *chip; |
739 | int supported = 0; |
740 | |
741 | if (spi->irq < 0) { |
742 | dev_err(&spi->dev, "no IRQ specified\n"); |
743 | return -EINVAL; |
744 | } |
745 | |
746 | dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops); |
747 | if (!dev) |
748 | return -ENOMEM; |
749 | |
750 | lp = dev->priv; |
751 | lp->dev = dev; |
752 | |
753 | lp->spi = spi; |
754 | |
755 | dev->priv = lp; |
756 | dev->parent = &spi->dev; |
757 | dev->extra_tx_headroom = 0; |
758 | /* We do support only 2.4 Ghz */ |
759 | dev->phy->channels_supported[0] = 0x7FFF800; |
760 | #ifdef ENABLE_AACK |
761 | dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK; |
762 | #else |
763 | dev->flags = IEEE802154_HW_OMIT_CKSUM; |
764 | #endif |
765 | |
766 | mutex_init(&lp->bmux); |
767 | INIT_WORK(&lp->irqwork, at86rf230_irqwork); |
768 | init_completion(&lp->tx_complete); |
769 | |
770 | spi_set_drvdata(spi, lp); |
771 | |
772 | rc = at86rf230_fill_data(spi); |
773 | if (rc) |
774 | goto err_fill; |
775 | |
776 | if (gpio_is_valid(lp->rstn)) { |
777 | rc = gpio_request(lp->rstn, "rstn"); |
778 | if (rc) |
779 | goto err_rstn; |
780 | } |
781 | |
782 | if (gpio_is_valid(lp->slp_tr)) { |
783 | rc = gpio_request(lp->slp_tr, "slp_tr"); |
784 | if (rc) |
785 | goto err_slp_tr; |
786 | } |
787 | |
788 | if (gpio_is_valid(lp->rstn)) { |
789 | rc = gpio_direction_output(lp->rstn, 1); |
790 | if (rc) |
791 | goto err_gpio_dir; |
792 | } |
793 | |
794 | if (gpio_is_valid(lp->slp_tr)) { |
795 | rc = gpio_direction_output(lp->slp_tr, 0); |
796 | if (rc) |
797 | goto err_gpio_dir; |
798 | } |
799 | |
800 | /* Reset */ |
801 | if (lp->reset) |
802 | lp->reset(lp->reset_data); |
803 | else { |
804 | msleep(1); |
805 | gpio_set_value(lp->rstn, 0); |
806 | msleep(1); |
807 | gpio_set_value(lp->rstn, 1); |
808 | msleep(1); |
809 | } |
810 | |
811 | rc = at86rf230_read_subreg(lp, SR_MAN_ID_0, &man_id_0); |
812 | if (rc) |
813 | goto err_gpio_dir; |
814 | rc = at86rf230_read_subreg(lp, SR_MAN_ID_1, &man_id_1); |
815 | if (rc) |
816 | goto err_gpio_dir; |
817 | |
818 | if (man_id_1 != 0x00 || man_id_0 != 0x1f) { |
819 | dev_err(&spi->dev, "Non-Atmel device found (MAN_ID" |
820 | "%02x %02x)\n", man_id_1, man_id_0); |
821 | rc = -EINVAL; |
822 | goto err_gpio_dir; |
823 | } |
824 | |
825 | rc = at86rf230_read_subreg(lp, SR_PART_NUM, &lp->part); |
826 | if (rc) |
827 | goto err_gpio_dir; |
828 | |
829 | rc = at86rf230_read_subreg(lp, SR_VERSION_NUM, &lp->vers); |
830 | if (rc) |
831 | goto err_gpio_dir; |
832 | |
833 | switch (lp->part) { |
834 | case 2: |
835 | chip = "at86rf230"; |
836 | /* supported = 1; FIXME: should be easy to support; */ |
837 | break; |
838 | case 3: |
839 | chip = "at86rf231"; |
840 | supported = 1; |
841 | break; |
842 | default: |
843 | chip = "UNKNOWN"; |
844 | break; |
845 | } |
846 | |
847 | dev_info(&spi->dev, "Detected %s chip version %d\n", chip, lp->vers); |
848 | if (!supported) { |
849 | rc = -ENOTSUPP; |
850 | goto err_gpio_dir; |
851 | } |
852 | |
853 | rc = at86rf230_hw_init(lp); |
854 | if (rc) |
855 | goto err_gpio_dir; |
856 | |
857 | rc = request_irq(spi->irq, at86rf230_isr, IRQF_SHARED, |
858 | dev_name(&spi->dev), lp); |
859 | if (rc) |
860 | goto err_gpio_dir; |
861 | |
862 | dev_dbg(&spi->dev, "registered at86rf230\n"); |
863 | |
864 | rc = ieee802154_register_device(lp->dev); |
865 | if (rc) |
866 | goto err_irq; |
867 | |
868 | return rc; |
869 | |
870 | err_irq: |
871 | disable_irq(spi->irq); |
872 | flush_work(&lp->irqwork); |
873 | free_irq(spi->irq, lp); |
874 | err_gpio_dir: |
875 | if (gpio_is_valid(lp->slp_tr)) |
876 | gpio_free(lp->slp_tr); |
877 | err_slp_tr: |
878 | if (gpio_is_valid(lp->rstn)) |
879 | gpio_free(lp->rstn); |
880 | err_rstn: |
881 | err_fill: |
882 | spi_set_drvdata(spi, NULL); |
883 | mutex_destroy(&lp->bmux); |
884 | ieee802154_free_device(lp->dev); |
885 | return rc; |
886 | } |
887 | |
888 | static int __devexit at86rf230_remove(struct spi_device *spi) |
889 | { |
890 | struct at86rf230_local *lp = spi_get_drvdata(spi); |
891 | |
892 | /* |
893 | * @@@ this looks wrong - what if a frame arrives before |
894 | * disable_irq ? -- wa |
895 | */ |
896 | ieee802154_unregister_device(lp->dev); |
897 | |
898 | disable_irq(spi->irq); |
899 | flush_work(&lp->irqwork); |
900 | free_irq(spi->irq, lp); |
901 | |
902 | if (gpio_is_valid(lp->slp_tr)) |
903 | gpio_free(lp->slp_tr); |
904 | if (gpio_is_valid(lp->rstn)) |
905 | gpio_free(lp->rstn); |
906 | |
907 | spi_set_drvdata(spi, NULL); |
908 | mutex_destroy(&lp->bmux); |
909 | ieee802154_free_device(lp->dev); |
910 | |
911 | dev_dbg(&spi->dev, "unregistered at86rf230\n"); |
912 | return 0; |
913 | } |
914 | |
915 | static struct spi_driver at86rf230_driver = { |
916 | .driver = { |
917 | .name = "at86rf230", |
918 | .owner = THIS_MODULE, |
919 | }, |
920 | .probe = at86rf230_probe, |
921 | .remove = __devexit_p(at86rf230_remove), |
922 | .suspend = at86rf230_suspend, |
923 | .resume = at86rf230_resume, |
924 | }; |
925 | |
926 | static int __init at86rf230_init(void) |
927 | { |
928 | return spi_register_driver(&at86rf230_driver); |
929 | } |
930 | module_init(at86rf230_init); |
931 | |
932 | static void __exit at86rf230_exit(void) |
933 | { |
934 | spi_unregister_driver(&at86rf230_driver); |
935 | } |
936 | module_exit(at86rf230_exit); |
937 | |
938 | MODULE_DESCRIPTION("AT86RF230 Transceiver Driver"); |
939 | MODULE_LICENSE("GPL v2"); |
940 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9