~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/iio/adc/ad7745.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 
9
9
#include <linux/interrupt.h>
10
10
#include <linux/gpio.h>
11
 
#include <linux/workqueue.h>
12
11
#include <linux/device.h>
13
12
#include <linux/kernel.h>
14
13
#include <linux/slab.h>
15
14
#include <linux/sysfs.h>
16
15
#include <linux/list.h>
17
16
#include <linux/i2c.h>
18
 
#include <linux/rtc.h>
19
17
 
20
18
#include "../iio.h"
21
19
#include "../sysfs.h"
55
53
 */
56
54
 
57
55
struct ad774x_chip_info {
58
 
        const char *name;
59
56
        struct i2c_client *client;
60
57
        struct iio_dev *indio_dev;
61
 
        struct work_struct thresh_work;
62
58
        bool inter;
63
 
        s64 last_timestamp;
64
59
        u16 cap_offs;                   /* Capacitive offset */
65
60
        u16 cap_gain;                   /* Capacitive gain calibration */
66
61
        u16 volt_gain;                  /* Voltage gain calibration */
76
71
        u8 reg_cfg;
77
72
};
78
73
 
79
 
struct ad774x_conversion_mode ad774x_conv_mode_table[AD774X_MAX_CONV_MODE] = {
 
74
static struct ad774x_conversion_mode
 
75
ad774x_conv_mode_table[AD774X_MAX_CONV_MODE] = {
80
76
        { "idle", 0 },
81
77
        { "continuous-conversion", 1 },
82
78
        { "single-conversion", 2 },
502
498
                ad774x_show_cap_gain,
503
499
                ad774x_store_cap_gain);
504
500
 
505
 
static ssize_t ad774x_show_name(struct device *dev,
506
 
                struct device_attribute *attr,
507
 
                char *buf)
508
 
{
509
 
        struct iio_dev *dev_info = dev_get_drvdata(dev);
510
 
        struct ad774x_chip_info *chip = dev_info->dev_data;
511
 
        return sprintf(buf, "%s\n", chip->name);
512
 
}
513
 
 
514
 
static IIO_DEVICE_ATTR(name, S_IRUGO, ad774x_show_name, NULL, 0);
515
 
 
516
501
static struct attribute *ad774x_attributes[] = {
517
502
        &iio_dev_attr_available_conversion_modes.dev_attr.attr,
518
503
        &iio_dev_attr_conversion_mode.dev_attr.attr,
526
511
        &iio_dev_attr_cap0_raw.dev_attr.attr,
527
512
        &iio_dev_attr_capdac0_raw.dev_attr.attr,
528
513
        &iio_dev_attr_capdac1_raw.dev_attr.attr,
529
 
        &iio_dev_attr_name.dev_attr.attr,
530
514
        NULL,
531
515
};
532
516
 
538
522
 * data ready events
539
523
 */
540
524
 
541
 
#define IIO_EVENT_CODE_CAP_RDY     IIO_BUFFER_EVENT_CODE(0)
542
 
#define IIO_EVENT_CODE_VT_RDY      IIO_BUFFER_EVENT_CODE(1)
 
525
#define IIO_EVENT_CODE_CAP_RDY     0
 
526
#define IIO_EVENT_CODE_VT_RDY      1
543
527
 
544
528
#define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask)        \
545
529
        IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask)
547
531
#define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \
548
532
        IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask)
549
533
 
550
 
static void ad774x_interrupt_handler_bh(struct work_struct *work_s)
 
534
static irqreturn_t ad774x_event_handler(int irq, void *private)
551
535
{
552
 
        struct ad774x_chip_info *chip =
553
 
                container_of(work_s, struct ad774x_chip_info, thresh_work);
 
536
        struct iio_dev *indio_dev = private;
 
537
        struct ad774x_chip_info *chip = iio_dev_get_devdata(indio_dev);
554
538
        u8 int_status;
555
539
 
556
 
        enable_irq(chip->client->irq);
557
 
 
558
540
        ad774x_i2c_read(chip, AD774X_STATUS, &int_status, 1);
559
541
 
560
542
        if (int_status & AD774X_STATUS_RDYCAP)
561
 
                iio_push_event(chip->indio_dev, 0,
562
 
                                IIO_EVENT_CODE_CAP_RDY,
563
 
                                chip->last_timestamp);
 
543
                iio_push_event(indio_dev, 0,
 
544
                               IIO_EVENT_CODE_CAP_RDY,
 
545
                               iio_get_time_ns());
564
546
 
565
547
        if (int_status & AD774X_STATUS_RDYVT)
566
 
                iio_push_event(chip->indio_dev, 0,
567
 
                                IIO_EVENT_CODE_VT_RDY,
568
 
                                chip->last_timestamp);
569
 
}
570
 
 
571
 
static int ad774x_interrupt_handler_th(struct iio_dev *dev_info,
572
 
                int index,
573
 
                s64 timestamp,
574
 
                int no_test)
575
 
{
576
 
        struct ad774x_chip_info *chip = dev_info->dev_data;
577
 
 
578
 
        chip->last_timestamp = timestamp;
579
 
        schedule_work(&chip->thresh_work);
580
 
 
581
 
        return 0;
582
 
}
583
 
 
584
 
IIO_EVENT_SH(data_rdy, &ad774x_interrupt_handler_th);
585
 
 
586
 
static ssize_t ad774x_query_out_mode(struct device *dev,
587
 
                struct device_attribute *attr,
588
 
                char *buf)
589
 
{
590
 
        /*
591
 
         * AD774X provides one /RDY pin, which can be used as interrupt
592
 
         * but the pin is not configurable
593
 
         */
594
 
        return sprintf(buf, "1\n");
595
 
}
596
 
 
597
 
static ssize_t ad774x_set_out_mode(struct device *dev,
598
 
                struct device_attribute *attr,
599
 
                const char *buf,
600
 
                size_t len)
601
 
{
602
 
        return len;
603
 
}
604
 
 
605
 
IIO_EVENT_ATTR_CAP_RDY_SH(iio_event_data_rdy, ad774x_query_out_mode, ad774x_set_out_mode, 0);
606
 
IIO_EVENT_ATTR_VT_RDY_SH(iio_event_data_rdy, ad774x_query_out_mode, ad774x_set_out_mode, 0);
 
548
                iio_push_event(indio_dev, 0,
 
549
                               IIO_EVENT_CODE_VT_RDY,
 
550
                               iio_get_time_ns());
 
551
 
 
552
        return IRQ_HANDLED;
 
553
}
 
554
 
 
555
static IIO_CONST_ATTR(cap_rdy_en, "1");
 
556
static IIO_CONST_ATTR(vt_rdy_en, "1");
607
557
 
608
558
static struct attribute *ad774x_event_attributes[] = {
609
 
        &iio_event_attr_cap_rdy.dev_attr.attr,
610
 
        &iio_event_attr_vt_rdy.dev_attr.attr,
 
559
        &iio_const_attr_cap_rdy_en.dev_attr.attr,
 
560
        &iio_const_attr_vt_rdy_en.dev_attr.attr,
611
561
        NULL,
612
562
};
613
563
 
615
565
        .attrs = ad774x_event_attributes,
616
566
};
617
567
 
 
568
static const struct iio_info ad774x_info = {
 
569
        .attrs = &ad774x_event_attribute_group,
 
570
        .event_attrs = &ad774x_event_attribute_group,
 
571
        .num_interrupt_lines = 1,
 
572
        .driver_module = THIS_MODULE,
 
573
};
618
574
/*
619
575
 * device probe and remove
620
576
 */
633
589
        i2c_set_clientdata(client, chip);
634
590
 
635
591
        chip->client = client;
636
 
        chip->name = id->name;
637
592
 
638
 
        chip->indio_dev = iio_allocate_device();
 
593
        chip->indio_dev = iio_allocate_device(0);
639
594
        if (chip->indio_dev == NULL) {
640
595
                ret = -ENOMEM;
641
596
                goto error_free_chip;
642
597
        }
643
598
 
644
599
        /* Establish that the iio_dev is a child of the i2c device */
 
600
        chip->indio_dev->name = id->name;
645
601
        chip->indio_dev->dev.parent = &client->dev;
646
 
        chip->indio_dev->attrs = &ad774x_attribute_group;
647
 
        chip->indio_dev->event_attrs = &ad774x_event_attribute_group;
 
602
        chip->indio_dev->info = &ad774x_info;
648
603
        chip->indio_dev->dev_data = (void *)(chip);
649
 
        chip->indio_dev->driver_module = THIS_MODULE;
650
 
        chip->indio_dev->num_interrupt_lines = 1;
651
604
        chip->indio_dev->modes = INDIO_DIRECT_MODE;
652
605
 
653
606
        ret = iio_device_register(chip->indio_dev);
656
609
        regdone = 1;
657
610
 
658
611
        if (client->irq) {
659
 
                ret = iio_register_interrupt_line(client->irq,
660
 
                                chip->indio_dev,
661
 
                                0,
662
 
                                IRQF_TRIGGER_FALLING,
663
 
                                "ad774x");
 
612
                ret = request_threaded_irq(client->irq,
 
613
                                           NULL,
 
614
                                           &ad774x_event_handler,
 
615
                                           IRQF_TRIGGER_FALLING,
 
616
                                           "ad774x",
 
617
                                           chip->indio_dev);
664
618
                if (ret)
665
619
                        goto error_free_dev;
666
 
 
667
 
                iio_add_event_to_list(iio_event_attr_cap_rdy.listel,
668
 
                                &chip->indio_dev->interrupts[0]->ev_list);
669
 
 
670
 
                INIT_WORK(&chip->thresh_work, ad774x_interrupt_handler_bh);
671
620
        }
672
621
 
673
622
        dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
676
625
 
677
626
error_free_dev:
678
627
        if (regdone)
679
 
                iio_device_unregister(chip->indio_dev);
 
628
                free_irq(client->irq, chip->indio_dev);
680
629
        else
681
630
                iio_free_device(chip->indio_dev);
682
631
error_free_chip:
691
640
        struct iio_dev *indio_dev = chip->indio_dev;
692
641
 
693
642
        if (client->irq)
694
 
                iio_unregister_interrupt_line(indio_dev, 0);
 
643
                free_irq(client->irq, indio_dev);
695
644
        iio_device_unregister(indio_dev);
696
645
        kfree(chip);
697
646