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, PM support, buffer
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/sysfs.h>
24 #include <linux/pm_runtime.h>
26 #define RPR0521_REG_SYSTEM_CTRL 0x40
27 #define RPR0521_REG_MODE_CTRL 0x41
28 #define RPR0521_REG_ALS_CTRL 0x42
29 #define RPR0521_REG_PXS_CTRL 0x43
30 #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
31 #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
32 #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
33 #define RPR0521_REG_ID 0x92
35 #define RPR0521_MODE_ALS_MASK BIT(7)
36 #define RPR0521_MODE_PXS_MASK BIT(6)
37 #define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
38 #define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
39 #define RPR0521_ALS_DATA0_GAIN_SHIFT 4
40 #define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
41 #define RPR0521_ALS_DATA1_GAIN_SHIFT 2
42 #define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
43 #define RPR0521_PXS_GAIN_SHIFT 4
45 #define RPR0521_MODE_ALS_ENABLE BIT(7)
46 #define RPR0521_MODE_ALS_DISABLE 0x00
47 #define RPR0521_MODE_PXS_ENABLE BIT(6)
48 #define RPR0521_MODE_PXS_DISABLE 0x00
50 #define RPR0521_MANUFACT_ID 0xE0
51 #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
53 #define RPR0521_DRV_NAME "RPR0521"
54 #define RPR0521_REGMAP_NAME "rpr0521_regmap"
56 #define RPR0521_SLEEP_DELAY_MS 2000
58 #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
59 #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
66 static const struct rpr0521_gain rpr0521_als_gain
[4] = {
73 static const struct rpr0521_gain rpr0521_pxs_gain
[3] = {
79 enum rpr0521_channel
{
80 RPR0521_CHAN_ALS_DATA0
,
81 RPR0521_CHAN_ALS_DATA1
,
85 struct rpr0521_reg_desc
{
90 static const struct rpr0521_reg_desc rpr0521_data_reg
[] = {
91 [RPR0521_CHAN_ALS_DATA0
] = {
92 .address
= RPR0521_REG_ALS_DATA0
,
93 .device_mask
= RPR0521_MODE_ALS_MASK
,
95 [RPR0521_CHAN_ALS_DATA1
] = {
96 .address
= RPR0521_REG_ALS_DATA1
,
97 .device_mask
= RPR0521_MODE_ALS_MASK
,
99 [RPR0521_CHAN_PXS
] = {
100 .address
= RPR0521_REG_PXS_DATA
,
101 .device_mask
= RPR0521_MODE_PXS_MASK
,
105 static const struct rpr0521_gain_info
{
109 const struct rpr0521_gain
*gain
;
112 [RPR0521_CHAN_ALS_DATA0
] = {
113 .reg
= RPR0521_REG_ALS_CTRL
,
114 .mask
= RPR0521_ALS_DATA0_GAIN_MASK
,
115 .shift
= RPR0521_ALS_DATA0_GAIN_SHIFT
,
116 .gain
= rpr0521_als_gain
,
117 .size
= ARRAY_SIZE(rpr0521_als_gain
),
119 [RPR0521_CHAN_ALS_DATA1
] = {
120 .reg
= RPR0521_REG_ALS_CTRL
,
121 .mask
= RPR0521_ALS_DATA1_GAIN_MASK
,
122 .shift
= RPR0521_ALS_DATA1_GAIN_SHIFT
,
123 .gain
= rpr0521_als_gain
,
124 .size
= ARRAY_SIZE(rpr0521_als_gain
),
126 [RPR0521_CHAN_PXS
] = {
127 .reg
= RPR0521_REG_PXS_CTRL
,
128 .mask
= RPR0521_PXS_GAIN_MASK
,
129 .shift
= RPR0521_PXS_GAIN_SHIFT
,
130 .gain
= rpr0521_pxs_gain
,
131 .size
= ARRAY_SIZE(rpr0521_pxs_gain
),
135 struct rpr0521_data
{
136 struct i2c_client
*client
;
138 /* protect device params updates (e.g state, gain) */
141 /* device active status */
145 /* optimize runtime pm ops - enable device only if needed */
149 struct regmap
*regmap
;
152 static IIO_CONST_ATTR(in_intensity_scale_available
, RPR0521_ALS_SCALE_AVAIL
);
153 static IIO_CONST_ATTR(in_proximity_scale_available
, RPR0521_PXS_SCALE_AVAIL
);
155 static struct attribute
*rpr0521_attributes
[] = {
156 &iio_const_attr_in_intensity_scale_available
.dev_attr
.attr
,
157 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
161 static const struct attribute_group rpr0521_attribute_group
= {
162 .attrs
= rpr0521_attributes
,
165 static const struct iio_chan_spec rpr0521_channels
[] = {
167 .type
= IIO_INTENSITY
,
169 .address
= RPR0521_CHAN_ALS_DATA0
,
170 .channel2
= IIO_MOD_LIGHT_BOTH
,
171 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
172 BIT(IIO_CHAN_INFO_SCALE
),
175 .type
= IIO_INTENSITY
,
177 .address
= RPR0521_CHAN_ALS_DATA1
,
178 .channel2
= IIO_MOD_LIGHT_IR
,
179 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
180 BIT(IIO_CHAN_INFO_SCALE
),
183 .type
= IIO_PROXIMITY
,
184 .address
= RPR0521_CHAN_PXS
,
185 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
186 BIT(IIO_CHAN_INFO_SCALE
),
190 static int rpr0521_als_enable(struct rpr0521_data
*data
, u8 status
)
194 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
195 RPR0521_MODE_ALS_MASK
,
200 data
->als_dev_en
= true;
205 static int rpr0521_pxs_enable(struct rpr0521_data
*data
, u8 status
)
209 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
210 RPR0521_MODE_PXS_MASK
,
215 data
->pxs_dev_en
= true;
221 * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
223 * @data: rpr0521 device private data
224 * @on: state to be set for devices in @device_mask
225 * @device_mask: bitmask specifying for which device we need to update @on state
227 * We rely on rpr0521_runtime_resume to enable our @device_mask devices, but
228 * if (for example) PXS was enabled (pxs_dev_en = true) by a previous call to
229 * rpr0521_runtime_resume and we want to enable ALS we MUST set ALS enable
230 * bit of RPR0521_REG_MODE_CTRL here because rpr0521_runtime_resume will not
233 static int rpr0521_set_power_state(struct rpr0521_data
*data
, bool on
,
240 if (device_mask
& RPR0521_MODE_ALS_MASK
) {
241 if (on
&& !data
->als_ps_need_en
&& data
->pxs_dev_en
)
242 update_mask
|= RPR0521_MODE_ALS_MASK
;
244 data
->als_ps_need_en
= on
;
247 if (device_mask
& RPR0521_MODE_PXS_MASK
) {
248 if (on
&& !data
->pxs_ps_need_en
&& data
->als_dev_en
)
249 update_mask
|= RPR0521_MODE_PXS_MASK
;
251 data
->pxs_ps_need_en
= on
;
255 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
256 update_mask
, update_mask
);
262 ret
= pm_runtime_get_sync(&data
->client
->dev
);
264 pm_runtime_mark_last_busy(&data
->client
->dev
);
265 ret
= pm_runtime_put_autosuspend(&data
->client
->dev
);
268 dev_err(&data
->client
->dev
,
269 "Failed: rpr0521_set_power_state for %d, ret %d\n",
272 pm_runtime_put_noidle(&data
->client
->dev
);
280 static int rpr0521_get_gain(struct rpr0521_data
*data
, int chan
,
285 ret
= regmap_read(data
->regmap
, rpr0521_gain
[chan
].reg
, ®
);
289 idx
= (rpr0521_gain
[chan
].mask
& reg
) >> rpr0521_gain
[chan
].shift
;
290 *val
= rpr0521_gain
[chan
].gain
[idx
].scale
;
291 *val2
= rpr0521_gain
[chan
].gain
[idx
].uscale
;
296 static int rpr0521_set_gain(struct rpr0521_data
*data
, int chan
,
299 int i
, idx
= -EINVAL
;
302 for (i
= 0; i
< rpr0521_gain
[chan
].size
; i
++)
303 if (val
== rpr0521_gain
[chan
].gain
[i
].scale
&&
304 val2
== rpr0521_gain
[chan
].gain
[i
].uscale
) {
312 return regmap_update_bits(data
->regmap
, rpr0521_gain
[chan
].reg
,
313 rpr0521_gain
[chan
].mask
,
314 idx
<< rpr0521_gain
[chan
].shift
);
317 static int rpr0521_read_raw(struct iio_dev
*indio_dev
,
318 struct iio_chan_spec
const *chan
, int *val
,
319 int *val2
, long mask
)
321 struct rpr0521_data
*data
= iio_priv(indio_dev
);
327 case IIO_CHAN_INFO_RAW
:
328 if (chan
->type
!= IIO_INTENSITY
&& chan
->type
!= IIO_PROXIMITY
)
331 device_mask
= rpr0521_data_reg
[chan
->address
].device_mask
;
333 mutex_lock(&data
->lock
);
334 ret
= rpr0521_set_power_state(data
, true, device_mask
);
336 mutex_unlock(&data
->lock
);
340 ret
= regmap_bulk_read(data
->regmap
,
341 rpr0521_data_reg
[chan
->address
].address
,
344 rpr0521_set_power_state(data
, false, device_mask
);
345 mutex_unlock(&data
->lock
);
349 ret
= rpr0521_set_power_state(data
, false, device_mask
);
350 mutex_unlock(&data
->lock
);
354 *val
= le16_to_cpu(raw_data
);
357 case IIO_CHAN_INFO_SCALE
:
358 mutex_lock(&data
->lock
);
359 ret
= rpr0521_get_gain(data
, chan
->address
, val
, val2
);
360 mutex_unlock(&data
->lock
);
364 return IIO_VAL_INT_PLUS_MICRO
;
370 static int rpr0521_write_raw(struct iio_dev
*indio_dev
,
371 struct iio_chan_spec
const *chan
, int val
,
374 struct rpr0521_data
*data
= iio_priv(indio_dev
);
378 case IIO_CHAN_INFO_SCALE
:
379 mutex_lock(&data
->lock
);
380 ret
= rpr0521_set_gain(data
, chan
->address
, val
, val2
);
381 mutex_unlock(&data
->lock
);
389 static const struct iio_info rpr0521_info
= {
390 .driver_module
= THIS_MODULE
,
391 .read_raw
= rpr0521_read_raw
,
392 .write_raw
= rpr0521_write_raw
,
393 .attrs
= &rpr0521_attribute_group
,
396 static int rpr0521_init(struct rpr0521_data
*data
)
401 ret
= regmap_read(data
->regmap
, RPR0521_REG_ID
, &id
);
403 dev_err(&data
->client
->dev
, "Failed to read REG_ID register\n");
407 if (id
!= RPR0521_MANUFACT_ID
) {
408 dev_err(&data
->client
->dev
, "Wrong id, got %x, expected %x\n",
409 id
, RPR0521_MANUFACT_ID
);
413 /* set default measurement time - 100 ms for both ALS and PS */
414 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
415 RPR0521_MODE_MEAS_TIME_MASK
,
416 RPR0521_DEFAULT_MEAS_TIME
);
418 pr_err("regmap_update_bits returned %d\n", ret
);
422 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
425 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
432 static int rpr0521_poweroff(struct rpr0521_data
*data
)
436 ret
= regmap_update_bits(data
->regmap
, RPR0521_REG_MODE_CTRL
,
437 RPR0521_MODE_ALS_MASK
|
438 RPR0521_MODE_PXS_MASK
,
439 RPR0521_MODE_ALS_DISABLE
|
440 RPR0521_MODE_PXS_DISABLE
);
444 data
->als_dev_en
= false;
445 data
->pxs_dev_en
= false;
450 static bool rpr0521_is_volatile_reg(struct device
*dev
, unsigned int reg
)
453 case RPR0521_REG_MODE_CTRL
:
454 case RPR0521_REG_ALS_CTRL
:
455 case RPR0521_REG_PXS_CTRL
:
462 static const struct regmap_config rpr0521_regmap_config
= {
463 .name
= RPR0521_REGMAP_NAME
,
468 .max_register
= RPR0521_REG_ID
,
469 .cache_type
= REGCACHE_RBTREE
,
470 .volatile_reg
= rpr0521_is_volatile_reg
,
473 static int rpr0521_probe(struct i2c_client
*client
,
474 const struct i2c_device_id
*id
)
476 struct rpr0521_data
*data
;
477 struct iio_dev
*indio_dev
;
478 struct regmap
*regmap
;
481 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
485 regmap
= devm_regmap_init_i2c(client
, &rpr0521_regmap_config
);
486 if (IS_ERR(regmap
)) {
487 dev_err(&client
->dev
, "regmap_init failed!\n");
488 return PTR_ERR(regmap
);
491 data
= iio_priv(indio_dev
);
492 i2c_set_clientdata(client
, indio_dev
);
493 data
->client
= client
;
494 data
->regmap
= regmap
;
496 mutex_init(&data
->lock
);
498 indio_dev
->dev
.parent
= &client
->dev
;
499 indio_dev
->info
= &rpr0521_info
;
500 indio_dev
->name
= RPR0521_DRV_NAME
;
501 indio_dev
->channels
= rpr0521_channels
;
502 indio_dev
->num_channels
= ARRAY_SIZE(rpr0521_channels
);
503 indio_dev
->modes
= INDIO_DIRECT_MODE
;
505 ret
= rpr0521_init(data
);
507 dev_err(&client
->dev
, "rpr0521 chip init failed\n");
511 ret
= pm_runtime_set_active(&client
->dev
);
515 pm_runtime_enable(&client
->dev
);
516 pm_runtime_set_autosuspend_delay(&client
->dev
, RPR0521_SLEEP_DELAY_MS
);
517 pm_runtime_use_autosuspend(&client
->dev
);
519 return iio_device_register(indio_dev
);
522 static int rpr0521_remove(struct i2c_client
*client
)
524 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
526 iio_device_unregister(indio_dev
);
528 pm_runtime_disable(&client
->dev
);
529 pm_runtime_set_suspended(&client
->dev
);
530 pm_runtime_put_noidle(&client
->dev
);
532 rpr0521_poweroff(iio_priv(indio_dev
));
538 static int rpr0521_runtime_suspend(struct device
*dev
)
540 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
541 struct rpr0521_data
*data
= iio_priv(indio_dev
);
544 /* disable channels and sets {als,pxs}_dev_en to false */
545 mutex_lock(&data
->lock
);
546 ret
= rpr0521_poweroff(data
);
547 mutex_unlock(&data
->lock
);
552 static int rpr0521_runtime_resume(struct device
*dev
)
554 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
555 struct rpr0521_data
*data
= iio_priv(indio_dev
);
558 if (data
->als_ps_need_en
) {
559 ret
= rpr0521_als_enable(data
, RPR0521_MODE_ALS_ENABLE
);
562 data
->als_ps_need_en
= false;
565 if (data
->pxs_ps_need_en
) {
566 ret
= rpr0521_pxs_enable(data
, RPR0521_MODE_PXS_ENABLE
);
569 data
->pxs_ps_need_en
= false;
576 static const struct dev_pm_ops rpr0521_pm_ops
= {
577 SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend
,
578 rpr0521_runtime_resume
, NULL
)
581 static const struct acpi_device_id rpr0521_acpi_match
[] = {
585 MODULE_DEVICE_TABLE(acpi
, rpr0521_acpi_match
);
587 static const struct i2c_device_id rpr0521_id
[] = {
592 MODULE_DEVICE_TABLE(i2c
, rpr0521_id
);
594 static struct i2c_driver rpr0521_driver
= {
596 .name
= RPR0521_DRV_NAME
,
597 .pm
= &rpr0521_pm_ops
,
598 .acpi_match_table
= ACPI_PTR(rpr0521_acpi_match
),
600 .probe
= rpr0521_probe
,
601 .remove
= rpr0521_remove
,
602 .id_table
= rpr0521_id
,
605 module_i2c_driver(rpr0521_driver
);
607 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
608 MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
609 MODULE_LICENSE("GPL v2");