505
476
return (u32) scaled;
508
static ssize_t tsl2563_lux_show(struct device *dev,
509
struct device_attribute *attr, char *buf)
511
struct iio_dev *indio_dev = dev_get_drvdata(dev);
512
struct tsl2563_chip *chip = indio_dev->dev_data;
479
static int tsl2563_write_raw(struct iio_dev *indio_dev,
480
struct iio_chan_spec const *chan,
485
struct tsl2563_chip *chip = iio_priv(indio_dev);
487
if (chan->channel == 0)
488
chip->calib0 = calib_from_sysfs(val);
490
chip->calib1 = calib_from_sysfs(val);
495
static int tsl2563_read_raw(struct iio_dev *indio_dev,
496
struct iio_chan_spec const *chan,
513
502
u32 calib0, calib1;
516
mutex_lock(&chip->lock);
518
ret = tsl2563_get_adc(chip);
522
calib0 = calib_adc(chip->data0, chip->calib0) * chip->cover_comp_gain;
523
calib1 = calib_adc(chip->data1, chip->calib1) * chip->cover_comp_gain;
525
ret = snprintf(buf, PAGE_SIZE, "%d\n", adc_to_lux(calib0, calib1));
528
mutex_unlock(&chip->lock);
532
static ssize_t format_calib(char *buf, int len, u32 calib)
534
return snprintf(buf, PAGE_SIZE, "%d\n", calib_to_sysfs(calib));
537
static ssize_t tsl2563_calib_show(struct device *dev,
538
struct device_attribute *attr, char *buf)
540
struct iio_dev *indio_dev = dev_get_drvdata(dev);
541
struct tsl2563_chip *chip = indio_dev->dev_data;
542
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
545
mutex_lock(&chip->lock);
546
switch (this_attr->address) {
548
ret = format_calib(buf, PAGE_SIZE, chip->calib0);
551
ret = format_calib(buf, PAGE_SIZE, chip->calib1);
556
mutex_unlock(&chip->lock);
560
static ssize_t tsl2563_calib_store(struct device *dev,
561
struct device_attribute *attr,
562
const char *buf, size_t len)
564
struct iio_dev *indio_dev = dev_get_drvdata(dev);
565
struct tsl2563_chip *chip = indio_dev->dev_data;
566
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
570
if (1 != sscanf(buf, "%d", &value))
573
calib = calib_from_sysfs(value);
575
switch (this_attr->address) {
577
chip->calib0 = calib;
580
chip->calib1 = calib;
587
static IIO_DEVICE_ATTR(intensity0_both_raw, S_IRUGO,
588
tsl2563_adc_show, NULL, 0);
589
static IIO_DEVICE_ATTR(intensity1_ir_raw, S_IRUGO,
590
tsl2563_adc_show, NULL, 1);
591
static DEVICE_ATTR(illuminance0_input, S_IRUGO, tsl2563_lux_show, NULL);
592
static IIO_DEVICE_ATTR(intensity0_both_calibgain, S_IRUGO | S_IWUSR,
593
tsl2563_calib_show, tsl2563_calib_store, 0);
594
static IIO_DEVICE_ATTR(intensity1_ir_calibgain, S_IRUGO | S_IWUSR,
595
tsl2563_calib_show, tsl2563_calib_store, 1);
597
static ssize_t tsl2563_show_name(struct device *dev,
598
struct device_attribute *attr,
601
struct iio_dev *indio_dev = dev_get_drvdata(dev);
602
struct tsl2563_chip *chip = indio_dev->dev_data;
603
return sprintf(buf, "%s\n", chip->client->name);
606
static DEVICE_ATTR(name, S_IRUGO, tsl2563_show_name, NULL);
608
static struct attribute *tsl2563_attributes[] = {
609
&iio_dev_attr_intensity0_both_raw.dev_attr.attr,
610
&iio_dev_attr_intensity1_ir_raw.dev_attr.attr,
611
&dev_attr_illuminance0_input.attr,
612
&iio_dev_attr_intensity0_both_calibgain.dev_attr.attr,
613
&iio_dev_attr_intensity1_ir_calibgain.dev_attr.attr,
618
static const struct attribute_group tsl2563_group = {
619
.attrs = tsl2563_attributes,
622
static ssize_t tsl2563_read_thresh(struct device *dev,
623
struct device_attribute *attr,
626
struct iio_dev *indio_dev = dev_get_drvdata(dev);
627
struct tsl2563_chip *chip = indio_dev->dev_data;
628
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
630
switch (this_attr->address) {
631
case TSL2563_REG_HIGHLOW:
632
val = chip->high_thres;
634
case TSL2563_REG_LOWLOW:
635
val = chip->low_thres;
638
return snprintf(buf, PAGE_SIZE, "%d\n", val);
641
static ssize_t tsl2563_write_thresh(struct device *dev,
642
struct device_attribute *attr,
646
struct iio_dev *indio_dev = dev_get_drvdata(dev);
647
struct tsl2563_chip *chip = indio_dev->dev_data;
648
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
652
ret = strict_strtoul(buf, 10, &val);
655
mutex_lock(&chip->lock);
656
ret = tsl2563_write(chip->client, this_attr->address, val & 0xFF);
503
struct tsl2563_chip *chip = iio_priv(indio_dev);
505
mutex_lock(&chip->lock);
508
switch (chan->type) {
510
ret = tsl2563_get_adc(chip);
513
calib0 = calib_adc(chip->data0, chip->calib0) *
514
chip->cover_comp_gain;
515
calib1 = calib_adc(chip->data1, chip->calib1) *
516
chip->cover_comp_gain;
517
*val = adc_to_lux(calib0, calib1);
521
ret = tsl2563_get_adc(chip);
524
if (chan->channel == 0)
535
case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
536
if (chan->channel == 0)
537
*val = calib_to_sysfs(chip->calib0);
539
*val = calib_to_sysfs(chip->calib1);
547
mutex_unlock(&chip->lock);
551
static const struct iio_chan_spec tsl2563_channels[] = {
552
IIO_CHAN(IIO_LIGHT, 0, 1, 1, NULL, 0, 0, 0, 0, 0, {}, 0),
553
IIO_CHAN(IIO_INTENSITY, 1, 1, 0, "both", 0,
554
(1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE), 0, 0, 0, {},
555
IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING) |
556
IIO_EV_BIT(IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING)),
557
IIO_CHAN(IIO_INTENSITY, 1, 1, 0, "ir", 1,
558
(1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE), 0, 0, 0, {},
562
static int tsl2563_read_thresh(struct iio_dev *indio_dev,
566
struct tsl2563_chip *chip = iio_priv(indio_dev);
568
switch (IIO_EVENT_CODE_EXTRACT_DIR(event_code)) {
569
case IIO_EV_DIR_RISING:
570
*val = chip->high_thres;
572
case IIO_EV_DIR_FALLING:
573
*val = chip->low_thres;
582
static ssize_t tsl2563_write_thresh(struct iio_dev *indio_dev,
586
struct tsl2563_chip *chip = iio_priv(indio_dev);
590
if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
591
address = TSL2563_REG_HIGHLOW;
593
address = TSL2563_REG_LOWLOW;
594
mutex_lock(&chip->lock);
595
ret = tsl2563_write(chip->client, address, val & 0xFF);
659
ret = tsl2563_write(chip->client, this_attr->address + 1,
598
ret = tsl2563_write(chip->client, address + 1,
660
599
(val >> 8) & 0xFF);
661
switch (this_attr->address) {
662
case TSL2563_REG_HIGHLOW:
600
if (IIO_EVENT_CODE_EXTRACT_DIR(event_code) == IIO_EV_DIR_RISING)
663
601
chip->high_thres = val;
665
case TSL2563_REG_LOWLOW:
666
603
chip->low_thres = val;
671
606
mutex_unlock(&chip->lock);
673
return ret < 0 ? ret : len;
676
static IIO_DEVICE_ATTR(intensity0_both_raw_thresh_rising_value,
679
tsl2563_write_thresh,
680
TSL2563_REG_HIGHLOW);
682
static IIO_DEVICE_ATTR(intensity0_both_raw_thresh_falling_value,
685
tsl2563_write_thresh,
688
static int tsl2563_int_th(struct iio_dev *dev_info,
693
struct tsl2563_chip *chip = dev_info->dev_data;
695
chip->event_timestamp = timestamp;
696
schedule_work(&chip->work_thresh);
701
static void tsl2563_int_bh(struct work_struct *work_s)
703
struct tsl2563_chip *chip
704
= container_of(work_s,
705
struct tsl2563_chip, work_thresh);
611
static irqreturn_t tsl2563_event_handler(int irq, void *private)
613
struct iio_dev *dev_info = private;
614
struct tsl2563_chip *chip = iio_priv(dev_info);
706
615
u8 cmd = TSL2563_CMD | TSL2563_CLEARINT;
708
iio_push_event(chip->indio_dev, 0,
617
iio_push_event(dev_info, 0,
709
618
IIO_UNMOD_EVENT_CODE(IIO_EV_CLASS_LIGHT,
711
620
IIO_EV_TYPE_THRESH,
712
621
IIO_EV_DIR_EITHER),
713
chip->event_timestamp);
716
enable_irq(chip->client->irq);
717
624
/* clear the interrupt and push the event */
718
625
i2c_master_send(chip->client, &cmd, sizeof(cmd));
722
static ssize_t tsl2563_write_interrupt_config(struct device *dev,
723
struct device_attribute *attr,
629
static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
727
struct iio_dev *indio_dev = dev_get_drvdata(dev);
728
struct tsl2563_chip *chip = indio_dev->dev_data;
729
struct iio_event_attr *this_attr = to_iio_event_attr(attr);
633
struct tsl2563_chip *chip = iio_priv(indio_dev);
732
ret = sscanf(buf, "%d", &input);
735
636
mutex_lock(&chip->lock);
736
if (input && !(chip->intr & 0x30)) {
737
iio_add_event_to_list(this_attr->listel,
738
&indio_dev->interrupts[0]->ev_list);
637
if (state && !(chip->intr & 0x30)) {
739
638
chip->intr &= ~0x30;
740
639
chip->intr |= 0x10;
741
640
/* ensure the chip is actually on */
765
662
mutex_unlock(&chip->lock);
767
return (ret < 0) ? ret : len;
770
static ssize_t tsl2563_read_interrupt_config(struct device *dev,
771
struct device_attribute *attr,
667
static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
774
struct iio_dev *indio_dev = dev_get_drvdata(dev);
775
struct tsl2563_chip *chip = indio_dev->dev_data;
670
struct tsl2563_chip *chip = iio_priv(indio_dev);
780
674
mutex_lock(&chip->lock);
781
ret = tsl2563_read(chip->client,
675
ret = tsl2563_read(chip->client, TSL2563_REG_INT,
676
&rxbuf, sizeof(rxbuf));
785
677
mutex_unlock(&chip->lock);
788
len = snprintf(buf, PAGE_SIZE, "%d\n", !!(rxbuf & 0x30));
680
ret = !!(rxbuf & 0x30);
791
return (ret < 0) ? ret : len;
794
IIO_EVENT_ATTR(intensity0_both_thresh_en,
795
tsl2563_read_interrupt_config,
796
tsl2563_write_interrupt_config,
800
static struct attribute *tsl2563_event_attributes[] = {
801
&iio_event_attr_intensity0_both_thresh_en.dev_attr.attr,
802
&iio_dev_attr_intensity0_both_raw_thresh_rising_value.dev_attr.attr,
803
&iio_dev_attr_intensity0_both_raw_thresh_falling_value.dev_attr.attr,
807
static struct attribute_group tsl2563_event_attribute_group = {
808
.attrs = tsl2563_event_attributes,
811
686
/*--------------------------------------------------------------*/
812
687
/* Probe, Attach, Remove */
813
688
/*--------------------------------------------------------------*/
814
689
static struct i2c_driver tsl2563_i2c_driver;
691
static const struct iio_info tsl2563_info_no_irq = {
692
.driver_module = THIS_MODULE,
695
static const struct iio_info tsl2563_info = {
696
.driver_module = THIS_MODULE,
697
.num_interrupt_lines = 1,
698
.read_raw = &tsl2563_read_raw,
699
.write_raw = &tsl2563_write_raw,
700
.read_event_value = &tsl2563_read_thresh,
701
.write_event_value = &tsl2563_write_thresh,
702
.read_event_config = &tsl2563_read_interrupt_config,
703
.write_event_config = &tsl2563_write_interrupt_config,
816
706
static int __devinit tsl2563_probe(struct i2c_client *client,
817
707
const struct i2c_device_id *device_id)
709
struct iio_dev *indio_dev;
819
710
struct tsl2563_chip *chip;
820
711
struct tsl2563_platform_data *pdata = client->dev.platform_data;
825
chip = kzalloc(sizeof(*chip), GFP_KERNEL);
716
indio_dev = iio_allocate_device(sizeof(*chip));
829
INIT_WORK(&chip->work_thresh, tsl2563_int_bh);
720
chip = iio_priv(indio_dev);
830
722
i2c_set_clientdata(client, chip);
831
723
chip->client = client;