2 * RPR-0521 ROHM Ambient Light and Proximity Sensor
4 * Copyright (c) 2015, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for RPR-0521RS (7-bit I2C slave address 0x38).
12 * TODO: illuminance channel
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/delay.h>
20 #include <linux/acpi.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/trigger.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/pm_runtime.h>
30 #define RPR0521_REG_SYSTEM_CTRL 0x40
31 #define RPR0521_REG_MODE_CTRL 0x41
32 #define RPR0521_REG_ALS_CTRL 0x42
33 #define RPR0521_REG_PXS_CTRL 0x43
34 #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
35 #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
36 #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
37 #define RPR0521_REG_INTERRUPT 0x4A
38 #define RPR0521_REG_PS_OFFSET_LSB 0x53
39 #define RPR0521_REG_ID 0x92
41 #define RPR0521_MODE_ALS_MASK BIT(7)
42 #define RPR0521_MODE_PXS_MASK BIT(6)
43 #define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
44 #define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
45 #define RPR0521_ALS_DATA0_GAIN_SHIFT 4
46 #define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
47 #define RPR0521_ALS_DATA1_GAIN_SHIFT 2
48 #define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
49 #define RPR0521_PXS_GAIN_SHIFT 4
50 #define RPR0521_PXS_PERSISTENCE_MASK GENMASK(3, 0)
51 #define RPR0521_INTERRUPT_INT_TRIG_PS_MASK BIT(0)
52 #define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK BIT(1)
53 #define RPR0521_INTERRUPT_INT_REASSERT_MASK BIT(3)
54 #define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK BIT(6)
55 #define RPR0521_INTERRUPT_PS_INT_STATUS_MASK BIT(7)
57 #define RPR0521_MODE_ALS_ENABLE BIT(7)
58 #define RPR0521_MODE_ALS_DISABLE 0x00
59 #define RPR0521_MODE_PXS_ENABLE BIT(6)
60 #define RPR0521_MODE_PXS_DISABLE 0x00
61 #define RPR0521_PXS_PERSISTENCE_DRDY 0x00
63 #define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE BIT(0)
64 #define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE 0x00
65 #define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE BIT(1)
66 #define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE 0x00
67 #define RPR0521_INTERRUPT_INT_REASSERT_ENABLE BIT(3)
68 #define RPR0521_INTERRUPT_INT_REASSERT_DISABLE 0x00
70 #define RPR0521_MANUFACT_ID 0xE0
71 #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
73 #define RPR0521_DRV_NAME "RPR0521"
74 #define RPR0521_IRQ_NAME "rpr0521_event"
75 #define RPR0521_REGMAP_NAME "rpr0521_regmap"
77 #define RPR0521_SLEEP_DELAY_MS 2000
79 #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
80 #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
87 static const struct rpr0521_gain rpr0521_als_gain
[4] = {
94 static const struct rpr0521_gain rpr0521_pxs_gain
[3] = {
100 enum rpr0521_channel
{
102 RPR0521_CHAN_ALS_DATA0
,
103 RPR0521_CHAN_ALS_DATA1
,
106 struct rpr0521_reg_desc
{
111 static const struct rpr0521_reg_desc rpr0521_data_reg
[] = {
112 [RPR0521_CHAN_PXS
] = {
113 .address
= RPR0521_REG_PXS_DATA
,
114 .device_mask
= RPR0521_MODE_PXS_MASK
,
116 [RPR0521_CHAN_ALS_DATA0
] = {
117 .address
= RPR0521_REG_ALS_DATA0
,
118 .device_mask
= RPR0521_MODE_ALS_MASK
,
120 [RPR0521_CHAN_ALS_DATA1
] = {
121 .address
= RPR0521_REG_ALS_DATA1
,
122 .device_mask
= RPR0521_MODE_ALS_MASK
,
126 static const struct rpr0521_gain_info
{
130 const struct rpr0521_gain
*gain
;
133 [RPR0521_CHAN_PXS
] = {
134 .reg
= RPR0521_REG_PXS_CTRL
,
135 .mask
= RPR0521_PXS_GAIN_MASK
,
136 .shift
= RPR0521_PXS_GAIN_SHIFT
,
137 .gain
= rpr0521_pxs_gain
,
138 .size
= ARRAY_SIZE(rpr0521_pxs_gain
),
140 [RPR0521_CHAN_ALS_DATA0
] = {
141 .reg
= RPR0521_REG_ALS_CTRL
,
142 .mask
= RPR0521_ALS_DATA0_GAIN_MASK
,
143 .shift
= RPR0521_ALS_DATA0_GAIN_SHIFT
,
144 .gain
= rpr0521_als_gain
,
145 .size
= ARRAY_SIZE(rpr0521_als_gain
),
147 [RPR0521_CHAN_ALS_DATA1
] = {
148 .reg
= RPR0521_REG_ALS_CTRL
,
149 .mask
= RPR0521_ALS_DATA1_GAIN_MASK
,
150 .shift
= RPR0521_ALS_DATA1_GAIN_SHIFT
,
151 .gain
= rpr0521_als_gain
,
152 .size
= ARRAY_SIZE(rpr0521_als_gain
),
156 struct rpr0521_samp_freq
{
163 static const struct rpr0521_samp_freq rpr0521_samp_freq_i
[13] = {
164 /* {ALS, PXS}, W==currently writable option */
165 {0, 0, 0, 0}, /* W0000, 0=standby */
166 {0, 0, 100, 0}, /* 0001 */
167 {0, 0, 25, 0}, /* 0010 */
168 {0, 0, 10, 0}, /* 0011 */
169 {0, 0, 2, 500000}, /* 0100 */
170 {10, 0, 20, 0}, /* 0101 */
171 {10, 0, 10, 0}, /* W0110 */
172 {10, 0, 2, 500000}, /* 0111 */
173 {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
174 {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
175 {2, 500000, 0, 0}, /* 1010, high sensitivity mode */
176 {2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
177 {20, 0, 20, 0} /* 1100, ALS_data x 0.5, see specification P.18 */
180 struct rpr0521_data
{
181 struct i2c_client
*client
;
183 /* protect device params updates (e.g state, gain) */
186 /* device active status */
190 struct iio_trigger
*drdy_trigger0
;
193 /* optimize runtime pm ops - enable/disable device only if needed */
199 struct regmap
*regmap
;
202 static IIO_CONST_ATTR(in_intensity_scale_available
, RPR0521_ALS_SCALE_AVAIL
);
203 static IIO_CONST_ATTR(in_proximity_scale_available
, RPR0521_PXS_SCALE_AVAIL
);
206 * Start with easy freq first, whole table of freq combinations is more
209 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
211 static struct attribute
*rpr0521_attributes
[] = {
212 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
213 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
214 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
218 static const struct attribute_group rpr0521_attribute_group
= {
219 .attrs
= rpr0521_attributes
,
222 /* Order of the channel data in buffer */
223 enum rpr0521_scan_index_order
{
224 RPR0521_CHAN_INDEX_PXS
,
225 RPR0521_CHAN_INDEX_BOTH
,
226 RPR0521_CHAN_INDEX_IR
,
229 static const unsigned long rpr0521_available_scan_masks
[] = {
230 BIT(RPR0521_CHAN_INDEX_PXS
) | BIT(RPR0521_CHAN_INDEX_BOTH
) |
231 BIT(RPR0521_CHAN_INDEX_IR
),
235 static const struct iio_chan_spec rpr0521_channels
[] = {
237 .type
= IIO_PROXIMITY
,
238 .address
= RPR0521_CHAN_PXS
,
239 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
240 BIT(IIO_CHAN_INFO_OFFSET
) |
241 BIT(IIO_CHAN_INFO_SCALE
),
242 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
243 .scan_index
= RPR0521_CHAN_INDEX_PXS
,
248 .endianness
= IIO_LE
,
252 .type
= IIO_INTENSITY
,
254 .address
= RPR0521_CHAN_ALS_DATA0
,
255 .channel2
= IIO_MOD_LIGHT_BOTH
,
256 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
257 BIT(IIO_CHAN_INFO_SCALE
),
258 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
259 .scan_index
= RPR0521_CHAN_INDEX_BOTH
,
264 .endianness
= IIO_LE
,
268 .type
= IIO_INTENSITY
,
270 .address
= RPR0521_CHAN_ALS_DATA1
,
271 .channel2
= IIO_MOD_LIGHT_IR
,
272 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
273 BIT(IIO_CHAN_INFO_SCALE
),
274 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
275 .scan_index
= RPR0521_CHAN_INDEX_IR
,
280 .endianness
= IIO_LE
,
285 static int rpr0521_als_enable(struct rpr0521_data
*data
, u8 status
)
289 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
290 RPR0521_MODE_ALS_MASK
,
295 if (status
& RPR0521_MODE_ALS_MASK
)
296 data
->als_dev_en
= true;
298 data
->als_dev_en
= false;
303 static int rpr0521_pxs_enable(struct rpr0521_data
*data
, u8 status
)
307 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
308 RPR0521_MODE_PXS_MASK
,
313 if (status
& RPR0521_MODE_PXS_MASK
)
314 data
->pxs_dev_en
= true;
316 data
->pxs_dev_en
= false;
322 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
324 * @data: rpr0521 device private data
325 * @on: state to be set for devices in @device_mask
326 * @device_mask: bitmask specifying for which device we need to update @on state
328 * Calls for this function must be balanced so that each ON should have matching
329 * OFF. Otherwise pm usage_count gets out of sync.
331 static int rpr0521_set_power_state(struct rpr0521_data
*data
, bool on
,
337 if (device_mask
& RPR0521_MODE_ALS_MASK
) {
338 data
->als_ps_need_en
= on
;
339 data
->als_need_dis
= !on
;
342 if (device_mask
& RPR0521_MODE_PXS_MASK
) {
343 data
->pxs_ps_need_en
= on
;
344 data
->pxs_need_dis
= !on
;
348 * On: _resume() is called only when we are suspended
349 * Off: _suspend() is called after delay if _resume() is not
350 * called before that.
351 * Note: If either measurement is re-enabled before _suspend(),
352 * both stay enabled until _suspend().
355 ret
= pm_runtime_get_sync(&data
->client
->dev
);
357 pm_runtime_mark_last_busy(&data
->client
->dev
);
358 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
361 dev_err(&data
->client
->dev
,
362 "Failed: rpr0521_set_power_state for %d, ret %d\n",
365 pm_runtime_put_noidle(&data
->client
->dev
);
371 /* If _resume() was not called, enable measurement now. */
372 if (data
->als_ps_need_en
) {
373 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
376 data
->als_ps_need_en
= false;
379 if (data
->pxs_ps_need_en
) {
380 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
383 data
->pxs_ps_need_en
= false;
390 /* Interrupt register tells if this sensor caused the interrupt or not. */
391 static inline bool rpr0521_is_triggered(struct rpr0521_data
*data
)
396 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, ®
);
398 return false; /* Reg read failed. */
400 (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK
|
401 RPR0521_INTERRUPT_PS_INT_STATUS_MASK
))
404 return false; /* Int not from this sensor. */
407 /* IRQ to trigger handler */
408 static irqreturn_t
rpr0521_drdy_irq_handler(int irq
, void *private)
410 struct iio_dev
*indio_dev
= private;
411 struct rpr0521_data
*data
= iio_priv(indio_dev
);
413 data
->irq_timestamp
= iio_get_time_ns(indio_dev
);
415 * We need to wake the thread to read the interrupt reg. It
416 * is not possible to do that here because regmap_read takes a
420 return IRQ_WAKE_THREAD
;
423 static irqreturn_t
rpr0521_drdy_irq_thread(int irq
, void *private)
425 struct iio_dev
*indio_dev
= private;
426 struct rpr0521_data
*data
= iio_priv(indio_dev
);
428 if (rpr0521_is_triggered(data
)) {
429 iio_trigger_poll_chained(data
->drdy_trigger0
);
436 static irqreturn_t
rpr0521_trigger_consumer_store_time(int irq
, void *p
)
438 struct iio_poll_func
*pf
= p
;
439 struct iio_dev
*indio_dev
= pf
->indio_dev
;
441 /* Other trigger polls store time here. */
442 if (!iio_trigger_using_own(indio_dev
))
443 pf
->timestamp
= iio_get_time_ns(indio_dev
);
445 return IRQ_WAKE_THREAD
;
448 static irqreturn_t
rpr0521_trigger_consumer_handler(int irq
, void *p
)
450 struct iio_poll_func
*pf
= p
;
451 struct iio_dev
*indio_dev
= pf
->indio_dev
;
452 struct rpr0521_data
*data
= iio_priv(indio_dev
);
455 u8 buffer
[16]; /* 3 16-bit channels + padding + ts */
457 /* Use irq timestamp when reasonable. */
458 if (iio_trigger_using_own(indio_dev
) && data
->irq_timestamp
) {
459 pf
->timestamp
= data
->irq_timestamp
;
460 data
->irq_timestamp
= 0;
462 /* Other chained trigger polls get timestamp only here. */
464 pf
->timestamp
= iio_get_time_ns(indio_dev
);
466 err
= regmap_bulk_read(data
->regmap
, RPR0521_REG_PXS_DATA
,
468 (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */
470 iio_push_to_buffers_with_timestamp(indio_dev
,
471 buffer
, pf
->timestamp
);
473 dev_err(&data
->client
->dev
,
474 "Trigger consumer can't read from sensor.\n");
477 iio_trigger_notify_done(indio_dev
->trig
);
482 static int rpr0521_write_int_enable(struct rpr0521_data
*data
)
486 /* Interrupt after each measurement */
487 err
= regmap_update_bits(data
->regmap
, RPR0521_REG_PXS_CTRL
,
488 RPR0521_PXS_PERSISTENCE_MASK
,
489 RPR0521_PXS_PERSISTENCE_DRDY
);
491 dev_err(&data
->client
->dev
, "PS control reg write fail.\n");
495 /* Ignore latch and mode because of drdy */
496 err
= regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
497 RPR0521_INTERRUPT_INT_REASSERT_DISABLE
|
498 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
499 RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
502 dev_err(&data
->client
->dev
, "Interrupt setup write fail.\n");
509 static int rpr0521_write_int_disable(struct rpr0521_data
*data
)
511 /* Don't care of clearing mode, assert and latch. */
512 return regmap_write(data
->regmap
, RPR0521_REG_INTERRUPT
,
513 RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE
|
514 RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
519 * Trigger producer enable / disable. Note that there will be trigs only when
520 * measurement data is ready to be read.
522 static int rpr0521_pxs_drdy_set_state(struct iio_trigger
*trigger
,
525 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trigger
);
526 struct rpr0521_data
*data
= iio_priv(indio_dev
);
530 err
= rpr0521_write_int_enable(data
);
532 err
= rpr0521_write_int_disable(data
);
534 dev_err(&data
->client
->dev
, "rpr0521_pxs_drdy_set_state failed\n");
539 static const struct iio_trigger_ops rpr0521_trigger_ops
= {
540 .set_trigger_state
= rpr0521_pxs_drdy_set_state
,
544 static int rpr0521_buffer_preenable(struct iio_dev
*indio_dev
)
547 struct rpr0521_data
*data
= iio_priv(indio_dev
);
549 mutex_lock(&data
->lock
);
550 err
= rpr0521_set_power_state(data
, true,
551 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
552 mutex_unlock(&data
->lock
);
554 dev_err(&data
->client
->dev
, "_buffer_preenable fail\n");
559 static int rpr0521_buffer_postdisable(struct iio_dev
*indio_dev
)
562 struct rpr0521_data
*data
= iio_priv(indio_dev
);
564 mutex_lock(&data
->lock
);
565 err
= rpr0521_set_power_state(data
, false,
566 (RPR0521_MODE_PXS_MASK
| RPR0521_MODE_ALS_MASK
));
567 mutex_unlock(&data
->lock
);
569 dev_err(&data
->client
->dev
, "_buffer_postdisable fail\n");
574 static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops
= {
575 .preenable
= rpr0521_buffer_preenable
,
576 .postenable
= iio_triggered_buffer_postenable
,
577 .predisable
= iio_triggered_buffer_predisable
,
578 .postdisable
= rpr0521_buffer_postdisable
,
581 static int rpr0521_get_gain(struct rpr0521_data
*data
, int chan
,
586 ret
= regmap_read(data
->regmap
, rpr0521_gain
[chan
].reg
, ®
);
590 idx
= (rpr0521_gain
[chan
].mask
& reg
) >> rpr0521_gain
[chan
].shift
;
591 *val
= rpr0521_gain
[chan
].gain
[idx
].scale
;
592 *val2
= rpr0521_gain
[chan
].gain
[idx
].uscale
;
597 static int rpr0521_set_gain(struct rpr0521_data
*data
, int chan
,
600 int i
, idx
= -EINVAL
;
603 for (i
= 0; i
< rpr0521_gain
[chan
].size
; i
++)
604 if (val
== rpr0521_gain
[chan
].gain
[i
].scale
&&
605 val2
== rpr0521_gain
[chan
].gain
[i
].uscale
) {
613 return regmap_update_bits(data
->regmap
, rpr0521_gain
[chan
].reg
,
614 rpr0521_gain
[chan
].mask
,
615 idx
<< rpr0521_gain
[chan
].shift
);
618 static int rpr0521_read_samp_freq(struct rpr0521_data
*data
,
619 enum iio_chan_type chan_type
,
624 ret
= regmap_read(data
->regmap
, RPR0521_REG_MODE_CTRL
, ®
);
628 reg
&= RPR0521_MODE_MEAS_TIME_MASK
;
629 if (reg
>= ARRAY_SIZE(rpr0521_samp_freq_i
))
634 *val
= rpr0521_samp_freq_i
[reg
].als_hz
;
635 *val2
= rpr0521_samp_freq_i
[reg
].als_uhz
;
639 *val
= rpr0521_samp_freq_i
[reg
].pxs_hz
;
640 *val2
= rpr0521_samp_freq_i
[reg
].pxs_uhz
;
648 static int rpr0521_write_samp_freq_common(struct rpr0521_data
*data
,
649 enum iio_chan_type chan_type
,
656 * both pxs and als are setup only to same freq because of simplicity
678 return regmap_update_bits(data
->regmap
,
679 RPR0521_REG_MODE_CTRL
,
680 RPR0521_MODE_MEAS_TIME_MASK
,
684 static int rpr0521_read_ps_offset(struct rpr0521_data
*data
, int *offset
)
689 ret
= regmap_bulk_read(data
->regmap
,
690 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
693 dev_err(&data
->client
->dev
, "Failed to read PS OFFSET register\n");
696 *offset
= le16_to_cpu(buffer
);
701 static int rpr0521_write_ps_offset(struct rpr0521_data
*data
, int offset
)
706 buffer
= cpu_to_le16(offset
& 0x3ff);
707 ret
= regmap_raw_write(data
->regmap
,
708 RPR0521_REG_PS_OFFSET_LSB
, &buffer
, sizeof(buffer
));
711 dev_err(&data
->client
->dev
, "Failed to write PS OFFSET register\n");
718 static int rpr0521_read_raw(struct iio_dev
*indio_dev
,
719 struct iio_chan_spec
const *chan
, int *val
,
720 int *val2
, long mask
)
722 struct rpr0521_data
*data
= iio_priv(indio_dev
);
729 case IIO_CHAN_INFO_RAW
:
730 if (chan
->type
!= IIO_INTENSITY
&& chan
->type
!= IIO_PROXIMITY
)
733 busy
= iio_device_claim_direct_mode(indio_dev
);
737 device_mask
= rpr0521_data_reg
[chan
->address
].device_mask
;
739 mutex_lock(&data
->lock
);
740 ret
= rpr0521_set_power_state(data
, true, device_mask
);
742 goto rpr0521_read_raw_out
;
744 ret
= regmap_bulk_read(data
->regmap
,
745 rpr0521_data_reg
[chan
->address
].address
,
746 &raw_data
, sizeof(raw_data
));
748 rpr0521_set_power_state(data
, false, device_mask
);
749 goto rpr0521_read_raw_out
;
752 ret
= rpr0521_set_power_state(data
, false, device_mask
);
754 rpr0521_read_raw_out
:
755 mutex_unlock(&data
->lock
);
756 iio_device_release_direct_mode(indio_dev
);
760 *val
= le16_to_cpu(raw_data
);
764 case IIO_CHAN_INFO_SCALE
:
765 mutex_lock(&data
->lock
);
766 ret
= rpr0521_get_gain(data
, chan
->address
, val
, val2
);
767 mutex_unlock(&data
->lock
);
771 return IIO_VAL_INT_PLUS_MICRO
;
773 case IIO_CHAN_INFO_SAMP_FREQ
:
774 mutex_lock(&data
->lock
);
775 ret
= rpr0521_read_samp_freq(data
, chan
->type
, val
, val2
);
776 mutex_unlock(&data
->lock
);
780 return IIO_VAL_INT_PLUS_MICRO
;
782 case IIO_CHAN_INFO_OFFSET
:
783 mutex_lock(&data
->lock
);
784 ret
= rpr0521_read_ps_offset(data
, val
);
785 mutex_unlock(&data
->lock
);
796 static int rpr0521_write_raw(struct iio_dev
*indio_dev
,
797 struct iio_chan_spec
const *chan
, int val
,
800 struct rpr0521_data
*data
= iio_priv(indio_dev
);
804 case IIO_CHAN_INFO_SCALE
:
805 mutex_lock(&data
->lock
);
806 ret
= rpr0521_set_gain(data
, chan
->address
, val
, val2
);
807 mutex_unlock(&data
->lock
);
811 case IIO_CHAN_INFO_SAMP_FREQ
:
812 mutex_lock(&data
->lock
);
813 ret
= rpr0521_write_samp_freq_common(data
, chan
->type
,
815 mutex_unlock(&data
->lock
);
819 case IIO_CHAN_INFO_OFFSET
:
820 mutex_lock(&data
->lock
);
821 ret
= rpr0521_write_ps_offset(data
, val
);
822 mutex_unlock(&data
->lock
);
831 static const struct iio_info rpr0521_info
= {
832 .read_raw
= rpr0521_read_raw
,
833 .write_raw
= rpr0521_write_raw
,
834 .attrs
= &rpr0521_attribute_group
,
837 static int rpr0521_init(struct rpr0521_data
*data
)
842 ret
= regmap_read(data
->regmap
, RPR0521_REG_ID
, &id
);
844 dev_err(&data
->client
->dev
, "Failed to read REG_ID register\n");
848 if (id
!= RPR0521_MANUFACT_ID
) {
849 dev_err(&data
->client
->dev
, "Wrong id, got %x, expected %x\n",
850 id
, RPR0521_MANUFACT_ID
);
854 /* set default measurement time - 100 ms for both ALS and PS */
855 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
856 RPR0521_MODE_MEAS_TIME_MASK
,
857 RPR0521_DEFAULT_MEAS_TIME
);
859 pr_err("regmap_update_bits returned %d\n", ret
);
864 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
867 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
872 data
->irq_timestamp
= 0;
877 static int rpr0521_poweroff(struct rpr0521_data
*data
)
882 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
883 RPR0521_MODE_ALS_MASK
|
884 RPR0521_MODE_PXS_MASK
,
885 RPR0521_MODE_ALS_DISABLE
|
886 RPR0521_MODE_PXS_DISABLE
);
890 data
->als_dev_en
= false;
891 data
->pxs_dev_en
= false;
894 * Int pin keeps state after power off. Set pin to high impedance
895 * mode to prevent power drain.
897 ret
= regmap_read(data
->regmap
, RPR0521_REG_INTERRUPT
, &tmp
);
899 dev_err(&data
->client
->dev
, "Failed to reset int pin.\n");
906 static bool rpr0521_is_volatile_reg(struct device
*dev
, unsigned int reg
)
909 case RPR0521_REG_MODE_CTRL
:
910 case RPR0521_REG_ALS_CTRL
:
911 case RPR0521_REG_PXS_CTRL
:
918 static const struct regmap_config rpr0521_regmap_config
= {
919 .name
= RPR0521_REGMAP_NAME
,
924 .max_register
= RPR0521_REG_ID
,
925 .cache_type
= REGCACHE_RBTREE
,
926 .volatile_reg
= rpr0521_is_volatile_reg
,
929 static int rpr0521_probe(struct i2c_client
*client
,
930 const struct i2c_device_id
*id
)
932 struct rpr0521_data
*data
;
933 struct iio_dev
*indio_dev
;
934 struct regmap
*regmap
;
937 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
941 regmap
= devm_regmap_init_i2c(client
, &rpr0521_regmap_config
);
942 if (IS_ERR(regmap
)) {
943 dev_err(&client
->dev
, "regmap_init failed!\n");
944 return PTR_ERR(regmap
);
947 data
= iio_priv(indio_dev
);
948 i2c_set_clientdata(client
, indio_dev
);
949 data
->client
= client
;
950 data
->regmap
= regmap
;
952 mutex_init(&data
->lock
);
954 indio_dev
->dev
.parent
= &client
->dev
;
955 indio_dev
->info
= &rpr0521_info
;
956 indio_dev
->name
= RPR0521_DRV_NAME
;
957 indio_dev
->channels
= rpr0521_channels
;
958 indio_dev
->num_channels
= ARRAY_SIZE(rpr0521_channels
);
959 indio_dev
->modes
= INDIO_DIRECT_MODE
;
961 ret
= rpr0521_init(data
);
963 dev_err(&client
->dev
, "rpr0521 chip init failed\n");
967 ret
= pm_runtime_set_active(&client
->dev
);
971 pm_runtime_enable(&client
->dev
);
972 pm_runtime_set_autosuspend_delay(&client
->dev
, RPR0521_SLEEP_DELAY_MS
);
973 pm_runtime_use_autosuspend(&client
->dev
);
976 * If sensor write/read is needed in _probe after _use_autosuspend,
977 * sensor needs to be _resumed first using rpr0521_set_power_state().
980 /* IRQ to trigger setup */
982 /* Trigger0 producer setup */
983 data
->drdy_trigger0
= devm_iio_trigger_alloc(
984 indio_dev
->dev
.parent
,
985 "%s-dev%d", indio_dev
->name
, indio_dev
->id
);
986 if (!data
->drdy_trigger0
) {
990 data
->drdy_trigger0
->dev
.parent
= indio_dev
->dev
.parent
;
991 data
->drdy_trigger0
->ops
= &rpr0521_trigger_ops
;
992 indio_dev
->available_scan_masks
= rpr0521_available_scan_masks
;
993 iio_trigger_set_drvdata(data
->drdy_trigger0
, indio_dev
);
995 /* Ties irq to trigger producer handler. */
996 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
997 rpr0521_drdy_irq_handler
, rpr0521_drdy_irq_thread
,
998 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
999 RPR0521_IRQ_NAME
, indio_dev
);
1001 dev_err(&client
->dev
, "request irq %d for trigger0 failed\n",
1003 goto err_pm_disable
;
1006 ret
= devm_iio_trigger_register(indio_dev
->dev
.parent
,
1007 data
->drdy_trigger0
);
1009 dev_err(&client
->dev
, "iio trigger register failed\n");
1010 goto err_pm_disable
;
1014 * Now whole pipe from physical interrupt (irq defined by
1015 * devicetree to device) to trigger0 output is set up.
1018 /* Trigger consumer setup */
1019 ret
= devm_iio_triggered_buffer_setup(indio_dev
->dev
.parent
,
1021 rpr0521_trigger_consumer_store_time
,
1022 rpr0521_trigger_consumer_handler
,
1023 &rpr0521_buffer_setup_ops
);
1025 dev_err(&client
->dev
, "iio triggered buffer setup failed\n");
1026 goto err_pm_disable
;
1030 ret
= iio_device_register(indio_dev
);
1032 goto err_pm_disable
;
1037 pm_runtime_disable(&client
->dev
);
1038 pm_runtime_set_suspended(&client
->dev
);
1039 pm_runtime_put_noidle(&client
->dev
);
1041 rpr0521_poweroff(data
);
1046 static int rpr0521_remove(struct i2c_client
*client
)
1048 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1050 iio_device_unregister(indio_dev
);
1052 pm_runtime_disable(&client
->dev
);
1053 pm_runtime_set_suspended(&client
->dev
);
1054 pm_runtime_put_noidle(&client
->dev
);
1056 rpr0521_poweroff(iio_priv(indio_dev
));
1062 static int rpr0521_runtime_suspend(struct device
*dev
)
1064 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1065 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1068 mutex_lock(&data
->lock
);
1069 /* If measurements are enabled, enable them on resume */
1070 if (!data
->als_need_dis
)
1071 data
->als_ps_need_en
= data
->als_dev_en
;
1072 if (!data
->pxs_need_dis
)
1073 data
->pxs_ps_need_en
= data
->pxs_dev_en
;
1075 /* disable channels and sets {als,pxs}_dev_en to false */
1076 ret
= rpr0521_poweroff(data
);
1077 regcache_mark_dirty(data
->regmap
);
1078 mutex_unlock(&data
->lock
);
1083 static int rpr0521_runtime_resume(struct device
*dev
)
1085 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1086 struct rpr0521_data
*data
= iio_priv(indio_dev
);
1089 regcache_sync(data
->regmap
);
1090 if (data
->als_ps_need_en
) {
1091 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
1094 data
->als_ps_need_en
= false;
1097 if (data
->pxs_ps_need_en
) {
1098 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
1101 data
->pxs_ps_need_en
= false;
1103 msleep(100); //wait for first measurement result
1109 static const struct dev_pm_ops rpr0521_pm_ops
= {
1110 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend
,
1111 rpr0521_runtime_resume
, NULL
)
1114 static const struct acpi_device_id rpr0521_acpi_match
[] = {
1118 MODULE_DEVICE_TABLE(acpi
, rpr0521_acpi_match
);
1120 static const struct i2c_device_id rpr0521_id
[] = {
1125 MODULE_DEVICE_TABLE(i2c
, rpr0521_id
);
1127 static struct i2c_driver rpr0521_driver
= {
1129 .name
= RPR0521_DRV_NAME
,
1130 .pm
= &rpr0521_pm_ops
,
1131 .acpi_match_table
= ACPI_PTR(rpr0521_acpi_match
),
1133 .probe
= rpr0521_probe
,
1134 .remove
= rpr0521_remove
,
1135 .id_table
= rpr0521_id
,
1138 module_i2c_driver(rpr0521_driver
);
1140 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1141 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
1142 MODULE_LICENSE("GPL v2");