1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * lm3533-als.c -- LM3533 Ambient Light Sensor driver
5 * Copyright (C) 2011-2012 Texas Instruments
7 * Author: Johan Hovold <jhovold@gmail.com>
10 #include <linux/atomic.h>
12 #include <linux/interrupt.h>
14 #include <linux/iio/events.h>
15 #include <linux/iio/iio.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/mfd/core.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
23 #include <linux/mfd/lm3533.h>
26 #define LM3533_ALS_RESISTOR_MIN 1
27 #define LM3533_ALS_RESISTOR_MAX 127
28 #define LM3533_ALS_CHANNEL_CURRENT_MAX 2
29 #define LM3533_ALS_THRESH_MAX 3
30 #define LM3533_ALS_ZONE_MAX 4
32 #define LM3533_REG_ALS_RESISTOR_SELECT 0x30
33 #define LM3533_REG_ALS_CONF 0x31
34 #define LM3533_REG_ALS_ZONE_INFO 0x34
35 #define LM3533_REG_ALS_READ_ADC_RAW 0x37
36 #define LM3533_REG_ALS_READ_ADC_AVERAGE 0x38
37 #define LM3533_REG_ALS_BOUNDARY_BASE 0x50
38 #define LM3533_REG_ALS_TARGET_BASE 0x60
40 #define LM3533_ALS_ENABLE_MASK 0x01
41 #define LM3533_ALS_INPUT_MODE_MASK 0x02
42 #define LM3533_ALS_INT_ENABLE_MASK 0x01
44 #define LM3533_ALS_ZONE_SHIFT 2
45 #define LM3533_ALS_ZONE_MASK 0x1c
47 #define LM3533_ALS_FLAG_INT_ENABLED 1
51 struct lm3533
*lm3533
;
52 struct platform_device
*pdev
;
58 struct mutex thresh_mutex
;
62 static int lm3533_als_get_adc(struct iio_dev
*indio_dev
, bool average
,
65 struct lm3533_als
*als
= iio_priv(indio_dev
);
71 reg
= LM3533_REG_ALS_READ_ADC_AVERAGE
;
73 reg
= LM3533_REG_ALS_READ_ADC_RAW
;
75 ret
= lm3533_read(als
->lm3533
, reg
, &val
);
77 dev_err(&indio_dev
->dev
, "failed to read adc\n");
86 static int _lm3533_als_get_zone(struct iio_dev
*indio_dev
, u8
*zone
)
88 struct lm3533_als
*als
= iio_priv(indio_dev
);
92 ret
= lm3533_read(als
->lm3533
, LM3533_REG_ALS_ZONE_INFO
, &val
);
94 dev_err(&indio_dev
->dev
, "failed to read zone\n");
98 val
= (val
& LM3533_ALS_ZONE_MASK
) >> LM3533_ALS_ZONE_SHIFT
;
99 *zone
= min_t(u8
, val
, LM3533_ALS_ZONE_MAX
);
104 static int lm3533_als_get_zone(struct iio_dev
*indio_dev
, u8
*zone
)
106 struct lm3533_als
*als
= iio_priv(indio_dev
);
109 if (test_bit(LM3533_ALS_FLAG_INT_ENABLED
, &als
->flags
)) {
110 *zone
= atomic_read(&als
->zone
);
112 ret
= _lm3533_als_get_zone(indio_dev
, zone
);
121 * channel output channel 0..2
124 static inline u8
lm3533_als_get_target_reg(unsigned channel
, unsigned zone
)
126 return LM3533_REG_ALS_TARGET_BASE
+ 5 * channel
+ zone
;
129 static int lm3533_als_get_target(struct iio_dev
*indio_dev
, unsigned channel
,
130 unsigned zone
, u8
*val
)
132 struct lm3533_als
*als
= iio_priv(indio_dev
);
136 if (channel
> LM3533_ALS_CHANNEL_CURRENT_MAX
)
139 if (zone
> LM3533_ALS_ZONE_MAX
)
142 reg
= lm3533_als_get_target_reg(channel
, zone
);
143 ret
= lm3533_read(als
->lm3533
, reg
, val
);
145 dev_err(&indio_dev
->dev
, "failed to get target current\n");
150 static int lm3533_als_set_target(struct iio_dev
*indio_dev
, unsigned channel
,
151 unsigned zone
, u8 val
)
153 struct lm3533_als
*als
= iio_priv(indio_dev
);
157 if (channel
> LM3533_ALS_CHANNEL_CURRENT_MAX
)
160 if (zone
> LM3533_ALS_ZONE_MAX
)
163 reg
= lm3533_als_get_target_reg(channel
, zone
);
164 ret
= lm3533_write(als
->lm3533
, reg
, val
);
166 dev_err(&indio_dev
->dev
, "failed to set target current\n");
171 static int lm3533_als_get_current(struct iio_dev
*indio_dev
, unsigned channel
,
178 ret
= lm3533_als_get_zone(indio_dev
, &zone
);
182 ret
= lm3533_als_get_target(indio_dev
, channel
, zone
, &target
);
191 static int lm3533_als_read_raw(struct iio_dev
*indio_dev
,
192 struct iio_chan_spec
const *chan
,
193 int *val
, int *val2
, long mask
)
198 case IIO_CHAN_INFO_RAW
:
199 switch (chan
->type
) {
201 ret
= lm3533_als_get_adc(indio_dev
, false, val
);
204 ret
= lm3533_als_get_current(indio_dev
, chan
->channel
,
211 case IIO_CHAN_INFO_AVERAGE_RAW
:
212 ret
= lm3533_als_get_adc(indio_dev
, true, val
);
224 #define CHANNEL_CURRENT(_channel) \
226 .type = IIO_CURRENT, \
227 .channel = _channel, \
230 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
233 static const struct iio_chan_spec lm3533_als_channels
[] = {
238 .info_mask_separate
= BIT(IIO_CHAN_INFO_AVERAGE_RAW
) |
239 BIT(IIO_CHAN_INFO_RAW
),
246 static irqreturn_t
lm3533_als_isr(int irq
, void *dev_id
)
249 struct iio_dev
*indio_dev
= dev_id
;
250 struct lm3533_als
*als
= iio_priv(indio_dev
);
254 /* Clear interrupt by reading the ALS zone register. */
255 ret
= _lm3533_als_get_zone(indio_dev
, &zone
);
259 atomic_set(&als
->zone
, zone
);
261 iio_push_event(indio_dev
,
262 IIO_UNMOD_EVENT_CODE(IIO_LIGHT
,
266 iio_get_time_ns(indio_dev
));
271 static int lm3533_als_set_int_mode(struct iio_dev
*indio_dev
, int enable
)
273 struct lm3533_als
*als
= iio_priv(indio_dev
);
274 u8 mask
= LM3533_ALS_INT_ENABLE_MASK
;
283 ret
= lm3533_update(als
->lm3533
, LM3533_REG_ALS_ZONE_INFO
, val
, mask
);
285 dev_err(&indio_dev
->dev
, "failed to set int mode %d\n",
293 static int lm3533_als_get_int_mode(struct iio_dev
*indio_dev
, int *enable
)
295 struct lm3533_als
*als
= iio_priv(indio_dev
);
296 u8 mask
= LM3533_ALS_INT_ENABLE_MASK
;
300 ret
= lm3533_read(als
->lm3533
, LM3533_REG_ALS_ZONE_INFO
, &val
);
302 dev_err(&indio_dev
->dev
, "failed to get int mode\n");
306 *enable
= !!(val
& mask
);
311 static inline u8
lm3533_als_get_threshold_reg(unsigned nr
, bool raising
)
313 u8 offset
= !raising
;
315 return LM3533_REG_ALS_BOUNDARY_BASE
+ 2 * nr
+ offset
;
318 static int lm3533_als_get_threshold(struct iio_dev
*indio_dev
, unsigned nr
,
319 bool raising
, u8
*val
)
321 struct lm3533_als
*als
= iio_priv(indio_dev
);
325 if (nr
> LM3533_ALS_THRESH_MAX
)
328 reg
= lm3533_als_get_threshold_reg(nr
, raising
);
329 ret
= lm3533_read(als
->lm3533
, reg
, val
);
331 dev_err(&indio_dev
->dev
, "failed to get threshold\n");
336 static int lm3533_als_set_threshold(struct iio_dev
*indio_dev
, unsigned nr
,
337 bool raising
, u8 val
)
339 struct lm3533_als
*als
= iio_priv(indio_dev
);
344 if (nr
> LM3533_ALS_THRESH_MAX
)
347 reg
= lm3533_als_get_threshold_reg(nr
, raising
);
348 reg2
= lm3533_als_get_threshold_reg(nr
, !raising
);
350 mutex_lock(&als
->thresh_mutex
);
351 ret
= lm3533_read(als
->lm3533
, reg2
, &val2
);
353 dev_err(&indio_dev
->dev
, "failed to get threshold\n");
357 * This device does not allow negative hysteresis (in fact, it uses
358 * whichever value is smaller as the lower bound) so we need to make
359 * sure that thresh_falling <= thresh_raising.
361 if ((raising
&& (val
< val2
)) || (!raising
&& (val
> val2
))) {
366 ret
= lm3533_write(als
->lm3533
, reg
, val
);
368 dev_err(&indio_dev
->dev
, "failed to set threshold\n");
372 mutex_unlock(&als
->thresh_mutex
);
377 static int lm3533_als_get_hysteresis(struct iio_dev
*indio_dev
, unsigned nr
,
380 struct lm3533_als
*als
= iio_priv(indio_dev
);
385 if (nr
> LM3533_ALS_THRESH_MAX
)
388 mutex_lock(&als
->thresh_mutex
);
389 ret
= lm3533_als_get_threshold(indio_dev
, nr
, false, &falling
);
392 ret
= lm3533_als_get_threshold(indio_dev
, nr
, true, &raising
);
396 *val
= raising
- falling
;
398 mutex_unlock(&als
->thresh_mutex
);
403 static ssize_t
show_thresh_either_en(struct device
*dev
,
404 struct device_attribute
*attr
,
407 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
408 struct lm3533_als
*als
= iio_priv(indio_dev
);
413 ret
= lm3533_als_get_int_mode(indio_dev
, &enable
);
420 return scnprintf(buf
, PAGE_SIZE
, "%u\n", enable
);
423 static ssize_t
store_thresh_either_en(struct device
*dev
,
424 struct device_attribute
*attr
,
425 const char *buf
, size_t len
)
427 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
428 struct lm3533_als
*als
= iio_priv(indio_dev
);
429 unsigned long enable
;
437 if (kstrtoul(buf
, 0, &enable
))
440 int_enabled
= test_bit(LM3533_ALS_FLAG_INT_ENABLED
, &als
->flags
);
442 if (enable
&& !int_enabled
) {
443 ret
= lm3533_als_get_zone(indio_dev
, &zone
);
447 atomic_set(&als
->zone
, zone
);
449 set_bit(LM3533_ALS_FLAG_INT_ENABLED
, &als
->flags
);
452 ret
= lm3533_als_set_int_mode(indio_dev
, enable
);
455 clear_bit(LM3533_ALS_FLAG_INT_ENABLED
, &als
->flags
);
461 clear_bit(LM3533_ALS_FLAG_INT_ENABLED
, &als
->flags
);
466 static ssize_t
show_zone(struct device
*dev
,
467 struct device_attribute
*attr
, char *buf
)
469 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
473 ret
= lm3533_als_get_zone(indio_dev
, &zone
);
477 return scnprintf(buf
, PAGE_SIZE
, "%u\n", zone
);
480 enum lm3533_als_attribute_type
{
481 LM3533_ATTR_TYPE_HYSTERESIS
,
482 LM3533_ATTR_TYPE_TARGET
,
483 LM3533_ATTR_TYPE_THRESH_FALLING
,
484 LM3533_ATTR_TYPE_THRESH_RAISING
,
487 struct lm3533_als_attribute
{
488 struct device_attribute dev_attr
;
489 enum lm3533_als_attribute_type type
;
494 static inline struct lm3533_als_attribute
*
495 to_lm3533_als_attr(struct device_attribute
*attr
)
497 return container_of(attr
, struct lm3533_als_attribute
, dev_attr
);
500 static ssize_t
show_als_attr(struct device
*dev
,
501 struct device_attribute
*attr
,
504 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
505 struct lm3533_als_attribute
*als_attr
= to_lm3533_als_attr(attr
);
509 switch (als_attr
->type
) {
510 case LM3533_ATTR_TYPE_HYSTERESIS
:
511 ret
= lm3533_als_get_hysteresis(indio_dev
, als_attr
->val1
,
514 case LM3533_ATTR_TYPE_TARGET
:
515 ret
= lm3533_als_get_target(indio_dev
, als_attr
->val1
,
516 als_attr
->val2
, &val
);
518 case LM3533_ATTR_TYPE_THRESH_FALLING
:
519 ret
= lm3533_als_get_threshold(indio_dev
, als_attr
->val1
,
522 case LM3533_ATTR_TYPE_THRESH_RAISING
:
523 ret
= lm3533_als_get_threshold(indio_dev
, als_attr
->val1
,
533 return scnprintf(buf
, PAGE_SIZE
, "%u\n", val
);
536 static ssize_t
store_als_attr(struct device
*dev
,
537 struct device_attribute
*attr
,
538 const char *buf
, size_t len
)
540 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
541 struct lm3533_als_attribute
*als_attr
= to_lm3533_als_attr(attr
);
545 if (kstrtou8(buf
, 0, &val
))
548 switch (als_attr
->type
) {
549 case LM3533_ATTR_TYPE_TARGET
:
550 ret
= lm3533_als_set_target(indio_dev
, als_attr
->val1
,
551 als_attr
->val2
, val
);
553 case LM3533_ATTR_TYPE_THRESH_FALLING
:
554 ret
= lm3533_als_set_threshold(indio_dev
, als_attr
->val1
,
557 case LM3533_ATTR_TYPE_THRESH_RAISING
:
558 ret
= lm3533_als_set_threshold(indio_dev
, als_attr
->val1
,
571 #define ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
572 { .dev_attr = __ATTR(_name, _mode, _show, _store), \
577 #define LM3533_ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2) \
578 struct lm3533_als_attribute lm3533_als_attr_##_name = \
579 ALS_ATTR(_name, _mode, _show, _store, _type, _val1, _val2)
581 #define ALS_TARGET_ATTR_RW(_channel, _zone) \
582 LM3533_ALS_ATTR(out_current##_channel##_current##_zone##_raw, \
584 show_als_attr, store_als_attr, \
585 LM3533_ATTR_TYPE_TARGET, _channel, _zone)
587 * ALS output current values (ALS mapper targets)
589 * out_current[0-2]_current[0-4]_raw 0-255
591 static ALS_TARGET_ATTR_RW(0, 0);
592 static ALS_TARGET_ATTR_RW(0, 1);
593 static ALS_TARGET_ATTR_RW(0, 2);
594 static ALS_TARGET_ATTR_RW(0, 3);
595 static ALS_TARGET_ATTR_RW(0, 4);
597 static ALS_TARGET_ATTR_RW(1, 0);
598 static ALS_TARGET_ATTR_RW(1, 1);
599 static ALS_TARGET_ATTR_RW(1, 2);
600 static ALS_TARGET_ATTR_RW(1, 3);
601 static ALS_TARGET_ATTR_RW(1, 4);
603 static ALS_TARGET_ATTR_RW(2, 0);
604 static ALS_TARGET_ATTR_RW(2, 1);
605 static ALS_TARGET_ATTR_RW(2, 2);
606 static ALS_TARGET_ATTR_RW(2, 3);
607 static ALS_TARGET_ATTR_RW(2, 4);
609 #define ALS_THRESH_FALLING_ATTR_RW(_nr) \
610 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_falling_value, \
612 show_als_attr, store_als_attr, \
613 LM3533_ATTR_TYPE_THRESH_FALLING, _nr, 0)
615 #define ALS_THRESH_RAISING_ATTR_RW(_nr) \
616 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_raising_value, \
618 show_als_attr, store_als_attr, \
619 LM3533_ATTR_TYPE_THRESH_RAISING, _nr, 0)
621 * ALS Zone thresholds (boundaries)
623 * in_illuminance0_thresh[0-3]_falling_value 0-255
624 * in_illuminance0_thresh[0-3]_raising_value 0-255
626 static ALS_THRESH_FALLING_ATTR_RW(0);
627 static ALS_THRESH_FALLING_ATTR_RW(1);
628 static ALS_THRESH_FALLING_ATTR_RW(2);
629 static ALS_THRESH_FALLING_ATTR_RW(3);
631 static ALS_THRESH_RAISING_ATTR_RW(0);
632 static ALS_THRESH_RAISING_ATTR_RW(1);
633 static ALS_THRESH_RAISING_ATTR_RW(2);
634 static ALS_THRESH_RAISING_ATTR_RW(3);
636 #define ALS_HYSTERESIS_ATTR_RO(_nr) \
637 LM3533_ALS_ATTR(in_illuminance0_thresh##_nr##_hysteresis, \
638 S_IRUGO, show_als_attr, NULL, \
639 LM3533_ATTR_TYPE_HYSTERESIS, _nr, 0)
641 * ALS Zone threshold hysteresis
643 * threshY_hysteresis = threshY_raising - threshY_falling
645 * in_illuminance0_thresh[0-3]_hysteresis 0-255
646 * in_illuminance0_thresh[0-3]_hysteresis 0-255
648 static ALS_HYSTERESIS_ATTR_RO(0);
649 static ALS_HYSTERESIS_ATTR_RO(1);
650 static ALS_HYSTERESIS_ATTR_RO(2);
651 static ALS_HYSTERESIS_ATTR_RO(3);
653 #define ILLUMINANCE_ATTR_RO(_name) \
654 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO, show_##_name, NULL)
655 #define ILLUMINANCE_ATTR_RW(_name) \
656 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO | S_IWUSR, \
657 show_##_name, store_##_name)
659 * ALS Zone threshold-event enable
661 * in_illuminance0_thresh_either_en 0,1
663 static ILLUMINANCE_ATTR_RW(thresh_either_en
);
668 * in_illuminance0_zone 0-4
670 static ILLUMINANCE_ATTR_RO(zone
);
672 static struct attribute
*lm3533_als_event_attributes
[] = {
673 &dev_attr_in_illuminance0_thresh_either_en
.attr
,
674 &lm3533_als_attr_in_illuminance0_thresh0_falling_value
.dev_attr
.attr
,
675 &lm3533_als_attr_in_illuminance0_thresh0_hysteresis
.dev_attr
.attr
,
676 &lm3533_als_attr_in_illuminance0_thresh0_raising_value
.dev_attr
.attr
,
677 &lm3533_als_attr_in_illuminance0_thresh1_falling_value
.dev_attr
.attr
,
678 &lm3533_als_attr_in_illuminance0_thresh1_hysteresis
.dev_attr
.attr
,
679 &lm3533_als_attr_in_illuminance0_thresh1_raising_value
.dev_attr
.attr
,
680 &lm3533_als_attr_in_illuminance0_thresh2_falling_value
.dev_attr
.attr
,
681 &lm3533_als_attr_in_illuminance0_thresh2_hysteresis
.dev_attr
.attr
,
682 &lm3533_als_attr_in_illuminance0_thresh2_raising_value
.dev_attr
.attr
,
683 &lm3533_als_attr_in_illuminance0_thresh3_falling_value
.dev_attr
.attr
,
684 &lm3533_als_attr_in_illuminance0_thresh3_hysteresis
.dev_attr
.attr
,
685 &lm3533_als_attr_in_illuminance0_thresh3_raising_value
.dev_attr
.attr
,
689 static const struct attribute_group lm3533_als_event_attribute_group
= {
690 .attrs
= lm3533_als_event_attributes
693 static struct attribute
*lm3533_als_attributes
[] = {
694 &dev_attr_in_illuminance0_zone
.attr
,
695 &lm3533_als_attr_out_current0_current0_raw
.dev_attr
.attr
,
696 &lm3533_als_attr_out_current0_current1_raw
.dev_attr
.attr
,
697 &lm3533_als_attr_out_current0_current2_raw
.dev_attr
.attr
,
698 &lm3533_als_attr_out_current0_current3_raw
.dev_attr
.attr
,
699 &lm3533_als_attr_out_current0_current4_raw
.dev_attr
.attr
,
700 &lm3533_als_attr_out_current1_current0_raw
.dev_attr
.attr
,
701 &lm3533_als_attr_out_current1_current1_raw
.dev_attr
.attr
,
702 &lm3533_als_attr_out_current1_current2_raw
.dev_attr
.attr
,
703 &lm3533_als_attr_out_current1_current3_raw
.dev_attr
.attr
,
704 &lm3533_als_attr_out_current1_current4_raw
.dev_attr
.attr
,
705 &lm3533_als_attr_out_current2_current0_raw
.dev_attr
.attr
,
706 &lm3533_als_attr_out_current2_current1_raw
.dev_attr
.attr
,
707 &lm3533_als_attr_out_current2_current2_raw
.dev_attr
.attr
,
708 &lm3533_als_attr_out_current2_current3_raw
.dev_attr
.attr
,
709 &lm3533_als_attr_out_current2_current4_raw
.dev_attr
.attr
,
713 static const struct attribute_group lm3533_als_attribute_group
= {
714 .attrs
= lm3533_als_attributes
717 static int lm3533_als_set_input_mode(struct lm3533_als
*als
, bool pwm_mode
)
719 u8 mask
= LM3533_ALS_INPUT_MODE_MASK
;
724 val
= mask
; /* pwm input */
726 val
= 0; /* analog input */
728 ret
= lm3533_update(als
->lm3533
, LM3533_REG_ALS_CONF
, val
, mask
);
730 dev_err(&als
->pdev
->dev
, "failed to set input mode %d\n",
738 static int lm3533_als_set_resistor(struct lm3533_als
*als
, u8 val
)
742 if (val
< LM3533_ALS_RESISTOR_MIN
|| val
> LM3533_ALS_RESISTOR_MAX
) {
743 dev_err(&als
->pdev
->dev
, "invalid resistor value\n");
747 ret
= lm3533_write(als
->lm3533
, LM3533_REG_ALS_RESISTOR_SELECT
, val
);
749 dev_err(&als
->pdev
->dev
, "failed to set resistor\n");
756 static int lm3533_als_setup(struct lm3533_als
*als
,
757 struct lm3533_als_platform_data
*pdata
)
761 ret
= lm3533_als_set_input_mode(als
, pdata
->pwm_mode
);
765 /* ALS input is always high impedance in PWM-mode. */
766 if (!pdata
->pwm_mode
) {
767 ret
= lm3533_als_set_resistor(als
, pdata
->r_select
);
775 static int lm3533_als_setup_irq(struct lm3533_als
*als
, void *dev
)
777 u8 mask
= LM3533_ALS_INT_ENABLE_MASK
;
780 /* Make sure interrupts are disabled. */
781 ret
= lm3533_update(als
->lm3533
, LM3533_REG_ALS_ZONE_INFO
, 0, mask
);
783 dev_err(&als
->pdev
->dev
, "failed to disable interrupts\n");
787 ret
= request_threaded_irq(als
->irq
, NULL
, lm3533_als_isr
,
788 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
789 dev_name(&als
->pdev
->dev
), dev
);
791 dev_err(&als
->pdev
->dev
, "failed to request irq %d\n",
799 static int lm3533_als_enable(struct lm3533_als
*als
)
801 u8 mask
= LM3533_ALS_ENABLE_MASK
;
804 ret
= lm3533_update(als
->lm3533
, LM3533_REG_ALS_CONF
, mask
, mask
);
806 dev_err(&als
->pdev
->dev
, "failed to enable ALS\n");
811 static int lm3533_als_disable(struct lm3533_als
*als
)
813 u8 mask
= LM3533_ALS_ENABLE_MASK
;
816 ret
= lm3533_update(als
->lm3533
, LM3533_REG_ALS_CONF
, 0, mask
);
818 dev_err(&als
->pdev
->dev
, "failed to disable ALS\n");
823 static const struct iio_info lm3533_als_info
= {
824 .attrs
= &lm3533_als_attribute_group
,
825 .event_attrs
= &lm3533_als_event_attribute_group
,
826 .read_raw
= &lm3533_als_read_raw
,
829 static int lm3533_als_probe(struct platform_device
*pdev
)
831 struct lm3533
*lm3533
;
832 struct lm3533_als_platform_data
*pdata
;
833 struct lm3533_als
*als
;
834 struct iio_dev
*indio_dev
;
837 lm3533
= dev_get_drvdata(pdev
->dev
.parent
);
841 pdata
= pdev
->dev
.platform_data
;
843 dev_err(&pdev
->dev
, "no platform data\n");
847 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(*als
));
851 indio_dev
->info
= &lm3533_als_info
;
852 indio_dev
->channels
= lm3533_als_channels
;
853 indio_dev
->num_channels
= ARRAY_SIZE(lm3533_als_channels
);
854 indio_dev
->name
= dev_name(&pdev
->dev
);
855 iio_device_set_parent(indio_dev
, pdev
->dev
.parent
);
856 indio_dev
->modes
= INDIO_DIRECT_MODE
;
858 als
= iio_priv(indio_dev
);
859 als
->lm3533
= lm3533
;
861 als
->irq
= lm3533
->irq
;
862 atomic_set(&als
->zone
, 0);
863 mutex_init(&als
->thresh_mutex
);
865 platform_set_drvdata(pdev
, indio_dev
);
868 ret
= lm3533_als_setup_irq(als
, indio_dev
);
873 ret
= lm3533_als_setup(als
, pdata
);
877 ret
= lm3533_als_enable(als
);
881 ret
= iio_device_register(indio_dev
);
883 dev_err(&pdev
->dev
, "failed to register ALS\n");
890 lm3533_als_disable(als
);
893 free_irq(als
->irq
, indio_dev
);
898 static int lm3533_als_remove(struct platform_device
*pdev
)
900 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
901 struct lm3533_als
*als
= iio_priv(indio_dev
);
903 lm3533_als_set_int_mode(indio_dev
, false);
904 iio_device_unregister(indio_dev
);
905 lm3533_als_disable(als
);
907 free_irq(als
->irq
, indio_dev
);
912 static struct platform_driver lm3533_als_driver
= {
914 .name
= "lm3533-als",
916 .probe
= lm3533_als_probe
,
917 .remove
= lm3533_als_remove
,
919 module_platform_driver(lm3533_als_driver
);
921 MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
922 MODULE_DESCRIPTION("LM3533 Ambient Light Sensor driver");
923 MODULE_LICENSE("GPL");
924 MODULE_ALIAS("platform:lm3533-als");