1 // SPDX-License-Identifier: GPL-2.0-only
3 * RPR-0521 ROHM Ambient Light and Proximity Sensor
5 * Copyright (c) 2015, Intel Corporation.
7 * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
9 * TODO: illuminance channel
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
15 #include <linux/regmap.h>
16 #include <linux/delay.h>
17 #include <linux/acpi.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/pm_runtime.h>
27 #define RPR0521_REG_SYSTEM_CTRL 0x40
28 #define RPR0521_REG_MODE_CTRL 0x41
29 #define RPR0521_REG_ALS_CTRL 0x42
30 #define RPR0521_REG_PXS_CTRL 0x43
31 #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
32 #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
33 #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
34 #define RPR0521_REG_INTERRUPT 0x4A
35 #define RPR0521_REG_PS_OFFSET_LSB 0x53
36 #define RPR0521_REG_ID 0x92
38 #define RPR0521_MODE_ALS_MASK BIT(7)
39 #define RPR0521_MODE_PXS_MASK BIT(6)
40 #define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
41 #define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
42 #define RPR0521_ALS_DATA0_GAIN_SHIFT 4
43 #define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
44 #define RPR0521_ALS_DATA1_GAIN_SHIFT 2
45 #define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
46 #define RPR0521_PXS_GAIN_SHIFT 4
47 #define RPR0521_PXS_PERSISTENCE_MASK GENMASK(3, 0)
48 #define RPR0521_INTERRUPT_INT_TRIG_PS_MASK BIT(0)
49 #define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK BIT(1)
50 #define RPR0521_INTERRUPT_INT_REASSERT_MASK BIT(3)
51 #define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK BIT(6)
52 #define RPR0521_INTERRUPT_PS_INT_STATUS_MASK BIT(7)
54 #define RPR0521_MODE_ALS_ENABLE BIT(7)
55 #define RPR0521_MODE_ALS_DISABLE 0x00
56 #define RPR0521_MODE_PXS_ENABLE BIT(6)
57 #define RPR0521_MODE_PXS_DISABLE 0x00
58 #define RPR0521_PXS_PERSISTENCE_DRDY 0x00
60 #define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE BIT(0)
61 #define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE 0x00
62 #define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE BIT(1)
63 #define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE 0x00
64 #define RPR0521_INTERRUPT_INT_REASSERT_ENABLE BIT(3)
65 #define RPR0521_INTERRUPT_INT_REASSERT_DISABLE 0x00
67 #define RPR0521_MANUFACT_ID 0xE0
68 #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
70 #define RPR0521_DRV_NAME "RPR0521"
71 #define RPR0521_IRQ_NAME "rpr0521_event"
72 #define RPR0521_REGMAP_NAME "rpr0521_regmap"
74 #define RPR0521_SLEEP_DELAY_MS 2000
76 #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
77 #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
84 static const struct rpr0521_gain rpr0521_als_gain
[4] = {
91 static const struct rpr0521_gain rpr0521_pxs_gain
[3] = {
97 enum rpr0521_channel
{
99 RPR0521_CHAN_ALS_DATA0
,
100 RPR0521_CHAN_ALS_DATA1
,
103 struct rpr0521_reg_desc
{
108 static const struct rpr0521_reg_desc rpr0521_data_reg
[] = {
109 [RPR0521_CHAN_PXS
] = {
110 .address
= RPR0521_REG_PXS_DATA
,
111 .device_mask
= RPR0521_MODE_PXS_MASK
,
113 [RPR0521_CHAN_ALS_DATA0
] = {
114 .address
= RPR0521_REG_ALS_DATA0
,
115 .device_mask
= RPR0521_MODE_ALS_MASK
,
117 [RPR0521_CHAN_ALS_DATA1
] = {
118 .address
= RPR0521_REG_ALS_DATA1
,
119 .device_mask
= RPR0521_MODE_ALS_MASK
,
123 static const struct rpr0521_gain_info
{
127 const struct rpr0521_gain
*gain
;
130 [RPR0521_CHAN_PXS
] = {
131 .reg
= RPR0521_REG_PXS_CTRL
,
132 .mask
= RPR0521_PXS_GAIN_MASK
,
133 .shift
= RPR0521_PXS_GAIN_SHIFT
,
134 .gain
= rpr0521_pxs_gain
,
135 .size
= ARRAY_SIZE(rpr0521_pxs_gain
),
137 [RPR0521_CHAN_ALS_DATA0
] = {
138 .reg
= RPR0521_REG_ALS_CTRL
,
139 .mask
= RPR0521_ALS_DATA0_GAIN_MASK
,
140 .shift
= RPR0521_ALS_DATA0_GAIN_SHIFT
,
141 .gain
= rpr0521_als_gain
,
142 .size
= ARRAY_SIZE(rpr0521_als_gain
),
144 [RPR0521_CHAN_ALS_DATA1
] = {
145 .reg
= RPR0521_REG_ALS_CTRL
,
146 .mask
= RPR0521_ALS_DATA1_GAIN_MASK
,
147 .shift
= RPR0521_ALS_DATA1_GAIN_SHIFT
,
148 .gain
= rpr0521_als_gain
,
149 .size
= ARRAY_SIZE(rpr0521_als_gain
),
153 struct rpr0521_samp_freq
{
160 static const struct rpr0521_samp_freq rpr0521_samp_freq_i
[13] = {
161 /* {ALS, PXS}, W==currently writable option */
162 {0, 0, 0, 0}, /* W0000, 0=standby */
163 {0, 0, 100, 0}, /* 0001 */
164 {0, 0, 25, 0}, /* 0010 */
165 {0, 0, 10, 0}, /* 0011 */
166 {0, 0, 2, 500000}, /* 0100 */
167 {10, 0, 20, 0}, /* 0101 */
168 {10, 0, 10, 0}, /* W0110 */
169 {10, 0, 2, 500000}, /* 0111 */
170 {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
171 {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
172 {2, 500000, 0, 0}, /* 1010, high sensitivity mode */
173 {2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
174 {20, 0, 20, 0} /* 1100, ALS_data x 0.5, see specification P.18 */
177 struct rpr0521_data
{
178 struct i2c_client
*client
;
180 /* protect device params updates (e.g state, gain) */
183 /* device active status */
187 struct iio_trigger
*drdy_trigger0
;
190 /* optimize runtime pm ops - enable/disable device only if needed */
196 struct regmap
*regmap
;
199 * Ensure correct naturally aligned timestamp.
200 * Note that the read will put garbage data into
201 * the padding but this should not be a problem
210 static IIO_CONST_ATTR(in_intensity_scale_available
, RPR0521_ALS_SCALE_AVAIL
);
211 static IIO_CONST_ATTR(in_proximity_scale_available
, RPR0521_PXS_SCALE_AVAIL
);
214 * Start with easy freq first, whole table of freq combinations is more
217 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
219 static struct attribute
*rpr0521_attributes
[] = {
220 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
221 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
222 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
226 static const struct attribute_group rpr0521_attribute_group
= {
227 .attrs
= rpr0521_attributes
,
230 /* Order of the channel data in buffer */
231 enum rpr0521_scan_index_order
{
232 RPR0521_CHAN_INDEX_PXS
,
233 RPR0521_CHAN_INDEX_BOTH
,
234 RPR0521_CHAN_INDEX_IR
,
237 static const unsigned long rpr0521_available_scan_masks
[] = {
238 BIT(RPR0521_CHAN_INDEX_PXS
) | BIT(RPR0521_CHAN_INDEX_BOTH
) |
239 BIT(RPR0521_CHAN_INDEX_IR
),
243 static const struct iio_chan_spec rpr0521_channels
[] = {
245 .type
= IIO_PROXIMITY
,
246 .address
= RPR0521_CHAN_PXS
,
247 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
248 BIT(IIO_CHAN_INFO_OFFSET
) |
249 BIT(IIO_CHAN_INFO_SCALE
),
250 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
251 .scan_index
= RPR0521_CHAN_INDEX_PXS
,
256 .endianness
= IIO_LE
,
260 .type
= IIO_INTENSITY
,
262 .address
= RPR0521_CHAN_ALS_DATA0
,
263 .channel2
= IIO_MOD_LIGHT_BOTH
,
264 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
265 BIT(IIO_CHAN_INFO_SCALE
),
266 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
267 .scan_index
= RPR0521_CHAN_INDEX_BOTH
,
272 .endianness
= IIO_LE
,
276 .type
= IIO_INTENSITY
,
278 .address
= RPR0521_CHAN_ALS_DATA1
,
279 .channel2
= IIO_MOD_LIGHT_IR
,
280 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
281 BIT(IIO_CHAN_INFO_SCALE
),
282 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
283 .scan_index
= RPR0521_CHAN_INDEX_IR
,
288 .endianness
= IIO_LE
,
293 static int rpr0521_als_enable(struct rpr0521_data
*data
, u8 status
)
297 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
298 RPR0521_MODE_ALS_MASK
,
303 if (status
& RPR0521_MODE_ALS_MASK
)
304 data
->als_dev_en
= true;
306 data
->als_dev_en
= false;
311 static int rpr0521_pxs_enable(struct rpr0521_data
*data
, u8 status
)
315 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
316 RPR0521_MODE_PXS_MASK
,
321 if (status
& RPR0521_MODE_PXS_MASK
)
322 data
->pxs_dev_en
= true;
324 data
->pxs_dev_en
= false;
330 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
332 * @data: rpr0521 device private data
333 * @on: state to be set for devices in @device_mask
334 * @device_mask: bitmask specifying for which device we need to update @on state
336 * Calls for this function must be balanced so that each ON should have matching
337 * OFF. Otherwise pm usage_count gets out of sync.
339 static int rpr0521_set_power_state(struct rpr0521_data
*data
, bool on
,
345 if (device_mask
& RPR0521_MODE_ALS_MASK
) {
346 data
->als_ps_need_en
= on
;
347 data
->als_need_dis
= !on
;
350 if (device_mask
& RPR0521_MODE_PXS_MASK
) {
351 data
->pxs_ps_need_en
= on
;
352 data
->pxs_need_dis
= !on
;
356 * On: _resume() is called only when we are suspended
357 * Off: _suspend() is called after delay if _resume() is not
358 * called before that.
359 * Note: If either measurement is re-enabled before _suspend(),
360 * both stay enabled until _suspend().
363 ret
= pm_runtime_get_sync(&data
->client
->dev
);
365 pm_runtime_mark_last_busy(&data
->client
->dev
);
366 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
369 dev_err(&data
->client
->dev
,
370 "Failed: rpr0521_set_power_state for %d, ret %d\n",
373 pm_runtime_put_noidle(&data
->client
->dev
);
379 /* If _resume() was not called, enable measurement now. */
380 if (data
->als_ps_need_en
) {
381 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
384 data
->als_ps_need_en
= false;
387 if (data
->pxs_ps_need_en
) {
388 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
391 data
->pxs_ps_need_en
= false;
398 /* Interrupt register tells if this sensor caused the interrupt or not. */
399 static inline bool rpr0521_is_triggered(struct rpr0521_data
*data
)
404 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, ®
);
406 return false; /* Reg read failed. */
408 (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK
|
409 RPR0521_INTERRUPT_PS_INT_STATUS_MASK
))
412 return false; /* Int not from this sensor. */
415 /* IRQ to trigger handler */
416 static irqreturn_t
rpr0521_drdy_irq_handler(int irq
, void *private)
418 struct iio_dev
*indio_dev
= private;
419 struct rpr0521_data
*data
= iio_priv(indio_dev
);
421 data
->irq_timestamp
= iio_get_time_ns(indio_dev
);
423 * We need to wake the thread to read the interrupt reg. It
424 * is not possible to do that here because regmap_read takes a
428 return IRQ_WAKE_THREAD
;
431 static irqreturn_t
rpr0521_drdy_irq_thread(int irq
, void *private)
433 struct iio_dev
*indio_dev
= private;
434 struct rpr0521_data
*data
= iio_priv(indio_dev
);
436 if (rpr0521_is_triggered(data
)) {
437 iio_trigger_poll_chained(data
->drdy_trigger0
);
444 static irqreturn_t
rpr0521_trigger_consumer_store_time(int irq
, void *p
)
446 struct iio_poll_func
*pf
= p
;
447 struct iio_dev
*indio_dev
= pf
->indio_dev
;
449 /* Other trigger polls store time here. */
450 if (!iio_trigger_using_own(indio_dev
))
451 pf
->timestamp
= iio_get_time_ns(indio_dev
);
453 return IRQ_WAKE_THREAD
;
456 static irqreturn_t
rpr0521_trigger_consumer_handler(int irq
, void *p
)
458 struct iio_poll_func
*pf
= p
;
459 struct iio_dev
*indio_dev
= pf
->indio_dev
;
460 struct rpr0521_data
*data
= iio_priv(indio_dev
);
463 /* Use irq timestamp when reasonable. */
464 if (iio_trigger_using_own(indio_dev
) && data
->irq_timestamp
) {
465 pf
->timestamp
= data
->irq_timestamp
;
466 data
->irq_timestamp
= 0;
468 /* Other chained trigger polls get timestamp only here. */
470 pf
->timestamp
= iio_get_time_ns(indio_dev
);
472 err
= regmap_bulk_read(data
->regmap
, RPR0521_REG_PXS_DATA
,
474 (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */
476 iio_push_to_buffers_with_timestamp(indio_dev
,
477 &data
->scan
, pf
->timestamp
);
479 dev_err(&data
->client
->dev
,
480 "Trigger consumer can't read from sensor.\n");
483 iio_trigger_notify_done(indio_dev
->trig
);
488 static int rpr0521_write_int_enable(struct rpr0521_data
*data
)
492 /* Interrupt after each measurement */
493 err
= regmap_update_bits(data
->regmap
, RPR0521_REG_PXS_CTRL
,
494 RPR0521_PXS_PERSISTENCE_MASK
,
495 RPR0521_PXS_PERSISTENCE_DRDY
);
497 dev_err(&data
->client
->dev
, "PS control reg write fail.\n");
501 /* Ignore latch and mode because of drdy */
502 err
= regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
503 RPR0521_INTERRUPT_INT_REASSERT_DISABLE
|
504 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
505 RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
508 dev_err(&data
->client
->dev
, "Interrupt setup write fail.\n");
515 static int rpr0521_write_int_disable(struct rpr0521_data
*data
)
517 /* Don't care of clearing mode, assert and latch. */
518 return regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
519 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
520 RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
525 * Trigger producer enable / disable. Note that there will be trigs only when
526 * measurement data is ready to be read.
528 static int rpr0521_pxs_drdy_set_state(struct iio_trigger
*trigger
,
531 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trigger
);
532 struct rpr0521_data
*data
= iio_priv(indio_dev
);
536 err
= rpr0521_write_int_enable(data
);
538 err
= rpr0521_write_int_disable(data
);
540 dev_err(&data
->client
->dev
, "rpr0521_pxs_drdy_set_state failed\n");
545 static const struct iio_trigger_ops rpr0521_trigger_ops
= {
546 .set_trigger_state
= rpr0521_pxs_drdy_set_state
,
550 static int rpr0521_buffer_preenable(struct iio_dev
*indio_dev
)
553 struct rpr0521_data
*data
= iio_priv(indio_dev
);
555 mutex_lock(&data
->lock
);
556 err
= rpr0521_set_power_state(data
, true,
557 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
558 mutex_unlock(&data
->lock
);
560 dev_err(&data
->client
->dev
, "_buffer_preenable fail\n");
565 static int rpr0521_buffer_postdisable(struct iio_dev
*indio_dev
)
568 struct rpr0521_data
*data
= iio_priv(indio_dev
);
570 mutex_lock(&data
->lock
);
571 err
= rpr0521_set_power_state(data
, false,
572 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
573 mutex_unlock(&data
->lock
);
575 dev_err(&data
->client
->dev
, "_buffer_postdisable fail\n");
580 static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops
= {
581 .preenable
= rpr0521_buffer_preenable
,
582 .postdisable
= rpr0521_buffer_postdisable
,
585 static int rpr0521_get_gain(struct rpr0521_data
*data
, int chan
,
590 ret
= regmap_read(data
->regmap
, rpr0521_gain
[chan
].reg
, ®
);
594 idx
= (rpr0521_gain
[chan
].mask
& reg
) >> rpr0521_gain
[chan
].shift
;
595 *val
= rpr0521_gain
[chan
].gain
[idx
].scale
;
596 *val2
= rpr0521_gain
[chan
].gain
[idx
].uscale
;
601 static int rpr0521_set_gain(struct rpr0521_data
*data
, int chan
,
604 int i
, idx
= -EINVAL
;
607 for (i
= 0; i
< rpr0521_gain
[chan
].size
; i
++)
608 if (val
== rpr0521_gain
[chan
].gain
[i
].scale
&&
609 val2
== rpr0521_gain
[chan
].gain
[i
].uscale
) {
617 return regmap_update_bits(data
->regmap
, rpr0521_gain
[chan
].reg
,
618 rpr0521_gain
[chan
].mask
,
619 idx
<< rpr0521_gain
[chan
].shift
);
622 static int rpr0521_read_samp_freq(struct rpr0521_data
*data
,
623 enum iio_chan_type chan_type
,
628 ret
= regmap_read(data
->regmap
, RPR0521_REG_MODE_CTRL
, ®
);
632 reg
&= RPR0521_MODE_MEAS_TIME_MASK
;
633 if (reg
>= ARRAY_SIZE(rpr0521_samp_freq_i
))
638 *val
= rpr0521_samp_freq_i
[reg
].als_hz
;
639 *val2
= rpr0521_samp_freq_i
[reg
].als_uhz
;
643 *val
= rpr0521_samp_freq_i
[reg
].pxs_hz
;
644 *val2
= rpr0521_samp_freq_i
[reg
].pxs_uhz
;
652 static int rpr0521_write_samp_freq_common(struct rpr0521_data
*data
,
653 enum iio_chan_type chan_type
,
660 * both pxs and als are setup only to same freq because of simplicity
682 return regmap_update_bits(data
->regmap
,
683 RPR0521_REG_MODE_CTRL
,
684 RPR0521_MODE_MEAS_TIME_MASK
,
688 static int rpr0521_read_ps_offset(struct rpr0521_data
*data
, int *offset
)
693 ret
= regmap_bulk_read(data
->regmap
,
694 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
697 dev_err(&data
->client
->dev
, "Failed to read PS OFFSET register\n");
700 *offset
= le16_to_cpu(buffer
);
705 static int rpr0521_write_ps_offset(struct rpr0521_data
*data
, int offset
)
710 buffer
= cpu_to_le16(offset
& 0x3ff);
711 ret
= regmap_raw_write(data
->regmap
,
712 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
715 dev_err(&data
->client
->dev
, "Failed to write PS OFFSET register\n");
722 static int rpr0521_read_raw(struct iio_dev
*indio_dev
,
723 struct iio_chan_spec
const *chan
, int *val
,
724 int *val2
, long mask
)
726 struct rpr0521_data
*data
= iio_priv(indio_dev
);
733 case IIO_CHAN_INFO_RAW
:
734 if (chan
->type
!= IIO_INTENSITY
&& chan
->type
!= IIO_PROXIMITY
)
737 busy
= iio_device_claim_direct_mode(indio_dev
);
741 device_mask
= rpr0521_data_reg
[chan
->address
].device_mask
;
743 mutex_lock(&data
->lock
);
744 ret
= rpr0521_set_power_state(data
, true, device_mask
);
746 goto rpr0521_read_raw_out
;
748 ret
= regmap_bulk_read(data
->regmap
,
749 rpr0521_data_reg
[chan
->address
].address
,
750 &raw_data
, sizeof(raw_data
));
752 rpr0521_set_power_state(data
, false, device_mask
);
753 goto rpr0521_read_raw_out
;
756 ret
= rpr0521_set_power_state(data
, false, device_mask
);
758 rpr0521_read_raw_out
:
759 mutex_unlock(&data
->lock
);
760 iio_device_release_direct_mode(indio_dev
);
764 *val
= le16_to_cpu(raw_data
);
768 case IIO_CHAN_INFO_SCALE
:
769 mutex_lock(&data
->lock
);
770 ret
= rpr0521_get_gain(data
, chan
->address
, val
, val2
);
771 mutex_unlock(&data
->lock
);
775 return IIO_VAL_INT_PLUS_MICRO
;
777 case IIO_CHAN_INFO_SAMP_FREQ
:
778 mutex_lock(&data
->lock
);
779 ret
= rpr0521_read_samp_freq(data
, chan
->type
, val
, val2
);
780 mutex_unlock(&data
->lock
);
784 return IIO_VAL_INT_PLUS_MICRO
;
786 case IIO_CHAN_INFO_OFFSET
:
787 mutex_lock(&data
->lock
);
788 ret
= rpr0521_read_ps_offset(data
, val
);
789 mutex_unlock(&data
->lock
);
800 static int rpr0521_write_raw(struct iio_dev
*indio_dev
,
801 struct iio_chan_spec
const *chan
, int val
,
804 struct rpr0521_data
*data
= iio_priv(indio_dev
);
808 case IIO_CHAN_INFO_SCALE
:
809 mutex_lock(&data
->lock
);
810 ret
= rpr0521_set_gain(data
, chan
->address
, val
, val2
);
811 mutex_unlock(&data
->lock
);
815 case IIO_CHAN_INFO_SAMP_FREQ
:
816 mutex_lock(&data
->lock
);
817 ret
= rpr0521_write_samp_freq_common(data
, chan
->type
,
819 mutex_unlock(&data
->lock
);
823 case IIO_CHAN_INFO_OFFSET
:
824 mutex_lock(&data
->lock
);
825 ret
= rpr0521_write_ps_offset(data
, val
);
826 mutex_unlock(&data
->lock
);
835 static const struct iio_info rpr0521_info
= {
836 .read_raw
= rpr0521_read_raw
,
837 .write_raw
= rpr0521_write_raw
,
838 .attrs
= &rpr0521_attribute_group
,
841 static int rpr0521_init(struct rpr0521_data
*data
)
846 ret
= regmap_read(data
->regmap
, RPR0521_REG_ID
, &id
);
848 dev_err(&data
->client
->dev
, "Failed to read REG_ID register\n");
852 if (id
!= RPR0521_MANUFACT_ID
) {
853 dev_err(&data
->client
->dev
, "Wrong id, got %x, expected %x\n",
854 id
, RPR0521_MANUFACT_ID
);
858 /* set default measurement time - 100 ms for both ALS and PS */
859 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
860 RPR0521_MODE_MEAS_TIME_MASK
,
861 RPR0521_DEFAULT_MEAS_TIME
);
863 pr_err("regmap_update_bits returned %d\n", ret
);
868 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
871 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
876 data
->irq_timestamp
= 0;
881 static int rpr0521_poweroff(struct rpr0521_data
*data
)
886 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
887 RPR0521_MODE_ALS_MASK
|
888 RPR0521_MODE_PXS_MASK
,
889 RPR0521_MODE_ALS_DISABLE
|
890 RPR0521_MODE_PXS_DISABLE
);
894 data
->als_dev_en
= false;
895 data
->pxs_dev_en
= false;
898 * Int pin keeps state after power off. Set pin to high impedance
899 * mode to prevent power drain.
901 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, &tmp
);
903 dev_err(&data
->client
->dev
, "Failed to reset int pin.\n");
910 static bool rpr0521_is_volatile_reg(struct device
*dev
, unsigned int reg
)
913 case RPR0521_REG_MODE_CTRL
:
914 case RPR0521_REG_ALS_CTRL
:
915 case RPR0521_REG_PXS_CTRL
:
922 static const struct regmap_config rpr0521_regmap_config
= {
923 .name
= RPR0521_REGMAP_NAME
,
928 .max_register
= RPR0521_REG_ID
,
929 .cache_type
= REGCACHE_RBTREE
,
930 .volatile_reg
= rpr0521_is_volatile_reg
,
933 static int rpr0521_probe(struct i2c_client
*client
,
934 const struct i2c_device_id
*id
)
936 struct rpr0521_data
*data
;
937 struct iio_dev
*indio_dev
;
938 struct regmap
*regmap
;
941 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
945 regmap
= devm_regmap_init_i2c(client
, &rpr0521_regmap_config
);
946 if (IS_ERR(regmap
)) {
947 dev_err(&client
->dev
, "regmap_init failed!\n");
948 return PTR_ERR(regmap
);
951 data
= iio_priv(indio_dev
);
952 i2c_set_clientdata(client
, indio_dev
);
953 data
->client
= client
;
954 data
->regmap
= regmap
;
956 mutex_init(&data
->lock
);
958 indio_dev
->info
= &rpr0521_info
;
959 indio_dev
->name
= RPR0521_DRV_NAME
;
960 indio_dev
->channels
= rpr0521_channels
;
961 indio_dev
->num_channels
= ARRAY_SIZE(rpr0521_channels
);
962 indio_dev
->modes
= INDIO_DIRECT_MODE
;
964 ret
= rpr0521_init(data
);
966 dev_err(&client
->dev
, "rpr0521 chip init failed\n");
970 ret
= pm_runtime_set_active(&client
->dev
);
974 pm_runtime_enable(&client
->dev
);
975 pm_runtime_set_autosuspend_delay(&client
->dev
, RPR0521_SLEEP_DELAY_MS
);
976 pm_runtime_use_autosuspend(&client
->dev
);
979 * If sensor write/read is needed in _probe after _use_autosuspend,
980 * sensor needs to be _resumed first using rpr0521_set_power_state().
983 /* IRQ to trigger setup */
985 /* Trigger0 producer setup */
986 data
->drdy_trigger0
= devm_iio_trigger_alloc(
987 indio_dev
->dev
.parent
,
988 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
989 if (!data
->drdy_trigger0
) {
993 data
->drdy_trigger0
->dev
.parent
= indio_dev
->dev
.parent
;
994 data
->drdy_trigger0
->ops
= &rpr0521_trigger_ops
;
995 indio_dev
->available_scan_masks
= rpr0521_available_scan_masks
;
996 iio_trigger_set_drvdata(data
->drdy_trigger0
, indio_dev
);
998 /* Ties irq to trigger producer handler. */
999 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1000 rpr0521_drdy_irq_handler
, rpr0521_drdy_irq_thread
,
1001 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1002 RPR0521_IRQ_NAME
, indio_dev
);
1004 dev_err(&client
->dev
, "request irq %d for trigger0 failed\n",
1006 goto err_pm_disable
;
1009 ret
= devm_iio_trigger_register(indio_dev
->dev
.parent
,
1010 data
->drdy_trigger0
);
1012 dev_err(&client
->dev
, "iio trigger register failed\n");
1013 goto err_pm_disable
;
1017 * Now whole pipe from physical interrupt (irq defined by
1018 * devicetree to device) to trigger0 output is set up.
1021 /* Trigger consumer setup */
1022 ret
= devm_iio_triggered_buffer_setup(indio_dev
->dev
.parent
,
1024 rpr0521_trigger_consumer_store_time
,
1025 rpr0521_trigger_consumer_handler
,
1026 &rpr0521_buffer_setup_ops
);
1028 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1029 goto err_pm_disable
;
1033 ret
= iio_device_register(indio_dev
);
1035 goto err_pm_disable
;
1040 pm_runtime_disable(&client
->dev
);
1041 pm_runtime_set_suspended(&client
->dev
);
1042 pm_runtime_put_noidle(&client
->dev
);
1044 rpr0521_poweroff(data
);
1049 static int rpr0521_remove(struct i2c_client
*client
)
1051 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1053 iio_device_unregister(indio_dev
);
1055 pm_runtime_disable(&client
->dev
);
1056 pm_runtime_set_suspended(&client
->dev
);
1057 pm_runtime_put_noidle(&client
->dev
);
1059 rpr0521_poweroff(iio_priv(indio_dev
));
1065 static int rpr0521_runtime_suspend(struct device
*dev
)
1067 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1068 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1071 mutex_lock(&data
->lock
);
1072 /* If measurements are enabled, enable them on resume */
1073 if (!data
->als_need_dis
)
1074 data
->als_ps_need_en
= data
->als_dev_en
;
1075 if (!data
->pxs_need_dis
)
1076 data
->pxs_ps_need_en
= data
->pxs_dev_en
;
1078 /* disable channels and sets {als,pxs}_dev_en to false */
1079 ret
= rpr0521_poweroff(data
);
1080 regcache_mark_dirty(data
->regmap
);
1081 mutex_unlock(&data
->lock
);
1086 static int rpr0521_runtime_resume(struct device
*dev
)
1088 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1089 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1092 regcache_sync(data
->regmap
);
1093 if (data
->als_ps_need_en
) {
1094 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
1097 data
->als_ps_need_en
= false;
1100 if (data
->pxs_ps_need_en
) {
1101 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
1104 data
->pxs_ps_need_en
= false;
1106 msleep(100); //wait for first measurement result
1112 static const struct dev_pm_ops rpr0521_pm_ops
= {
1113 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend
,
1114 rpr0521_runtime_resume
, NULL
)
1117 static const struct acpi_device_id rpr0521_acpi_match
[] = {
1121 MODULE_DEVICE_TABLE(acpi
, rpr0521_acpi_match
);
1123 static const struct i2c_device_id rpr0521_id
[] = {
1128 MODULE_DEVICE_TABLE(i2c
, rpr0521_id
);
1130 static struct i2c_driver rpr0521_driver
= {
1132 .name
= RPR0521_DRV_NAME
,
1133 .pm
= &rpr0521_pm_ops
,
1134 .acpi_match_table
= ACPI_PTR(rpr0521_acpi_match
),
1136 .probe
= rpr0521_probe
,
1137 .remove
= rpr0521_remove
,
1138 .id_table
= rpr0521_id
,
1141 module_i2c_driver(rpr0521_driver
);
1143 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1144 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
1145 MODULE_LICENSE("GPL v2");