Root/drivers/ieee802154/at86rf230.c

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

Archive Download this file



interactive