1 // SPDX-License-Identifier: GPL-2.0-only
3 * ltr501.c - Support for Lite-On LTR501 ambient light and proximity sensor
5 * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net>
7 * 7-bit I2C slave address 0x23
9 * TODO: IR LED characteristics
12 #include <linux/module.h>
13 #include <linux/i2c.h>
14 #include <linux/err.h>
15 #include <linux/delay.h>
16 #include <linux/regmap.h>
17 #include <linux/acpi.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/events.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/triggered_buffer.h>
26 #define LTR501_DRV_NAME "ltr501"
28 #define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */
29 #define LTR501_PS_CONTR 0x81 /* PS operation mode */
30 #define LTR501_PS_MEAS_RATE 0x84 /* measurement rate*/
31 #define LTR501_ALS_MEAS_RATE 0x85 /* ALS integ time, measurement rate*/
32 #define LTR501_PART_ID 0x86
33 #define LTR501_MANUFAC_ID 0x87
34 #define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */
35 #define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */
36 #define LTR501_ALS_PS_STATUS 0x8c
37 #define LTR501_PS_DATA 0x8d /* 16-bit, little endian */
38 #define LTR501_INTR 0x8f /* output mode, polarity, mode */
39 #define LTR501_PS_THRESH_UP 0x90 /* 11 bit, ps upper threshold */
40 #define LTR501_PS_THRESH_LOW 0x92 /* 11 bit, ps lower threshold */
41 #define LTR501_ALS_THRESH_UP 0x97 /* 16 bit, ALS upper threshold */
42 #define LTR501_ALS_THRESH_LOW 0x99 /* 16 bit, ALS lower threshold */
43 #define LTR501_INTR_PRST 0x9e /* ps thresh, als thresh */
44 #define LTR501_MAX_REG 0x9f
46 #define LTR501_ALS_CONTR_SW_RESET BIT(2)
47 #define LTR501_CONTR_PS_GAIN_MASK (BIT(3) | BIT(2))
48 #define LTR501_CONTR_PS_GAIN_SHIFT 2
49 #define LTR501_CONTR_ALS_GAIN_MASK BIT(3)
50 #define LTR501_CONTR_ACTIVE BIT(1)
52 #define LTR501_STATUS_ALS_INTR BIT(3)
53 #define LTR501_STATUS_ALS_RDY BIT(2)
54 #define LTR501_STATUS_PS_INTR BIT(1)
55 #define LTR501_STATUS_PS_RDY BIT(0)
57 #define LTR501_PS_DATA_MASK 0x7ff
58 #define LTR501_PS_THRESH_MASK 0x7ff
59 #define LTR501_ALS_THRESH_MASK 0xffff
61 #define LTR501_ALS_DEF_PERIOD 500000
62 #define LTR501_PS_DEF_PERIOD 100000
64 #define LTR501_REGMAP_NAME "ltr501_regmap"
66 #define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \
67 ((vis_coeff * vis_data) - (ir_coeff * ir_data))
69 static const int int_time_mapping
[] = {100000, 50000, 200000, 400000};
71 static const struct reg_field reg_field_it
=
72 REG_FIELD(LTR501_ALS_MEAS_RATE
, 3, 4);
73 static const struct reg_field reg_field_als_intr
=
74 REG_FIELD(LTR501_INTR
, 1, 1);
75 static const struct reg_field reg_field_ps_intr
=
76 REG_FIELD(LTR501_INTR
, 0, 0);
77 static const struct reg_field reg_field_als_rate
=
78 REG_FIELD(LTR501_ALS_MEAS_RATE
, 0, 2);
79 static const struct reg_field reg_field_ps_rate
=
80 REG_FIELD(LTR501_PS_MEAS_RATE
, 0, 3);
81 static const struct reg_field reg_field_als_prst
=
82 REG_FIELD(LTR501_INTR_PRST
, 0, 3);
83 static const struct reg_field reg_field_ps_prst
=
84 REG_FIELD(LTR501_INTR_PRST
, 4, 7);
86 struct ltr501_samp_table
{
87 int freq_val
; /* repetition frequency in micro HZ*/
88 int time_val
; /* repetition rate in micro seconds */
91 #define LTR501_RESERVED_GAIN -1
104 static struct ltr501_gain ltr501_als_gain_tbl
[] = {
109 static struct ltr501_gain ltr559_als_gain_tbl
[] = {
114 {LTR501_RESERVED_GAIN
, LTR501_RESERVED_GAIN
},
115 {LTR501_RESERVED_GAIN
, LTR501_RESERVED_GAIN
},
120 static struct ltr501_gain ltr501_ps_gain_tbl
[] = {
127 static struct ltr501_gain ltr559_ps_gain_tbl
[] = {
128 {0, 62500}, /* x16 gain */
129 {0, 31250}, /* x32 gain */
130 {0, 15625}, /* bits X1 are for x64 gain */
134 struct ltr501_chip_info
{
136 struct ltr501_gain
*als_gain
;
137 int als_gain_tbl_size
;
138 struct ltr501_gain
*ps_gain
;
139 int ps_gain_tbl_size
;
143 struct iio_chan_spec
const *channels
;
144 const int no_channels
;
145 const struct iio_info
*info
;
146 const struct iio_info
*info_no_irq
;
150 struct i2c_client
*client
;
151 struct mutex lock_als
, lock_ps
;
152 struct ltr501_chip_info
*chip_info
;
153 u8 als_contr
, ps_contr
;
154 int als_period
, ps_period
; /* period in micro seconds */
155 struct regmap
*regmap
;
156 struct regmap_field
*reg_it
;
157 struct regmap_field
*reg_als_intr
;
158 struct regmap_field
*reg_ps_intr
;
159 struct regmap_field
*reg_als_rate
;
160 struct regmap_field
*reg_ps_rate
;
161 struct regmap_field
*reg_als_prst
;
162 struct regmap_field
*reg_ps_prst
;
165 static const struct ltr501_samp_table ltr501_als_samp_table
[] = {
166 {20000000, 50000}, {10000000, 100000},
167 {5000000, 200000}, {2000000, 500000},
168 {1000000, 1000000}, {500000, 2000000},
169 {500000, 2000000}, {500000, 2000000}
172 static const struct ltr501_samp_table ltr501_ps_samp_table
[] = {
173 {20000000, 50000}, {14285714, 70000},
174 {10000000, 100000}, {5000000, 200000},
175 {2000000, 500000}, {1000000, 1000000},
176 {500000, 2000000}, {500000, 2000000},
180 static int ltr501_match_samp_freq(const struct ltr501_samp_table
*tab
,
181 int len
, int val
, int val2
)
185 freq
= val
* 1000000 + val2
;
187 for (i
= 0; i
< len
; i
++) {
188 if (tab
[i
].freq_val
== freq
)
195 static int ltr501_als_read_samp_freq(struct ltr501_data
*data
,
200 ret
= regmap_field_read(data
->reg_als_rate
, &i
);
204 if (i
< 0 || i
>= ARRAY_SIZE(ltr501_als_samp_table
))
207 *val
= ltr501_als_samp_table
[i
].freq_val
/ 1000000;
208 *val2
= ltr501_als_samp_table
[i
].freq_val
% 1000000;
210 return IIO_VAL_INT_PLUS_MICRO
;
213 static int ltr501_ps_read_samp_freq(struct ltr501_data
*data
,
218 ret
= regmap_field_read(data
->reg_ps_rate
, &i
);
222 if (i
< 0 || i
>= ARRAY_SIZE(ltr501_ps_samp_table
))
225 *val
= ltr501_ps_samp_table
[i
].freq_val
/ 1000000;
226 *val2
= ltr501_ps_samp_table
[i
].freq_val
% 1000000;
228 return IIO_VAL_INT_PLUS_MICRO
;
231 static int ltr501_als_write_samp_freq(struct ltr501_data
*data
,
236 i
= ltr501_match_samp_freq(ltr501_als_samp_table
,
237 ARRAY_SIZE(ltr501_als_samp_table
),
243 mutex_lock(&data
->lock_als
);
244 ret
= regmap_field_write(data
->reg_als_rate
, i
);
245 mutex_unlock(&data
->lock_als
);
250 static int ltr501_ps_write_samp_freq(struct ltr501_data
*data
,
255 i
= ltr501_match_samp_freq(ltr501_ps_samp_table
,
256 ARRAY_SIZE(ltr501_ps_samp_table
),
262 mutex_lock(&data
->lock_ps
);
263 ret
= regmap_field_write(data
->reg_ps_rate
, i
);
264 mutex_unlock(&data
->lock_ps
);
269 static int ltr501_als_read_samp_period(struct ltr501_data
*data
, int *val
)
273 ret
= regmap_field_read(data
->reg_als_rate
, &i
);
277 if (i
< 0 || i
>= ARRAY_SIZE(ltr501_als_samp_table
))
280 *val
= ltr501_als_samp_table
[i
].time_val
;
285 static int ltr501_ps_read_samp_period(struct ltr501_data
*data
, int *val
)
289 ret
= regmap_field_read(data
->reg_ps_rate
, &i
);
293 if (i
< 0 || i
>= ARRAY_SIZE(ltr501_ps_samp_table
))
296 *val
= ltr501_ps_samp_table
[i
].time_val
;
301 /* IR and visible spectrum coeff's are given in data sheet */
302 static unsigned long ltr501_calculate_lux(u16 vis_data
, u16 ir_data
)
304 unsigned long ratio
, lux
;
309 /* multiply numerator by 100 to avoid handling ratio < 1 */
310 ratio
= DIV_ROUND_UP(ir_data
* 100, ir_data
+ vis_data
);
313 lux
= LTR501_LUX_CONV(1774, vis_data
, -1105, ir_data
);
314 else if (ratio
>= 45 && ratio
< 64)
315 lux
= LTR501_LUX_CONV(3772, vis_data
, 1336, ir_data
);
316 else if (ratio
>= 64 && ratio
< 85)
317 lux
= LTR501_LUX_CONV(1690, vis_data
, 169, ir_data
);
324 static int ltr501_drdy(struct ltr501_data
*data
, u8 drdy_mask
)
330 ret
= regmap_read(data
->regmap
, LTR501_ALS_PS_STATUS
, &status
);
333 if ((status
& drdy_mask
) == drdy_mask
)
338 dev_err(&data
->client
->dev
, "ltr501_drdy() failed, data not ready\n");
342 static int ltr501_set_it_time(struct ltr501_data
*data
, int it
)
344 int ret
, i
, index
= -1, status
;
346 for (i
= 0; i
< ARRAY_SIZE(int_time_mapping
); i
++) {
347 if (int_time_mapping
[i
] == it
) {
352 /* Make sure integ time index is valid */
356 ret
= regmap_read(data
->regmap
, LTR501_ALS_CONTR
, &status
);
360 if (status
& LTR501_CONTR_ALS_GAIN_MASK
) {
362 * 200 ms and 400 ms integ time can only be
363 * used in dynamic range 1
368 /* 50 ms integ time can only be used in dynamic range 2 */
372 return regmap_field_write(data
->reg_it
, index
);
375 /* read int time in micro seconds */
376 static int ltr501_read_it_time(struct ltr501_data
*data
, int *val
, int *val2
)
380 ret
= regmap_field_read(data
->reg_it
, &index
);
384 /* Make sure integ time index is valid */
385 if (index
< 0 || index
>= ARRAY_SIZE(int_time_mapping
))
388 *val2
= int_time_mapping
[index
];
391 return IIO_VAL_INT_PLUS_MICRO
;
394 static int ltr501_read_als(struct ltr501_data
*data
, __le16 buf
[2])
398 ret
= ltr501_drdy(data
, LTR501_STATUS_ALS_RDY
);
401 /* always read both ALS channels in given order */
402 return regmap_bulk_read(data
->regmap
, LTR501_ALS_DATA1
,
403 buf
, 2 * sizeof(__le16
));
406 static int ltr501_read_ps(struct ltr501_data
*data
)
410 ret
= ltr501_drdy(data
, LTR501_STATUS_PS_RDY
);
414 ret
= regmap_bulk_read(data
->regmap
, LTR501_PS_DATA
,
422 static int ltr501_read_intr_prst(struct ltr501_data
*data
,
423 enum iio_chan_type type
,
426 int ret
, samp_period
, prst
;
430 ret
= regmap_field_read(data
->reg_als_prst
, &prst
);
434 ret
= ltr501_als_read_samp_period(data
, &samp_period
);
438 *val2
= samp_period
* prst
;
439 return IIO_VAL_INT_PLUS_MICRO
;
441 ret
= regmap_field_read(data
->reg_ps_prst
, &prst
);
445 ret
= ltr501_ps_read_samp_period(data
, &samp_period
);
450 *val2
= samp_period
* prst
;
451 return IIO_VAL_INT_PLUS_MICRO
;
459 static int ltr501_write_intr_prst(struct ltr501_data
*data
,
460 enum iio_chan_type type
,
463 int ret
, samp_period
, new_val
;
464 unsigned long period
;
466 if (val
< 0 || val2
< 0)
469 /* period in microseconds */
470 period
= ((val
* 1000000) + val2
);
474 ret
= ltr501_als_read_samp_period(data
, &samp_period
);
478 /* period should be atleast equal to sampling period */
479 if (period
< samp_period
)
482 new_val
= DIV_ROUND_UP(period
, samp_period
);
483 if (new_val
< 0 || new_val
> 0x0f)
486 mutex_lock(&data
->lock_als
);
487 ret
= regmap_field_write(data
->reg_als_prst
, new_val
);
488 mutex_unlock(&data
->lock_als
);
490 data
->als_period
= period
;
494 ret
= ltr501_ps_read_samp_period(data
, &samp_period
);
498 /* period should be atleast equal to rate */
499 if (period
< samp_period
)
502 new_val
= DIV_ROUND_UP(period
, samp_period
);
503 if (new_val
< 0 || new_val
> 0x0f)
506 mutex_lock(&data
->lock_ps
);
507 ret
= regmap_field_write(data
->reg_ps_prst
, new_val
);
508 mutex_unlock(&data
->lock_ps
);
510 data
->ps_period
= period
;
520 static const struct iio_event_spec ltr501_als_event_spec
[] = {
522 .type
= IIO_EV_TYPE_THRESH
,
523 .dir
= IIO_EV_DIR_RISING
,
524 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
526 .type
= IIO_EV_TYPE_THRESH
,
527 .dir
= IIO_EV_DIR_FALLING
,
528 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
530 .type
= IIO_EV_TYPE_THRESH
,
531 .dir
= IIO_EV_DIR_EITHER
,
532 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
533 BIT(IIO_EV_INFO_PERIOD
),
538 static const struct iio_event_spec ltr501_pxs_event_spec
[] = {
540 .type
= IIO_EV_TYPE_THRESH
,
541 .dir
= IIO_EV_DIR_RISING
,
542 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
544 .type
= IIO_EV_TYPE_THRESH
,
545 .dir
= IIO_EV_DIR_FALLING
,
546 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
548 .type
= IIO_EV_TYPE_THRESH
,
549 .dir
= IIO_EV_DIR_EITHER
,
550 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
) |
551 BIT(IIO_EV_INFO_PERIOD
),
555 #define LTR501_INTENSITY_CHANNEL(_idx, _addr, _mod, _shared, \
556 _evspec, _evsize) { \
557 .type = IIO_INTENSITY, \
559 .address = (_addr), \
560 .channel2 = (_mod), \
561 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
562 .info_mask_shared_by_type = (_shared), \
563 .scan_index = (_idx), \
568 .endianness = IIO_CPU, \
570 .event_spec = _evspec,\
571 .num_event_specs = _evsize,\
574 #define LTR501_LIGHT_CHANNEL() { \
576 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
580 static const struct iio_chan_spec ltr501_channels
[] = {
581 LTR501_LIGHT_CHANNEL(),
582 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0
, IIO_MOD_LIGHT_BOTH
, 0,
583 ltr501_als_event_spec
,
584 ARRAY_SIZE(ltr501_als_event_spec
)),
585 LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1
, IIO_MOD_LIGHT_IR
,
586 BIT(IIO_CHAN_INFO_SCALE
) |
587 BIT(IIO_CHAN_INFO_INT_TIME
) |
588 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
591 .type
= IIO_PROXIMITY
,
592 .address
= LTR501_PS_DATA
,
593 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
594 BIT(IIO_CHAN_INFO_SCALE
),
600 .endianness
= IIO_CPU
,
602 .event_spec
= ltr501_pxs_event_spec
,
603 .num_event_specs
= ARRAY_SIZE(ltr501_pxs_event_spec
),
605 IIO_CHAN_SOFT_TIMESTAMP(3),
608 static const struct iio_chan_spec ltr301_channels
[] = {
609 LTR501_LIGHT_CHANNEL(),
610 LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0
, IIO_MOD_LIGHT_BOTH
, 0,
611 ltr501_als_event_spec
,
612 ARRAY_SIZE(ltr501_als_event_spec
)),
613 LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1
, IIO_MOD_LIGHT_IR
,
614 BIT(IIO_CHAN_INFO_SCALE
) |
615 BIT(IIO_CHAN_INFO_INT_TIME
) |
616 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
618 IIO_CHAN_SOFT_TIMESTAMP(2),
621 static int ltr501_read_raw(struct iio_dev
*indio_dev
,
622 struct iio_chan_spec
const *chan
,
623 int *val
, int *val2
, long mask
)
625 struct ltr501_data
*data
= iio_priv(indio_dev
);
630 case IIO_CHAN_INFO_PROCESSED
:
631 switch (chan
->type
) {
633 ret
= iio_device_claim_direct_mode(indio_dev
);
637 mutex_lock(&data
->lock_als
);
638 ret
= ltr501_read_als(data
, buf
);
639 mutex_unlock(&data
->lock_als
);
640 iio_device_release_direct_mode(indio_dev
);
643 *val
= ltr501_calculate_lux(le16_to_cpu(buf
[1]),
644 le16_to_cpu(buf
[0]));
649 case IIO_CHAN_INFO_RAW
:
650 ret
= iio_device_claim_direct_mode(indio_dev
);
654 switch (chan
->type
) {
656 mutex_lock(&data
->lock_als
);
657 ret
= ltr501_read_als(data
, buf
);
658 mutex_unlock(&data
->lock_als
);
661 *val
= le16_to_cpu(chan
->address
== LTR501_ALS_DATA1
?
666 mutex_lock(&data
->lock_ps
);
667 ret
= ltr501_read_ps(data
);
668 mutex_unlock(&data
->lock_ps
);
671 *val
= ret
& LTR501_PS_DATA_MASK
;
679 iio_device_release_direct_mode(indio_dev
);
682 case IIO_CHAN_INFO_SCALE
:
683 switch (chan
->type
) {
685 i
= (data
->als_contr
& data
->chip_info
->als_gain_mask
)
686 >> data
->chip_info
->als_gain_shift
;
687 *val
= data
->chip_info
->als_gain
[i
].scale
;
688 *val2
= data
->chip_info
->als_gain
[i
].uscale
;
689 return IIO_VAL_INT_PLUS_MICRO
;
691 i
= (data
->ps_contr
& LTR501_CONTR_PS_GAIN_MASK
) >>
692 LTR501_CONTR_PS_GAIN_SHIFT
;
693 *val
= data
->chip_info
->ps_gain
[i
].scale
;
694 *val2
= data
->chip_info
->ps_gain
[i
].uscale
;
695 return IIO_VAL_INT_PLUS_MICRO
;
699 case IIO_CHAN_INFO_INT_TIME
:
700 switch (chan
->type
) {
702 return ltr501_read_it_time(data
, val
, val2
);
706 case IIO_CHAN_INFO_SAMP_FREQ
:
707 switch (chan
->type
) {
709 return ltr501_als_read_samp_freq(data
, val
, val2
);
711 return ltr501_ps_read_samp_freq(data
, val
, val2
);
719 static int ltr501_get_gain_index(struct ltr501_gain
*gain
, int size
,
724 for (i
= 0; i
< size
; i
++)
725 if (val
== gain
[i
].scale
&& val2
== gain
[i
].uscale
)
731 static int ltr501_write_raw(struct iio_dev
*indio_dev
,
732 struct iio_chan_spec
const *chan
,
733 int val
, int val2
, long mask
)
735 struct ltr501_data
*data
= iio_priv(indio_dev
);
736 int i
, ret
, freq_val
, freq_val2
;
737 struct ltr501_chip_info
*info
= data
->chip_info
;
739 ret
= iio_device_claim_direct_mode(indio_dev
);
744 case IIO_CHAN_INFO_SCALE
:
745 switch (chan
->type
) {
747 i
= ltr501_get_gain_index(info
->als_gain
,
748 info
->als_gain_tbl_size
,
755 data
->als_contr
&= ~info
->als_gain_mask
;
756 data
->als_contr
|= i
<< info
->als_gain_shift
;
758 ret
= regmap_write(data
->regmap
, LTR501_ALS_CONTR
,
762 i
= ltr501_get_gain_index(info
->ps_gain
,
763 info
->ps_gain_tbl_size
,
769 data
->ps_contr
&= ~LTR501_CONTR_PS_GAIN_MASK
;
770 data
->ps_contr
|= i
<< LTR501_CONTR_PS_GAIN_SHIFT
;
772 ret
= regmap_write(data
->regmap
, LTR501_PS_CONTR
,
781 case IIO_CHAN_INFO_INT_TIME
:
782 switch (chan
->type
) {
788 mutex_lock(&data
->lock_als
);
789 ret
= ltr501_set_it_time(data
, val2
);
790 mutex_unlock(&data
->lock_als
);
798 case IIO_CHAN_INFO_SAMP_FREQ
:
799 switch (chan
->type
) {
801 ret
= ltr501_als_read_samp_freq(data
, &freq_val
,
806 ret
= ltr501_als_write_samp_freq(data
, val
, val2
);
810 /* update persistence count when changing frequency */
811 ret
= ltr501_write_intr_prst(data
, chan
->type
,
812 0, data
->als_period
);
815 ret
= ltr501_als_write_samp_freq(data
, freq_val
,
819 ret
= ltr501_ps_read_samp_freq(data
, &freq_val
,
824 ret
= ltr501_ps_write_samp_freq(data
, val
, val2
);
828 /* update persistence count when changing frequency */
829 ret
= ltr501_write_intr_prst(data
, chan
->type
,
833 ret
= ltr501_ps_write_samp_freq(data
, freq_val
,
847 iio_device_release_direct_mode(indio_dev
);
851 static int ltr501_read_thresh(struct iio_dev
*indio_dev
,
852 const struct iio_chan_spec
*chan
,
853 enum iio_event_type type
,
854 enum iio_event_direction dir
,
855 enum iio_event_info info
,
858 struct ltr501_data
*data
= iio_priv(indio_dev
);
859 int ret
, thresh_data
;
861 switch (chan
->type
) {
864 case IIO_EV_DIR_RISING
:
865 ret
= regmap_bulk_read(data
->regmap
,
866 LTR501_ALS_THRESH_UP
,
870 *val
= thresh_data
& LTR501_ALS_THRESH_MASK
;
872 case IIO_EV_DIR_FALLING
:
873 ret
= regmap_bulk_read(data
->regmap
,
874 LTR501_ALS_THRESH_LOW
,
878 *val
= thresh_data
& LTR501_ALS_THRESH_MASK
;
885 case IIO_EV_DIR_RISING
:
886 ret
= regmap_bulk_read(data
->regmap
,
891 *val
= thresh_data
& LTR501_PS_THRESH_MASK
;
893 case IIO_EV_DIR_FALLING
:
894 ret
= regmap_bulk_read(data
->regmap
,
895 LTR501_PS_THRESH_LOW
,
899 *val
= thresh_data
& LTR501_PS_THRESH_MASK
;
911 static int ltr501_write_thresh(struct iio_dev
*indio_dev
,
912 const struct iio_chan_spec
*chan
,
913 enum iio_event_type type
,
914 enum iio_event_direction dir
,
915 enum iio_event_info info
,
918 struct ltr501_data
*data
= iio_priv(indio_dev
);
924 switch (chan
->type
) {
926 if (val
> LTR501_ALS_THRESH_MASK
)
929 case IIO_EV_DIR_RISING
:
930 mutex_lock(&data
->lock_als
);
931 ret
= regmap_bulk_write(data
->regmap
,
932 LTR501_ALS_THRESH_UP
,
934 mutex_unlock(&data
->lock_als
);
936 case IIO_EV_DIR_FALLING
:
937 mutex_lock(&data
->lock_als
);
938 ret
= regmap_bulk_write(data
->regmap
,
939 LTR501_ALS_THRESH_LOW
,
941 mutex_unlock(&data
->lock_als
);
947 if (val
> LTR501_PS_THRESH_MASK
)
950 case IIO_EV_DIR_RISING
:
951 mutex_lock(&data
->lock_ps
);
952 ret
= regmap_bulk_write(data
->regmap
,
955 mutex_unlock(&data
->lock_ps
);
957 case IIO_EV_DIR_FALLING
:
958 mutex_lock(&data
->lock_ps
);
959 ret
= regmap_bulk_write(data
->regmap
,
960 LTR501_PS_THRESH_LOW
,
962 mutex_unlock(&data
->lock_ps
);
974 static int ltr501_read_event(struct iio_dev
*indio_dev
,
975 const struct iio_chan_spec
*chan
,
976 enum iio_event_type type
,
977 enum iio_event_direction dir
,
978 enum iio_event_info info
,
984 case IIO_EV_INFO_VALUE
:
985 return ltr501_read_thresh(indio_dev
, chan
, type
, dir
,
987 case IIO_EV_INFO_PERIOD
:
988 ret
= ltr501_read_intr_prst(iio_priv(indio_dev
),
990 *val
= *val2
/ 1000000;
991 *val2
= *val2
% 1000000;
1000 static int ltr501_write_event(struct iio_dev
*indio_dev
,
1001 const struct iio_chan_spec
*chan
,
1002 enum iio_event_type type
,
1003 enum iio_event_direction dir
,
1004 enum iio_event_info info
,
1008 case IIO_EV_INFO_VALUE
:
1011 return ltr501_write_thresh(indio_dev
, chan
, type
, dir
,
1013 case IIO_EV_INFO_PERIOD
:
1014 return ltr501_write_intr_prst(iio_priv(indio_dev
), chan
->type
,
1023 static int ltr501_read_event_config(struct iio_dev
*indio_dev
,
1024 const struct iio_chan_spec
*chan
,
1025 enum iio_event_type type
,
1026 enum iio_event_direction dir
)
1028 struct ltr501_data
*data
= iio_priv(indio_dev
);
1031 switch (chan
->type
) {
1033 ret
= regmap_field_read(data
->reg_als_intr
, &status
);
1038 ret
= regmap_field_read(data
->reg_ps_intr
, &status
);
1049 static int ltr501_write_event_config(struct iio_dev
*indio_dev
,
1050 const struct iio_chan_spec
*chan
,
1051 enum iio_event_type type
,
1052 enum iio_event_direction dir
, int state
)
1054 struct ltr501_data
*data
= iio_priv(indio_dev
);
1057 /* only 1 and 0 are valid inputs */
1058 if (state
!= 1 && state
!= 0)
1061 switch (chan
->type
) {
1063 mutex_lock(&data
->lock_als
);
1064 ret
= regmap_field_write(data
->reg_als_intr
, state
);
1065 mutex_unlock(&data
->lock_als
);
1068 mutex_lock(&data
->lock_ps
);
1069 ret
= regmap_field_write(data
->reg_ps_intr
, state
);
1070 mutex_unlock(&data
->lock_ps
);
1079 static ssize_t
ltr501_show_proximity_scale_avail(struct device
*dev
,
1080 struct device_attribute
*attr
,
1083 struct ltr501_data
*data
= iio_priv(dev_to_iio_dev(dev
));
1084 struct ltr501_chip_info
*info
= data
->chip_info
;
1088 for (i
= 0; i
< info
->ps_gain_tbl_size
; i
++) {
1089 if (info
->ps_gain
[i
].scale
== LTR501_RESERVED_GAIN
)
1091 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06d ",
1092 info
->ps_gain
[i
].scale
,
1093 info
->ps_gain
[i
].uscale
);
1096 buf
[len
- 1] = '\n';
1101 static ssize_t
ltr501_show_intensity_scale_avail(struct device
*dev
,
1102 struct device_attribute
*attr
,
1105 struct ltr501_data
*data
= iio_priv(dev_to_iio_dev(dev
));
1106 struct ltr501_chip_info
*info
= data
->chip_info
;
1110 for (i
= 0; i
< info
->als_gain_tbl_size
; i
++) {
1111 if (info
->als_gain
[i
].scale
== LTR501_RESERVED_GAIN
)
1113 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06d ",
1114 info
->als_gain
[i
].scale
,
1115 info
->als_gain
[i
].uscale
);
1118 buf
[len
- 1] = '\n';
1123 static IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4");
1124 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5");
1126 static IIO_DEVICE_ATTR(in_proximity_scale_available
, S_IRUGO
,
1127 ltr501_show_proximity_scale_avail
, NULL
, 0);
1128 static IIO_DEVICE_ATTR(in_intensity_scale_available
, S_IRUGO
,
1129 ltr501_show_intensity_scale_avail
, NULL
, 0);
1131 static struct attribute
*ltr501_attributes
[] = {
1132 &iio_dev_attr_in_proximity_scale_available
.dev_attr
.attr
,
1133 &iio_dev_attr_in_intensity_scale_available
.dev_attr
.attr
,
1134 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
1135 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
1139 static struct attribute
*ltr301_attributes
[] = {
1140 &iio_dev_attr_in_intensity_scale_available
.dev_attr
.attr
,
1141 &iio_const_attr_integration_time_available
.dev_attr
.attr
,
1142 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
1146 static const struct attribute_group ltr501_attribute_group
= {
1147 .attrs
= ltr501_attributes
,
1150 static const struct attribute_group ltr301_attribute_group
= {
1151 .attrs
= ltr301_attributes
,
1154 static const struct iio_info ltr501_info_no_irq
= {
1155 .read_raw
= ltr501_read_raw
,
1156 .write_raw
= ltr501_write_raw
,
1157 .attrs
= <r501_attribute_group
,
1160 static const struct iio_info ltr501_info
= {
1161 .read_raw
= ltr501_read_raw
,
1162 .write_raw
= ltr501_write_raw
,
1163 .attrs
= <r501_attribute_group
,
1164 .read_event_value
= <r501_read_event
,
1165 .write_event_value
= <r501_write_event
,
1166 .read_event_config
= <r501_read_event_config
,
1167 .write_event_config
= <r501_write_event_config
,
1170 static const struct iio_info ltr301_info_no_irq
= {
1171 .read_raw
= ltr501_read_raw
,
1172 .write_raw
= ltr501_write_raw
,
1173 .attrs
= <r301_attribute_group
,
1176 static const struct iio_info ltr301_info
= {
1177 .read_raw
= ltr501_read_raw
,
1178 .write_raw
= ltr501_write_raw
,
1179 .attrs
= <r301_attribute_group
,
1180 .read_event_value
= <r501_read_event
,
1181 .write_event_value
= <r501_write_event
,
1182 .read_event_config
= <r501_read_event_config
,
1183 .write_event_config
= <r501_write_event_config
,
1186 static struct ltr501_chip_info ltr501_chip_info_tbl
[] = {
1189 .als_gain
= ltr501_als_gain_tbl
,
1190 .als_gain_tbl_size
= ARRAY_SIZE(ltr501_als_gain_tbl
),
1191 .ps_gain
= ltr501_ps_gain_tbl
,
1192 .ps_gain_tbl_size
= ARRAY_SIZE(ltr501_ps_gain_tbl
),
1193 .als_mode_active
= BIT(0) | BIT(1),
1194 .als_gain_mask
= BIT(3),
1195 .als_gain_shift
= 3,
1196 .info
= <r501_info
,
1197 .info_no_irq
= <r501_info_no_irq
,
1198 .channels
= ltr501_channels
,
1199 .no_channels
= ARRAY_SIZE(ltr501_channels
),
1203 .als_gain
= ltr559_als_gain_tbl
,
1204 .als_gain_tbl_size
= ARRAY_SIZE(ltr559_als_gain_tbl
),
1205 .ps_gain
= ltr559_ps_gain_tbl
,
1206 .ps_gain_tbl_size
= ARRAY_SIZE(ltr559_ps_gain_tbl
),
1207 .als_mode_active
= BIT(1),
1208 .als_gain_mask
= BIT(2) | BIT(3) | BIT(4),
1209 .als_gain_shift
= 2,
1210 .info
= <r501_info
,
1211 .info_no_irq
= <r501_info_no_irq
,
1212 .channels
= ltr501_channels
,
1213 .no_channels
= ARRAY_SIZE(ltr501_channels
),
1217 .als_gain
= ltr501_als_gain_tbl
,
1218 .als_gain_tbl_size
= ARRAY_SIZE(ltr501_als_gain_tbl
),
1219 .als_mode_active
= BIT(0) | BIT(1),
1220 .als_gain_mask
= BIT(3),
1221 .als_gain_shift
= 3,
1222 .info
= <r301_info
,
1223 .info_no_irq
= <r301_info_no_irq
,
1224 .channels
= ltr301_channels
,
1225 .no_channels
= ARRAY_SIZE(ltr301_channels
),
1229 static int ltr501_write_contr(struct ltr501_data
*data
, u8 als_val
, u8 ps_val
)
1233 ret
= regmap_write(data
->regmap
, LTR501_ALS_CONTR
, als_val
);
1237 return regmap_write(data
->regmap
, LTR501_PS_CONTR
, ps_val
);
1240 static irqreturn_t
ltr501_trigger_handler(int irq
, void *p
)
1242 struct iio_poll_func
*pf
= p
;
1243 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1244 struct ltr501_data
*data
= iio_priv(indio_dev
);
1251 memset(buf
, 0, sizeof(buf
));
1253 /* figure out which data needs to be ready */
1254 if (test_bit(0, indio_dev
->active_scan_mask
) ||
1255 test_bit(1, indio_dev
->active_scan_mask
))
1256 mask
|= LTR501_STATUS_ALS_RDY
;
1257 if (test_bit(2, indio_dev
->active_scan_mask
))
1258 mask
|= LTR501_STATUS_PS_RDY
;
1260 ret
= ltr501_drdy(data
, mask
);
1264 if (mask
& LTR501_STATUS_ALS_RDY
) {
1265 ret
= regmap_bulk_read(data
->regmap
, LTR501_ALS_DATA1
,
1266 (u8
*)als_buf
, sizeof(als_buf
));
1269 if (test_bit(0, indio_dev
->active_scan_mask
))
1270 buf
[j
++] = le16_to_cpu(als_buf
[1]);
1271 if (test_bit(1, indio_dev
->active_scan_mask
))
1272 buf
[j
++] = le16_to_cpu(als_buf
[0]);
1275 if (mask
& LTR501_STATUS_PS_RDY
) {
1276 ret
= regmap_bulk_read(data
->regmap
, LTR501_PS_DATA
,
1280 buf
[j
++] = psdata
& LTR501_PS_DATA_MASK
;
1283 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
1284 iio_get_time_ns(indio_dev
));
1287 iio_trigger_notify_done(indio_dev
->trig
);
1292 static irqreturn_t
ltr501_interrupt_handler(int irq
, void *private)
1294 struct iio_dev
*indio_dev
= private;
1295 struct ltr501_data
*data
= iio_priv(indio_dev
);
1298 ret
= regmap_read(data
->regmap
, LTR501_ALS_PS_STATUS
, &status
);
1300 dev_err(&data
->client
->dev
,
1301 "irq read int reg failed\n");
1305 if (status
& LTR501_STATUS_ALS_INTR
)
1306 iio_push_event(indio_dev
,
1307 IIO_UNMOD_EVENT_CODE(IIO_INTENSITY
, 0,
1310 iio_get_time_ns(indio_dev
));
1312 if (status
& LTR501_STATUS_PS_INTR
)
1313 iio_push_event(indio_dev
,
1314 IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 0,
1317 iio_get_time_ns(indio_dev
));
1322 static int ltr501_init(struct ltr501_data
*data
)
1326 ret
= regmap_read(data
->regmap
, LTR501_ALS_CONTR
, &status
);
1330 data
->als_contr
= status
| data
->chip_info
->als_mode_active
;
1332 ret
= regmap_read(data
->regmap
, LTR501_PS_CONTR
, &status
);
1336 data
->ps_contr
= status
| LTR501_CONTR_ACTIVE
;
1338 ret
= ltr501_read_intr_prst(data
, IIO_INTENSITY
, &data
->als_period
);
1342 ret
= ltr501_read_intr_prst(data
, IIO_PROXIMITY
, &data
->ps_period
);
1346 return ltr501_write_contr(data
, data
->als_contr
, data
->ps_contr
);
1349 static bool ltr501_is_volatile_reg(struct device
*dev
, unsigned int reg
)
1352 case LTR501_ALS_DATA1
:
1353 case LTR501_ALS_DATA0
:
1354 case LTR501_ALS_PS_STATUS
:
1355 case LTR501_PS_DATA
:
1362 static struct regmap_config ltr501_regmap_config
= {
1363 .name
= LTR501_REGMAP_NAME
,
1366 .max_register
= LTR501_MAX_REG
,
1367 .cache_type
= REGCACHE_RBTREE
,
1368 .volatile_reg
= ltr501_is_volatile_reg
,
1371 static int ltr501_powerdown(struct ltr501_data
*data
)
1373 return ltr501_write_contr(data
, data
->als_contr
&
1374 ~data
->chip_info
->als_mode_active
,
1375 data
->ps_contr
& ~LTR501_CONTR_ACTIVE
);
1378 static const char *ltr501_match_acpi_device(struct device
*dev
, int *chip_idx
)
1380 const struct acpi_device_id
*id
;
1382 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1385 *chip_idx
= id
->driver_data
;
1386 return dev_name(dev
);
1389 static int ltr501_probe(struct i2c_client
*client
,
1390 const struct i2c_device_id
*id
)
1392 struct ltr501_data
*data
;
1393 struct iio_dev
*indio_dev
;
1394 struct regmap
*regmap
;
1395 int ret
, partid
, chip_idx
= 0;
1396 const char *name
= NULL
;
1398 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1402 regmap
= devm_regmap_init_i2c(client
, <r501_regmap_config
);
1403 if (IS_ERR(regmap
)) {
1404 dev_err(&client
->dev
, "Regmap initialization failed.\n");
1405 return PTR_ERR(regmap
);
1408 data
= iio_priv(indio_dev
);
1409 i2c_set_clientdata(client
, indio_dev
);
1410 data
->client
= client
;
1411 data
->regmap
= regmap
;
1412 mutex_init(&data
->lock_als
);
1413 mutex_init(&data
->lock_ps
);
1415 data
->reg_it
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1417 if (IS_ERR(data
->reg_it
)) {
1418 dev_err(&client
->dev
, "Integ time reg field init failed.\n");
1419 return PTR_ERR(data
->reg_it
);
1422 data
->reg_als_intr
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1423 reg_field_als_intr
);
1424 if (IS_ERR(data
->reg_als_intr
)) {
1425 dev_err(&client
->dev
, "ALS intr mode reg field init failed\n");
1426 return PTR_ERR(data
->reg_als_intr
);
1429 data
->reg_ps_intr
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1431 if (IS_ERR(data
->reg_ps_intr
)) {
1432 dev_err(&client
->dev
, "PS intr mode reg field init failed.\n");
1433 return PTR_ERR(data
->reg_ps_intr
);
1436 data
->reg_als_rate
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1437 reg_field_als_rate
);
1438 if (IS_ERR(data
->reg_als_rate
)) {
1439 dev_err(&client
->dev
, "ALS samp rate field init failed.\n");
1440 return PTR_ERR(data
->reg_als_rate
);
1443 data
->reg_ps_rate
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1445 if (IS_ERR(data
->reg_ps_rate
)) {
1446 dev_err(&client
->dev
, "PS samp rate field init failed.\n");
1447 return PTR_ERR(data
->reg_ps_rate
);
1450 data
->reg_als_prst
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1451 reg_field_als_prst
);
1452 if (IS_ERR(data
->reg_als_prst
)) {
1453 dev_err(&client
->dev
, "ALS prst reg field init failed\n");
1454 return PTR_ERR(data
->reg_als_prst
);
1457 data
->reg_ps_prst
= devm_regmap_field_alloc(&client
->dev
, regmap
,
1459 if (IS_ERR(data
->reg_ps_prst
)) {
1460 dev_err(&client
->dev
, "PS prst reg field init failed.\n");
1461 return PTR_ERR(data
->reg_ps_prst
);
1464 ret
= regmap_read(data
->regmap
, LTR501_PART_ID
, &partid
);
1470 chip_idx
= id
->driver_data
;
1471 } else if (ACPI_HANDLE(&client
->dev
)) {
1472 name
= ltr501_match_acpi_device(&client
->dev
, &chip_idx
);
1477 data
->chip_info
= <r501_chip_info_tbl
[chip_idx
];
1479 if ((partid
>> 4) != data
->chip_info
->partid
)
1482 indio_dev
->dev
.parent
= &client
->dev
;
1483 indio_dev
->info
= data
->chip_info
->info
;
1484 indio_dev
->channels
= data
->chip_info
->channels
;
1485 indio_dev
->num_channels
= data
->chip_info
->no_channels
;
1486 indio_dev
->name
= name
;
1487 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1489 ret
= ltr501_init(data
);
1493 if (client
->irq
> 0) {
1494 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1495 NULL
, ltr501_interrupt_handler
,
1496 IRQF_TRIGGER_FALLING
|
1498 "ltr501_thresh_event",
1501 dev_err(&client
->dev
, "request irq (%d) failed\n",
1506 indio_dev
->info
= data
->chip_info
->info_no_irq
;
1509 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
1510 ltr501_trigger_handler
, NULL
);
1512 goto powerdown_on_error
;
1514 ret
= iio_device_register(indio_dev
);
1516 goto error_unreg_buffer
;
1521 iio_triggered_buffer_cleanup(indio_dev
);
1523 ltr501_powerdown(data
);
1527 static int ltr501_remove(struct i2c_client
*client
)
1529 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1531 iio_device_unregister(indio_dev
);
1532 iio_triggered_buffer_cleanup(indio_dev
);
1533 ltr501_powerdown(iio_priv(indio_dev
));
1538 #ifdef CONFIG_PM_SLEEP
1539 static int ltr501_suspend(struct device
*dev
)
1541 struct ltr501_data
*data
= iio_priv(i2c_get_clientdata(
1542 to_i2c_client(dev
)));
1543 return ltr501_powerdown(data
);
1546 static int ltr501_resume(struct device
*dev
)
1548 struct ltr501_data
*data
= iio_priv(i2c_get_clientdata(
1549 to_i2c_client(dev
)));
1551 return ltr501_write_contr(data
, data
->als_contr
,
1556 static SIMPLE_DEV_PM_OPS(ltr501_pm_ops
, ltr501_suspend
, ltr501_resume
);
1558 static const struct acpi_device_id ltr_acpi_match
[] = {
1559 {"LTER0501", ltr501
},
1560 {"LTER0559", ltr559
},
1561 {"LTER0301", ltr301
},
1564 MODULE_DEVICE_TABLE(acpi
, ltr_acpi_match
);
1566 static const struct i2c_device_id ltr501_id
[] = {
1567 { "ltr501", ltr501
},
1568 { "ltr559", ltr559
},
1569 { "ltr301", ltr301
},
1572 MODULE_DEVICE_TABLE(i2c
, ltr501_id
);
1574 static struct i2c_driver ltr501_driver
= {
1576 .name
= LTR501_DRV_NAME
,
1577 .pm
= <r501_pm_ops
,
1578 .acpi_match_table
= ACPI_PTR(ltr_acpi_match
),
1580 .probe
= ltr501_probe
,
1581 .remove
= ltr501_remove
,
1582 .id_table
= ltr501_id
,
1585 module_i2c_driver(ltr501_driver
);
1587 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
1588 MODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver");
1589 MODULE_LICENSE("GPL");