20 #include <linux/module.h>
24 #include <linux/slab.h>
30 #define LM3533_ALS_RESISTOR_MIN 1
31 #define LM3533_ALS_RESISTOR_MAX 127
32 #define LM3533_ALS_CHANNEL_CURRENT_MAX 2
33 #define LM3533_ALS_THRESH_MAX 3
34 #define LM3533_ALS_ZONE_MAX 4
36 #define LM3533_REG_ALS_RESISTOR_SELECT 0x30
37 #define LM3533_REG_ALS_CONF 0x31
38 #define LM3533_REG_ALS_ZONE_INFO 0x34
39 #define LM3533_REG_ALS_READ_ADC_RAW 0x37
40 #define LM3533_REG_ALS_READ_ADC_AVERAGE 0x38
41 #define LM3533_REG_ALS_BOUNDARY_BASE 0x50
42 #define LM3533_REG_ALS_TARGET_BASE 0x60
44 #define LM3533_ALS_ENABLE_MASK 0x01
45 #define LM3533_ALS_INPUT_MODE_MASK 0x02
46 #define LM3533_ALS_INT_ENABLE_MASK 0x01
48 #define LM3533_ALS_ZONE_SHIFT 2
49 #define LM3533_ALS_ZONE_MASK 0x1c
51 #define LM3533_ALS_FLAG_INT_ENABLED 1
66 static int lm3533_als_get_adc(
struct iio_dev *indio_dev,
bool average,
81 dev_err(&indio_dev->
dev,
"failed to read adc\n");
90 static int _lm3533_als_get_zone(
struct iio_dev *indio_dev,
u8 *
zone)
98 dev_err(&indio_dev->
dev,
"failed to read zone\n");
108 static int lm3533_als_get_zone(
struct iio_dev *indio_dev,
u8 *zone)
116 ret = _lm3533_als_get_zone(indio_dev, zone);
128 static inline u8 lm3533_als_get_target_reg(
unsigned channel,
unsigned zone)
133 static int lm3533_als_get_target(
struct iio_dev *indio_dev,
unsigned channel,
134 unsigned zone,
u8 *val)
146 reg = lm3533_als_get_target_reg(channel, zone);
149 dev_err(&indio_dev->
dev,
"failed to get target current\n");
154 static int lm3533_als_set_target(
struct iio_dev *indio_dev,
unsigned channel,
155 unsigned zone,
u8 val)
167 reg = lm3533_als_get_target_reg(channel, zone);
170 dev_err(&indio_dev->
dev,
"failed to set target current\n");
175 static int lm3533_als_get_current(
struct iio_dev *indio_dev,
unsigned channel,
182 ret = lm3533_als_get_zone(indio_dev, &zone);
186 ret = lm3533_als_get_target(indio_dev, channel, zone, &target);
195 static int lm3533_als_read_raw(
struct iio_dev *indio_dev,
197 int *val,
int *val2,
long mask)
203 switch (chan->
type) {
205 ret = lm3533_als_get_adc(indio_dev,
false, val);
208 ret = lm3533_als_get_current(indio_dev, chan->
channel,
216 ret = lm3533_als_get_adc(indio_dev,
true, val);
228 #define CHANNEL_CURRENT(_channel) \
230 .type = IIO_CURRENT, \
231 .channel = _channel, \
234 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT, \
259 ret = _lm3533_als_get_zone(indio_dev, &zone);
275 static int lm3533_als_set_int_mode(
struct iio_dev *indio_dev,
int enable)
289 dev_err(&indio_dev->
dev,
"failed to set int mode %d\n",
297 static int lm3533_als_get_int_mode(
struct iio_dev *indio_dev,
int *enable)
306 dev_err(&indio_dev->
dev,
"failed to get int mode\n");
310 *enable = !!(val &
mask);
315 static inline u8 lm3533_als_get_threshold_reg(
unsigned nr,
bool raising)
322 static int lm3533_als_get_threshold(
struct iio_dev *indio_dev,
unsigned nr,
323 bool raising,
u8 *val)
332 reg = lm3533_als_get_threshold_reg(nr, raising);
335 dev_err(&indio_dev->
dev,
"failed to get threshold\n");
340 static int lm3533_als_set_threshold(
struct iio_dev *indio_dev,
unsigned nr,
341 bool raising,
u8 val)
351 reg = lm3533_als_get_threshold_reg(nr, raising);
352 reg2 = lm3533_als_get_threshold_reg(nr, !raising);
357 dev_err(&indio_dev->
dev,
"failed to get threshold\n");
365 if ((raising && (val < val2)) || (!raising && (val > val2))) {
372 dev_err(&indio_dev->
dev,
"failed to set threshold\n");
381 static int lm3533_als_get_hysteresis(
struct iio_dev *indio_dev,
unsigned nr,
393 ret = lm3533_als_get_threshold(indio_dev, nr,
false, &falling);
396 ret = lm3533_als_get_threshold(indio_dev, nr,
true, &raising);
400 *val = raising - falling;
411 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
417 ret = lm3533_als_get_int_mode(indio_dev, &enable);
429 const char *buf,
size_t len)
431 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
441 if (kstrtoul(buf, 0, &enable))
446 if (enable && !int_enabled) {
447 ret = lm3533_als_get_zone(indio_dev, &zone);
456 ret = lm3533_als_set_int_mode(indio_dev, enable);
473 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
477 ret = lm3533_als_get_zone(indio_dev, &zone);
508 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
513 switch (als_attr->
type) {
515 ret = lm3533_als_get_hysteresis(indio_dev, als_attr->
val1,
519 ret = lm3533_als_get_target(indio_dev, als_attr->
val1,
520 als_attr->
val2, &val);
523 ret = lm3533_als_get_threshold(indio_dev, als_attr->
val1,
527 ret = lm3533_als_get_threshold(indio_dev, als_attr->
val1,
542 const char *buf,
size_t len)
544 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
552 switch (als_attr->
type) {
554 ret = lm3533_als_set_target(indio_dev, als_attr->
val1,
555 als_attr->
val2, val);
558 ret = lm3533_als_set_threshold(indio_dev, als_attr->
val1,
562 ret = lm3533_als_set_threshold(indio_dev, als_attr->
val1,
575 #define ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
576 { .dev_attr = __ATTR(_name, _mode, _show, _store), \
581 #define LM3533_ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
582 struct lm3533_als_attribute lm3533_als_attr_##_name = \
583 ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2)
585 #define ALS_TARGET_ATTR_RW(_channel, _zone) \
586 LM3533_ALS_ATTR(out_current##_channel##_current##_zone##_raw, \
588 show_als_attr, store_als_attr, \
589 LM3533_ATTR_TYPE_TARGET, _channel, _zone)
613 #define ALS_THRESH_FALLING_ATTR_RW(_nr) \
614 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_falling_value, \
616 show_als_attr, store_als_attr, \
617 LM3533_ATTR_TYPE_THRESH_FALLING, _nr, 0)
619 #define ALS_THRESH_RAISING_ATTR_RW(_nr) \
620 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_raising_value, \
622 show_als_attr, store_als_attr, \
623 LM3533_ATTR_TYPE_THRESH_RAISING, _nr, 0)
640 #define ALS_HYSTERESIS_ATTR_RO(_nr) \
641 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_hysteresis, \
642 S_IRUGO, show_als_attr, NULL, \
643 LM3533_ATTR_TYPE_HYSTERESIS, _nr, 0)
657 #define ILLUMINANCE_ATTR_RO(_name) \
658 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO, show_##_name, NULL)
659 #define ILLUMINANCE_ATTR_RW(_name) \
660 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO | S_IWUSR , \
661 show_##_name, store_##_name)
676 static struct attribute *lm3533_als_event_attributes[] = {
677 &dev_attr_in_illuminance0_thresh_either_en.attr,
678 &lm3533_als_attr_in_illuminance0_thresh0_falling_value.dev_attr.attr,
679 &lm3533_als_attr_in_illuminance0_thresh0_hysteresis.dev_attr.attr,
680 &lm3533_als_attr_in_illuminance0_thresh0_raising_value.dev_attr.attr,
681 &lm3533_als_attr_in_illuminance0_thresh1_falling_value.dev_attr.attr,
682 &lm3533_als_attr_in_illuminance0_thresh1_hysteresis.dev_attr.attr,
683 &lm3533_als_attr_in_illuminance0_thresh1_raising_value.dev_attr.attr,
684 &lm3533_als_attr_in_illuminance0_thresh2_falling_value.dev_attr.attr,
685 &lm3533_als_attr_in_illuminance0_thresh2_hysteresis.dev_attr.attr,
686 &lm3533_als_attr_in_illuminance0_thresh2_raising_value.dev_attr.attr,
687 &lm3533_als_attr_in_illuminance0_thresh3_falling_value.dev_attr.attr,
688 &lm3533_als_attr_in_illuminance0_thresh3_hysteresis.dev_attr.attr,
689 &lm3533_als_attr_in_illuminance0_thresh3_raising_value.dev_attr.attr,
694 .attrs = lm3533_als_event_attributes
697 static struct attribute *lm3533_als_attributes[] = {
698 &dev_attr_in_illuminance0_zone.attr,
699 &lm3533_als_attr_out_current0_current0_raw.dev_attr.attr,
700 &lm3533_als_attr_out_current0_current1_raw.dev_attr.attr,
701 &lm3533_als_attr_out_current0_current2_raw.dev_attr.attr,
702 &lm3533_als_attr_out_current0_current3_raw.dev_attr.attr,
703 &lm3533_als_attr_out_current0_current4_raw.dev_attr.attr,
704 &lm3533_als_attr_out_current1_current0_raw.dev_attr.attr,
705 &lm3533_als_attr_out_current1_current1_raw.dev_attr.attr,
706 &lm3533_als_attr_out_current1_current2_raw.dev_attr.attr,
707 &lm3533_als_attr_out_current1_current3_raw.dev_attr.attr,
708 &lm3533_als_attr_out_current1_current4_raw.dev_attr.attr,
709 &lm3533_als_attr_out_current2_current0_raw.dev_attr.attr,
710 &lm3533_als_attr_out_current2_current1_raw.dev_attr.attr,
711 &lm3533_als_attr_out_current2_current2_raw.dev_attr.attr,
712 &lm3533_als_attr_out_current2_current3_raw.dev_attr.attr,
713 &lm3533_als_attr_out_current2_current4_raw.dev_attr.attr,
718 .attrs = lm3533_als_attributes
735 dev_err(&als->
pdev->dev,
"failed to set input mode %d\n",
752 dev_err(&als->
pdev->dev,
"failed to set resistor\n");
764 ret = lm3533_als_set_input_mode(als, pdata->
pwm_mode);
770 ret = lm3533_als_set_resistor(als, pdata->
r_select);
786 dev_err(&als->
pdev->dev,
"failed to disable interrupts\n");
792 dev_name(&als->
pdev->dev), dev);
794 dev_err(&als->
pdev->dev,
"failed to request irq %d\n",
809 dev_err(&als->
pdev->dev,
"failed to enable ALS\n");
814 static int lm3533_als_disable(
struct lm3533_als *als)
821 dev_err(&als->
pdev->dev,
"failed to disable ALS\n");
826 static const struct iio_info lm3533_als_info = {
827 .attrs = &lm3533_als_attribute_group,
828 .event_attrs = &lm3533_als_event_attribute_group,
830 .read_raw = &lm3533_als_read_raw,
845 pdata = pdev->
dev.platform_data;
855 indio_dev->
info = &lm3533_als_info;
856 indio_dev->
channels = lm3533_als_channels;
858 indio_dev->
name = dev_name(&pdev->
dev);
859 indio_dev->
dev.parent = pdev->
dev.parent;
862 als = iio_priv(indio_dev);
869 platform_set_drvdata(pdev, indio_dev);
872 ret = lm3533_als_setup_irq(als, indio_dev);
877 ret = lm3533_als_setup(als, pdata);
881 ret = lm3533_als_enable(als);
887 dev_err(&pdev->
dev,
"failed to register ALS\n");
894 lm3533_als_disable(als);
906 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
909 lm3533_als_set_int_mode(indio_dev,
false);
911 lm3533_als_disable(als);
921 .name =
"lm3533-als",
924 .probe = lm3533_als_probe,