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 static IIO_CONST_ATTR(in_intensity_scale_available
, RPR0521_ALS_SCALE_AVAIL
);
200 static IIO_CONST_ATTR(in_proximity_scale_available
, RPR0521_PXS_SCALE_AVAIL
);
203 * Start with easy freq first, whole table of freq combinations is more
206 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
208 static struct attribute
*rpr0521_attributes
[] = {
209 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
210 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
211 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
215 static const struct attribute_group rpr0521_attribute_group
= {
216 .attrs
= rpr0521_attributes
,
219 /* Order of the channel data in buffer */
220 enum rpr0521_scan_index_order
{
221 RPR0521_CHAN_INDEX_PXS
,
222 RPR0521_CHAN_INDEX_BOTH
,
223 RPR0521_CHAN_INDEX_IR
,
226 static const unsigned long rpr0521_available_scan_masks
[] = {
227 BIT(RPR0521_CHAN_INDEX_PXS
) | BIT(RPR0521_CHAN_INDEX_BOTH
) |
228 BIT(RPR0521_CHAN_INDEX_IR
),
232 static const struct iio_chan_spec rpr0521_channels
[] = {
234 .type
= IIO_PROXIMITY
,
235 .address
= RPR0521_CHAN_PXS
,
236 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
237 BIT(IIO_CHAN_INFO_OFFSET
) |
238 BIT(IIO_CHAN_INFO_SCALE
),
239 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
240 .scan_index
= RPR0521_CHAN_INDEX_PXS
,
245 .endianness
= IIO_LE
,
249 .type
= IIO_INTENSITY
,
251 .address
= RPR0521_CHAN_ALS_DATA0
,
252 .channel2
= IIO_MOD_LIGHT_BOTH
,
253 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
254 BIT(IIO_CHAN_INFO_SCALE
),
255 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
256 .scan_index
= RPR0521_CHAN_INDEX_BOTH
,
261 .endianness
= IIO_LE
,
265 .type
= IIO_INTENSITY
,
267 .address
= RPR0521_CHAN_ALS_DATA1
,
268 .channel2
= IIO_MOD_LIGHT_IR
,
269 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
270 BIT(IIO_CHAN_INFO_SCALE
),
271 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
272 .scan_index
= RPR0521_CHAN_INDEX_IR
,
277 .endianness
= IIO_LE
,
282 static int rpr0521_als_enable(struct rpr0521_data
*data
, u8 status
)
286 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
287 RPR0521_MODE_ALS_MASK
,
292 if (status
& RPR0521_MODE_ALS_MASK
)
293 data
->als_dev_en
= true;
295 data
->als_dev_en
= false;
300 static int rpr0521_pxs_enable(struct rpr0521_data
*data
, u8 status
)
304 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
305 RPR0521_MODE_PXS_MASK
,
310 if (status
& RPR0521_MODE_PXS_MASK
)
311 data
->pxs_dev_en
= true;
313 data
->pxs_dev_en
= false;
319 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
321 * @data: rpr0521 device private data
322 * @on: state to be set for devices in @device_mask
323 * @device_mask: bitmask specifying for which device we need to update @on state
325 * Calls for this function must be balanced so that each ON should have matching
326 * OFF. Otherwise pm usage_count gets out of sync.
328 static int rpr0521_set_power_state(struct rpr0521_data
*data
, bool on
,
334 if (device_mask
& RPR0521_MODE_ALS_MASK
) {
335 data
->als_ps_need_en
= on
;
336 data
->als_need_dis
= !on
;
339 if (device_mask
& RPR0521_MODE_PXS_MASK
) {
340 data
->pxs_ps_need_en
= on
;
341 data
->pxs_need_dis
= !on
;
345 * On: _resume() is called only when we are suspended
346 * Off: _suspend() is called after delay if _resume() is not
347 * called before that.
348 * Note: If either measurement is re-enabled before _suspend(),
349 * both stay enabled until _suspend().
352 ret
= pm_runtime_get_sync(&data
->client
->dev
);
354 pm_runtime_mark_last_busy(&data
->client
->dev
);
355 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
358 dev_err(&data
->client
->dev
,
359 "Failed: rpr0521_set_power_state for %d, ret %d\n",
362 pm_runtime_put_noidle(&data
->client
->dev
);
368 /* If _resume() was not called, enable measurement now. */
369 if (data
->als_ps_need_en
) {
370 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
373 data
->als_ps_need_en
= false;
376 if (data
->pxs_ps_need_en
) {
377 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
380 data
->pxs_ps_need_en
= false;
387 /* Interrupt register tells if this sensor caused the interrupt or not. */
388 static inline bool rpr0521_is_triggered(struct rpr0521_data
*data
)
393 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, ®
);
395 return false; /* Reg read failed. */
397 (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK
|
398 RPR0521_INTERRUPT_PS_INT_STATUS_MASK
))
401 return false; /* Int not from this sensor. */
404 /* IRQ to trigger handler */
405 static irqreturn_t
rpr0521_drdy_irq_handler(int irq
, void *private)
407 struct iio_dev
*indio_dev
= private;
408 struct rpr0521_data
*data
= iio_priv(indio_dev
);
410 data
->irq_timestamp
= iio_get_time_ns(indio_dev
);
412 * We need to wake the thread to read the interrupt reg. It
413 * is not possible to do that here because regmap_read takes a
417 return IRQ_WAKE_THREAD
;
420 static irqreturn_t
rpr0521_drdy_irq_thread(int irq
, void *private)
422 struct iio_dev
*indio_dev
= private;
423 struct rpr0521_data
*data
= iio_priv(indio_dev
);
425 if (rpr0521_is_triggered(data
)) {
426 iio_trigger_poll_chained(data
->drdy_trigger0
);
433 static irqreturn_t
rpr0521_trigger_consumer_store_time(int irq
, void *p
)
435 struct iio_poll_func
*pf
= p
;
436 struct iio_dev
*indio_dev
= pf
->indio_dev
;
438 /* Other trigger polls store time here. */
439 if (!iio_trigger_using_own(indio_dev
))
440 pf
->timestamp
= iio_get_time_ns(indio_dev
);
442 return IRQ_WAKE_THREAD
;
445 static irqreturn_t
rpr0521_trigger_consumer_handler(int irq
, void *p
)
447 struct iio_poll_func
*pf
= p
;
448 struct iio_dev
*indio_dev
= pf
->indio_dev
;
449 struct rpr0521_data
*data
= iio_priv(indio_dev
);
452 u8 buffer
[16]; /* 3 16-bit channels + padding + ts */
454 /* Use irq timestamp when reasonable. */
455 if (iio_trigger_using_own(indio_dev
) && data
->irq_timestamp
) {
456 pf
->timestamp
= data
->irq_timestamp
;
457 data
->irq_timestamp
= 0;
459 /* Other chained trigger polls get timestamp only here. */
461 pf
->timestamp
= iio_get_time_ns(indio_dev
);
463 err
= regmap_bulk_read(data
->regmap
, RPR0521_REG_PXS_DATA
,
465 (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */
467 iio_push_to_buffers_with_timestamp(indio_dev
,
468 buffer
, pf
->timestamp
);
470 dev_err(&data
->client
->dev
,
471 "Trigger consumer can't read from sensor.\n");
474 iio_trigger_notify_done(indio_dev
->trig
);
479 static int rpr0521_write_int_enable(struct rpr0521_data
*data
)
483 /* Interrupt after each measurement */
484 err
= regmap_update_bits(data
->regmap
, RPR0521_REG_PXS_CTRL
,
485 RPR0521_PXS_PERSISTENCE_MASK
,
486 RPR0521_PXS_PERSISTENCE_DRDY
);
488 dev_err(&data
->client
->dev
, "PS control reg write fail.\n");
492 /* Ignore latch and mode because of drdy */
493 err
= regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
494 RPR0521_INTERRUPT_INT_REASSERT_DISABLE
|
495 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
496 RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
499 dev_err(&data
->client
->dev
, "Interrupt setup write fail.\n");
506 static int rpr0521_write_int_disable(struct rpr0521_data
*data
)
508 /* Don't care of clearing mode, assert and latch. */
509 return regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
510 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
511 RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
516 * Trigger producer enable / disable. Note that there will be trigs only when
517 * measurement data is ready to be read.
519 static int rpr0521_pxs_drdy_set_state(struct iio_trigger
*trigger
,
522 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trigger
);
523 struct rpr0521_data
*data
= iio_priv(indio_dev
);
527 err
= rpr0521_write_int_enable(data
);
529 err
= rpr0521_write_int_disable(data
);
531 dev_err(&data
->client
->dev
, "rpr0521_pxs_drdy_set_state failed\n");
536 static const struct iio_trigger_ops rpr0521_trigger_ops
= {
537 .set_trigger_state
= rpr0521_pxs_drdy_set_state
,
541 static int rpr0521_buffer_preenable(struct iio_dev
*indio_dev
)
544 struct rpr0521_data
*data
= iio_priv(indio_dev
);
546 mutex_lock(&data
->lock
);
547 err
= rpr0521_set_power_state(data
, true,
548 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
549 mutex_unlock(&data
->lock
);
551 dev_err(&data
->client
->dev
, "_buffer_preenable fail\n");
556 static int rpr0521_buffer_postdisable(struct iio_dev
*indio_dev
)
559 struct rpr0521_data
*data
= iio_priv(indio_dev
);
561 mutex_lock(&data
->lock
);
562 err
= rpr0521_set_power_state(data
, false,
563 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
564 mutex_unlock(&data
->lock
);
566 dev_err(&data
->client
->dev
, "_buffer_postdisable fail\n");
571 static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops
= {
572 .preenable
= rpr0521_buffer_preenable
,
573 .postenable
= iio_triggered_buffer_postenable
,
574 .predisable
= iio_triggered_buffer_predisable
,
575 .postdisable
= rpr0521_buffer_postdisable
,
578 static int rpr0521_get_gain(struct rpr0521_data
*data
, int chan
,
583 ret
= regmap_read(data
->regmap
, rpr0521_gain
[chan
].reg
, ®
);
587 idx
= (rpr0521_gain
[chan
].mask
& reg
) >> rpr0521_gain
[chan
].shift
;
588 *val
= rpr0521_gain
[chan
].gain
[idx
].scale
;
589 *val2
= rpr0521_gain
[chan
].gain
[idx
].uscale
;
594 static int rpr0521_set_gain(struct rpr0521_data
*data
, int chan
,
597 int i
, idx
= -EINVAL
;
600 for (i
= 0; i
< rpr0521_gain
[chan
].size
; i
++)
601 if (val
== rpr0521_gain
[chan
].gain
[i
].scale
&&
602 val2
== rpr0521_gain
[chan
].gain
[i
].uscale
) {
610 return regmap_update_bits(data
->regmap
, rpr0521_gain
[chan
].reg
,
611 rpr0521_gain
[chan
].mask
,
612 idx
<< rpr0521_gain
[chan
].shift
);
615 static int rpr0521_read_samp_freq(struct rpr0521_data
*data
,
616 enum iio_chan_type chan_type
,
621 ret
= regmap_read(data
->regmap
, RPR0521_REG_MODE_CTRL
, ®
);
625 reg
&= RPR0521_MODE_MEAS_TIME_MASK
;
626 if (reg
>= ARRAY_SIZE(rpr0521_samp_freq_i
))
631 *val
= rpr0521_samp_freq_i
[reg
].als_hz
;
632 *val2
= rpr0521_samp_freq_i
[reg
].als_uhz
;
636 *val
= rpr0521_samp_freq_i
[reg
].pxs_hz
;
637 *val2
= rpr0521_samp_freq_i
[reg
].pxs_uhz
;
645 static int rpr0521_write_samp_freq_common(struct rpr0521_data
*data
,
646 enum iio_chan_type chan_type
,
653 * both pxs and als are setup only to same freq because of simplicity
675 return regmap_update_bits(data
->regmap
,
676 RPR0521_REG_MODE_CTRL
,
677 RPR0521_MODE_MEAS_TIME_MASK
,
681 static int rpr0521_read_ps_offset(struct rpr0521_data
*data
, int *offset
)
686 ret
= regmap_bulk_read(data
->regmap
,
687 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
690 dev_err(&data
->client
->dev
, "Failed to read PS OFFSET register\n");
693 *offset
= le16_to_cpu(buffer
);
698 static int rpr0521_write_ps_offset(struct rpr0521_data
*data
, int offset
)
703 buffer
= cpu_to_le16(offset
& 0x3ff);
704 ret
= regmap_raw_write(data
->regmap
,
705 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
708 dev_err(&data
->client
->dev
, "Failed to write PS OFFSET register\n");
715 static int rpr0521_read_raw(struct iio_dev
*indio_dev
,
716 struct iio_chan_spec
const *chan
, int *val
,
717 int *val2
, long mask
)
719 struct rpr0521_data
*data
= iio_priv(indio_dev
);
726 case IIO_CHAN_INFO_RAW
:
727 if (chan
->type
!= IIO_INTENSITY
&& chan
->type
!= IIO_PROXIMITY
)
730 busy
= iio_device_claim_direct_mode(indio_dev
);
734 device_mask
= rpr0521_data_reg
[chan
->address
].device_mask
;
736 mutex_lock(&data
->lock
);
737 ret
= rpr0521_set_power_state(data
, true, device_mask
);
739 goto rpr0521_read_raw_out
;
741 ret
= regmap_bulk_read(data
->regmap
,
742 rpr0521_data_reg
[chan
->address
].address
,
743 &raw_data
, sizeof(raw_data
));
745 rpr0521_set_power_state(data
, false, device_mask
);
746 goto rpr0521_read_raw_out
;
749 ret
= rpr0521_set_power_state(data
, false, device_mask
);
751 rpr0521_read_raw_out
:
752 mutex_unlock(&data
->lock
);
753 iio_device_release_direct_mode(indio_dev
);
757 *val
= le16_to_cpu(raw_data
);
761 case IIO_CHAN_INFO_SCALE
:
762 mutex_lock(&data
->lock
);
763 ret
= rpr0521_get_gain(data
, chan
->address
, val
, val2
);
764 mutex_unlock(&data
->lock
);
768 return IIO_VAL_INT_PLUS_MICRO
;
770 case IIO_CHAN_INFO_SAMP_FREQ
:
771 mutex_lock(&data
->lock
);
772 ret
= rpr0521_read_samp_freq(data
, chan
->type
, val
, val2
);
773 mutex_unlock(&data
->lock
);
777 return IIO_VAL_INT_PLUS_MICRO
;
779 case IIO_CHAN_INFO_OFFSET
:
780 mutex_lock(&data
->lock
);
781 ret
= rpr0521_read_ps_offset(data
, val
);
782 mutex_unlock(&data
->lock
);
793 static int rpr0521_write_raw(struct iio_dev
*indio_dev
,
794 struct iio_chan_spec
const *chan
, int val
,
797 struct rpr0521_data
*data
= iio_priv(indio_dev
);
801 case IIO_CHAN_INFO_SCALE
:
802 mutex_lock(&data
->lock
);
803 ret
= rpr0521_set_gain(data
, chan
->address
, val
, val2
);
804 mutex_unlock(&data
->lock
);
808 case IIO_CHAN_INFO_SAMP_FREQ
:
809 mutex_lock(&data
->lock
);
810 ret
= rpr0521_write_samp_freq_common(data
, chan
->type
,
812 mutex_unlock(&data
->lock
);
816 case IIO_CHAN_INFO_OFFSET
:
817 mutex_lock(&data
->lock
);
818 ret
= rpr0521_write_ps_offset(data
, val
);
819 mutex_unlock(&data
->lock
);
828 static const struct iio_info rpr0521_info
= {
829 .read_raw
= rpr0521_read_raw
,
830 .write_raw
= rpr0521_write_raw
,
831 .attrs
= &rpr0521_attribute_group
,
834 static int rpr0521_init(struct rpr0521_data
*data
)
839 ret
= regmap_read(data
->regmap
, RPR0521_REG_ID
, &id
);
841 dev_err(&data
->client
->dev
, "Failed to read REG_ID register\n");
845 if (id
!= RPR0521_MANUFACT_ID
) {
846 dev_err(&data
->client
->dev
, "Wrong id, got %x, expected %x\n",
847 id
, RPR0521_MANUFACT_ID
);
851 /* set default measurement time - 100 ms for both ALS and PS */
852 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
853 RPR0521_MODE_MEAS_TIME_MASK
,
854 RPR0521_DEFAULT_MEAS_TIME
);
856 pr_err("regmap_update_bits returned %d\n", ret
);
861 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
864 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
869 data
->irq_timestamp
= 0;
874 static int rpr0521_poweroff(struct rpr0521_data
*data
)
879 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
880 RPR0521_MODE_ALS_MASK
|
881 RPR0521_MODE_PXS_MASK
,
882 RPR0521_MODE_ALS_DISABLE
|
883 RPR0521_MODE_PXS_DISABLE
);
887 data
->als_dev_en
= false;
888 data
->pxs_dev_en
= false;
891 * Int pin keeps state after power off. Set pin to high impedance
892 * mode to prevent power drain.
894 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, &tmp
);
896 dev_err(&data
->client
->dev
, "Failed to reset int pin.\n");
903 static bool rpr0521_is_volatile_reg(struct device
*dev
, unsigned int reg
)
906 case RPR0521_REG_MODE_CTRL
:
907 case RPR0521_REG_ALS_CTRL
:
908 case RPR0521_REG_PXS_CTRL
:
915 static const struct regmap_config rpr0521_regmap_config
= {
916 .name
= RPR0521_REGMAP_NAME
,
921 .max_register
= RPR0521_REG_ID
,
922 .cache_type
= REGCACHE_RBTREE
,
923 .volatile_reg
= rpr0521_is_volatile_reg
,
926 static int rpr0521_probe(struct i2c_client
*client
,
927 const struct i2c_device_id
*id
)
929 struct rpr0521_data
*data
;
930 struct iio_dev
*indio_dev
;
931 struct regmap
*regmap
;
934 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
938 regmap
= devm_regmap_init_i2c(client
, &rpr0521_regmap_config
);
939 if (IS_ERR(regmap
)) {
940 dev_err(&client
->dev
, "regmap_init failed!\n");
941 return PTR_ERR(regmap
);
944 data
= iio_priv(indio_dev
);
945 i2c_set_clientdata(client
, indio_dev
);
946 data
->client
= client
;
947 data
->regmap
= regmap
;
949 mutex_init(&data
->lock
);
951 indio_dev
->dev
.parent
= &client
->dev
;
952 indio_dev
->info
= &rpr0521_info
;
953 indio_dev
->name
= RPR0521_DRV_NAME
;
954 indio_dev
->channels
= rpr0521_channels
;
955 indio_dev
->num_channels
= ARRAY_SIZE(rpr0521_channels
);
956 indio_dev
->modes
= INDIO_DIRECT_MODE
;
958 ret
= rpr0521_init(data
);
960 dev_err(&client
->dev
, "rpr0521 chip init failed\n");
964 ret
= pm_runtime_set_active(&client
->dev
);
968 pm_runtime_enable(&client
->dev
);
969 pm_runtime_set_autosuspend_delay(&client
->dev
, RPR0521_SLEEP_DELAY_MS
);
970 pm_runtime_use_autosuspend(&client
->dev
);
973 * If sensor write/read is needed in _probe after _use_autosuspend,
974 * sensor needs to be _resumed first using rpr0521_set_power_state().
977 /* IRQ to trigger setup */
979 /* Trigger0 producer setup */
980 data
->drdy_trigger0
= devm_iio_trigger_alloc(
981 indio_dev
->dev
.parent
,
982 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
983 if (!data
->drdy_trigger0
) {
987 data
->drdy_trigger0
->dev
.parent
= indio_dev
->dev
.parent
;
988 data
->drdy_trigger0
->ops
= &rpr0521_trigger_ops
;
989 indio_dev
->available_scan_masks
= rpr0521_available_scan_masks
;
990 iio_trigger_set_drvdata(data
->drdy_trigger0
, indio_dev
);
992 /* Ties irq to trigger producer handler. */
993 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
994 rpr0521_drdy_irq_handler
, rpr0521_drdy_irq_thread
,
995 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
996 RPR0521_IRQ_NAME
, indio_dev
);
998 dev_err(&client
->dev
, "request irq %d for trigger0 failed\n",
1000 goto err_pm_disable
;
1003 ret
= devm_iio_trigger_register(indio_dev
->dev
.parent
,
1004 data
->drdy_trigger0
);
1006 dev_err(&client
->dev
, "iio trigger register failed\n");
1007 goto err_pm_disable
;
1011 * Now whole pipe from physical interrupt (irq defined by
1012 * devicetree to device) to trigger0 output is set up.
1015 /* Trigger consumer setup */
1016 ret
= devm_iio_triggered_buffer_setup(indio_dev
->dev
.parent
,
1018 rpr0521_trigger_consumer_store_time
,
1019 rpr0521_trigger_consumer_handler
,
1020 &rpr0521_buffer_setup_ops
);
1022 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1023 goto err_pm_disable
;
1027 ret
= iio_device_register(indio_dev
);
1029 goto err_pm_disable
;
1034 pm_runtime_disable(&client
->dev
);
1035 pm_runtime_set_suspended(&client
->dev
);
1036 pm_runtime_put_noidle(&client
->dev
);
1038 rpr0521_poweroff(data
);
1043 static int rpr0521_remove(struct i2c_client
*client
)
1045 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1047 iio_device_unregister(indio_dev
);
1049 pm_runtime_disable(&client
->dev
);
1050 pm_runtime_set_suspended(&client
->dev
);
1051 pm_runtime_put_noidle(&client
->dev
);
1053 rpr0521_poweroff(iio_priv(indio_dev
));
1059 static int rpr0521_runtime_suspend(struct device
*dev
)
1061 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1062 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1065 mutex_lock(&data
->lock
);
1066 /* If measurements are enabled, enable them on resume */
1067 if (!data
->als_need_dis
)
1068 data
->als_ps_need_en
= data
->als_dev_en
;
1069 if (!data
->pxs_need_dis
)
1070 data
->pxs_ps_need_en
= data
->pxs_dev_en
;
1072 /* disable channels and sets {als,pxs}_dev_en to false */
1073 ret
= rpr0521_poweroff(data
);
1074 regcache_mark_dirty(data
->regmap
);
1075 mutex_unlock(&data
->lock
);
1080 static int rpr0521_runtime_resume(struct device
*dev
)
1082 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1083 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1086 regcache_sync(data
->regmap
);
1087 if (data
->als_ps_need_en
) {
1088 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
1091 data
->als_ps_need_en
= false;
1094 if (data
->pxs_ps_need_en
) {
1095 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
1098 data
->pxs_ps_need_en
= false;
1100 msleep(100); //wait for first measurement result
1106 static const struct dev_pm_ops rpr0521_pm_ops
= {
1107 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend
,
1108 rpr0521_runtime_resume
, NULL
)
1111 static const struct acpi_device_id rpr0521_acpi_match
[] = {
1115 MODULE_DEVICE_TABLE(acpi
, rpr0521_acpi_match
);
1117 static const struct i2c_device_id rpr0521_id
[] = {
1122 MODULE_DEVICE_TABLE(i2c
, rpr0521_id
);
1124 static struct i2c_driver rpr0521_driver
= {
1126 .name
= RPR0521_DRV_NAME
,
1127 .pm
= &rpr0521_pm_ops
,
1128 .acpi_match_table
= ACPI_PTR(rpr0521_acpi_match
),
1130 .probe
= rpr0521_probe
,
1131 .remove
= rpr0521_remove
,
1132 .id_table
= rpr0521_id
,
1135 module_i2c_driver(rpr0521_driver
);
1137 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1138 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
1139 MODULE_LICENSE("GPL v2");